As part of an automation workflow I'd like to be able to store a reference to an applescript object inside an NSMutableDictionary. This is done, in effect, to be able to use variable variable names. Keeping an NSAppleScript instance alive, I'd then be able to refer back to previous results and continue from where I left off.
Concretely, I'd like to store a Presentation object (Microsoft PowerPoint) inside an NSMutableDictionary.
use framework "Foundation"
property memory : null
set memory to current application's NSMutableDictionary's dictionary()
-- function to get a ref to a presentation based on either a NAME or a PATH
on getPresentation(desc)
tell application "Microsoft PowerPoint"
if (item 1 of desc) is "" then
return item 1 of (get presentations whose path is (item 2 of desc))
else
return item 1 of (get presentations whose name is (item 1 of desc))
end if
end tell
end getPresentation
-- Fetch a currently open presentation (just an example)
-- this exists if you open a new presentation in PPT (no saving) and enter as title test
set pres to getPresentation({"test [Autosaved]", null})
memory's setObject:pres forKey:"presentation"
This throws the following error:
error "Can’t make «class pptP» 2 of application \"Microsoft PowerPoint\" into the expected type." number -1700 from «class pptP» 2
I'd like to be able to store the object pres for future use, refering to it from the containing Objective-C code (through NSAppleScript, calling methods on pres through wrappers as needed). I can tell the applescript to store the result of some handler at some address and use it later on.
I've tried using a reference to pres
to solve the issue, but no luck.
Is there a way to solve this? Perhaps a wrapping class around pres that allows it to be stored inside the NSMutableDictionary? Perhaps I can roll my own Objective-C code that would work on pres?
Any help would be much appreciated!
AppleScript’s reference
type is not bridged to ObjC so cannot cross the bridge directly. You can wrap it in a script
object (as long as that object inherits from NSObject
) and pass that over, though in past experience I found creating and using large numbers of bridged script objects also causes ASOC to crash.
Easiest solution is to keep AS references on the AS side, and store them a native data structure such as a key-value list, binary tree, or hash list. (e.g. My old Objects library provides reasonably fast DictionaryCollection
(hash list) objects created for exactly this reason.)