Sunday, December 4, 2022

#1 2021-07-30 10:12:50 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1518

NSString Error

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.

Applescript:

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.

Applescript:

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"

Last edited by peavine (2021-07-30 10:14:58 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#2 2021-07-30 05:48:16 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6799

Re: NSString Error

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:

Applescript:

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.)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2021-07-30 06:17:51 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1518

Re: NSString Error

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.

Last edited by peavine (2021-08-02 05:04:00 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)