"Open in Script Debugger" links gone

foundation
asobjc
safari

(Nigel Garvey) #1

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


(Jim Underwood) #2

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


(Mark Alldritt) #3

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.


(Nigel Garvey) #4

Cheers, Mark. Thanks for the quick reply.

NG


(Mark Alldritt) #5

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.


(Jim Underwood) #6

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.


(Mark Alldritt) #7

My original hack is available on GitHub.


(Jim Underwood) #8

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

(Mark Alldritt) #9

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

(Nigel Garvey) #10

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)

(Jim Underwood) #11

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


“Open in Script Debugger” links gone (bis)