"Open in Script Debugger" links gone

These fora were unreachable for a while the other evening (UK time). Since then, I’ve not seen the “Open in Script Debugger” link with posted scripts. Is this known about?

NG

I’m seeing (or not seeing :wink: ) the same thing.
Running Google Chrome 59.0.3071.115 (3071.115) on macOS 10.11.6.

I recently upgraded the Discourse software that runs this site and I imagine something broke my hack to provide the Open in Script Debugger links. Thanks for the heads-up and I’ll look into the problem when I get a chance.

Cheers, Mark. Thanks for the quick reply.

NG

Work is ongoing on this problem. I’ve managed to restore AppleScript syntax highlighting. Unfortunately, the implementation of the JavaScript code that does the syntax highlighting has changed so much that my hack to introduce the Open in Script Debugger links is useless and must be reimplemented from scratch.

I’ll return to this at some point, but its a difficult task because I have to integrate with obfuscated JavaScript which is time consuming and frustrating.

Mark, if you will share your script to open SD with a new document and set the contents, I will provide an AppleScript to select all of the forum script, add a header, and open in SD. I already have the JavaScript to do this.

My original hack is available on GitHub.

Thanks, but what I’m looking for is AppleScript to open a new document in SD and set the content to the clipboard.

I’ve got this script, but how do I create a new document in a new tab of the current window?

tell application "Script Debugger"
  
  set cbStr to the clipboard
  
  --- THIS WORKS ---
  set oDoc to make new document with properties {source text:cbStr}
  
  ### How do I open a new document in a tab in the current window?  ###
  
  set oWin to window 1 -- works fine
  
  --- THIS FAILS ---
  set oDoc to make new document at oWin with properties {source text:cbStr}
  
end tell

Yes, I see that the make new document command is creating a new window every time - it should probably be obeying the Open In Tabs prefs setting.

In the meantime, you could use the AppleScript URL scheme:

set theSource to "testing" -- this will have to be URL encoded
open location "sdapplescript://com.apple.scripteditor?action=new&script=" & theSource

This seems to work with Safari:

use framework "Foundation"
use framework "AppKit"

-- The user's responsible for what's actually selected here.
tell application "Safari" to set scriptText to (do JavaScript "'' + document.getSelection()" in front document)

set |⌘| to current application
set scriptText to |⌘|'s class "NSString"'s stringWithString:(scriptText)
set ASCIIRangeAlphanumericCharacterSet to |⌘|'s class "NSCharacterSet"'s characterSetWithCharactersInString:("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
set escapedScriptText to scriptText's stringByAddingPercentEncodingWithAllowedCharacters:(ASCIIRangeAlphanumericCharacterSet)
set SDURLString to |⌘|'s class "NSString"'s stringWithFormat_("sdapplescript://com.apple.scripteditor?action=new&script=%@", escapedScriptText)

(*
using terms from scripting additions
	open location (SDURLString as text)
end using terms from
*)

-- Or:
set SDURL to |⌘|'s class "NSURL"'s URLWithString:(SDURLString)
tell |⌘|'s class "NSWorkspace"'s sharedWorkspace() to openURL:(SDURL)
1 Like

Thanks Nigel. Nice script.
I happened to have a handler for doJavaScript that runs in the frontmost browser, so I made a minor mod to your script to use it. So now, it should run in either Safari or Chrome. I ran one test in each browser that worked OK.

use framework "Foundation"
use framework "AppKit"

-- The user's responsible for what's actually selected here.

##  tell application "Safari" to set scriptText to (do JavaScript "'' + document.getSelection()" in front document)

set jsStr to "'' + document.getSelection()"

--- DO JavaScript in the FrontMost Browser ---
set scriptText to doJavaScript("", jsStr)

set |⌘| to current application
set scriptText to |⌘|'s class "NSString"'s stringWithString:(scriptText)
set ASCIIRangeAlphanumericCharacterSet to |⌘|'s class "NSCharacterSet"'s characterSetWithCharactersInString:("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
set escapedScriptText to scriptText's stringByAddingPercentEncodingWithAllowedCharacters:(ASCIIRangeAlphanumericCharacterSet)
set SDURLString to |⌘|'s class "NSString"'s stringWithFormat_("sdapplescript://com.apple.scripteditor?action=new&script=%@", escapedScriptText)

(*
using terms from scripting additions
  open location (SDURLString as text)
end using terms from
*)

-- Or:
set SDURL to |⌘|'s class "NSURL"'s URLWithString:(SDURLString)
tell |⌘|'s class "NSWorkspace"'s sharedWorkspace() to openURL:(SDURL)

--~~~~~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~

on doJavaScript(pBrowserName, pScriptStr)
  
  local jsScriptResults, frontAppName
  
  if (pBrowserName = "") then
    tell application "System Events"
      set pBrowserName to item 1 of (get name of processes whose frontmost is true)
    end tell
  end if
  
  try
    
    set jsScriptResults to "TBD"
    
    if pBrowserName = "Safari" then
      tell application "Safari"
        set jsScriptResults to do JavaScript pScriptStr in front document
      end tell
      
    else if pBrowserName contains "Chrome" then
      tell application "Google Chrome"
        set jsScriptResults to execute (active tab of window 1) javascript pScriptStr
      end tell
      
    else
      error ("ERROR:  Invalid Broswer Name: " & pBrowserName ¬
        & return & "MUST BE either \"Safari\" OR \"Chrome\"")
    end if
    
  on error e
    error "Error in handler doJavaScript()" & return & return & e
    
  end try
  
  return jsScriptResults
  
end doJavaScript

I see the links are back today. Thanks, Mark! :slight_smile:

1 Like

After a long absence, the “Open in Editor” button has returned.

For those that care, I was using a Discourse plugin for this which failed each time Discourse was revised. I’ve found a more robust method of doing this without the need for a plugin. You can add the following code to the </HEAD> section of the Discourse theme:

<script type="text/discourse-plugin" version="0.1">
    if (navigator.appVersion.indexOf('Mac') >= 0) { // Only do this on the Mac
        api.decorateCooked(
            $elem => {
                $elem.find('.lang-applescript').filter(function () {
		            return $(this).parents('.d-editor-preview').length < 1; // Don't do this in editor previews
            	}).each(function(index) {
   		            var src = encodeURIComponent(this.innerText);

        		    $( '<a class="widget-button btn btn-default" href="applescript://com.apple.scripteditor?action=new&script=' + src + '">Open in Editor</a>' ).insertAfter($(this).parent());
        	    });
            },
            { id: 'applescript-decorator' }
        );
    }
</script>

This code goes here:

3 Likes

Thanks, Mark.

When testing the button, I found that posted scripts were now opening in Script Editor instead of Script Debugger, even though I’d set the latter as my default editor and script files on my machine were opening in it when double-clicked. It turns out that being the default editor isn’t enough. Script Debugger’s interception of “applescript://com.apple.scripteditor?…” URLs has to be set in Script Debugger’s own preferences.

I’m pretty sure the links were “sdapplescript://com.apple.scripteditor?…” before, which is how I would have got the information for my script in post #10.

Yes, I think you are right. I’ll switch it to ‘Open with Script Debugger’ and see how it goes.

Yep. That’s working here. :slight_smile:

Great!

This method of inserting the Open In button is so much better. I can make changes without having to restart the entire server.

Just to be clear, we don’t need to paste that code ourselves, right? It just works. That’s for anyone else using discourse to run a forum or web page?

Yes, that’s correct. This is just FYI for other Discourse sites that may want this facility.

Seems like the Open in Script Debugger links are gone again