I was curious why the first of the following scripts returns the error shown but the second script works fine. Is there some way to get the first script to work without adding the property? Thanks.
use framework "Foundation"
use scripting additions
tell application "Script Debugger" to tell document 1
set allCode to source text
end tell
set theString to current application's NSString's stringWithString:"ok"
--> Script Debugger got an error: NSString doesn’t understand the “stringWithString_” message.
use framework "Foundation"
use scripting additions
property NSString : a reference to current application's NSString
tell application "Script Debugger" to tell document 1
set allCode to source text
end tell
set theString to NSString's stringWithString:"ok" --> "ok"
It’s a quirk of AppleScript. When you write a simple ASObjC script, anything addressed to current application gets passed to AppleScript to resolve. But if you address the host application first, then it is defined as current application (the “traditional” behavior of AS), and subsequent attempts to use ASObjC fail.
The simplest workaround is to address the app by id:
use framework "Foundation"
use scripting additions
tell application id "com.latenightsw.ScriptDebugger8" to tell document 1
set allCode to source text
end tell
set theString to current application's NSString's stringWithString:"ok"
(There’s a thread about it over on the Script Debugger forum.)
Thanks Shane–that explains everything. I noticed that Script Debugger has a preference option to “reference applications by ID when pasting tell blocks,” which makes the fix easy to implement.