CTMacUser
CTMacUser

Reputation: 2062

Can an AppleScript dictionary be created programmatically?

I was looking at the Apple Foundation scripting API. I got fascinated by NSScriptSuiteRegistry and the other parts of of the dictionary sub-API. The description even says that the functions that read an “.sdef” file into the app use this API. I’m grokking most of it, but I’m asking if I could go all the way; build the app’s scripting dictionary completely at runtime.

As an alternative, could I build an “.sdef” file in memory, then load it into the app?

Upvotes: 1

Views: 37

Answers (2)

Vincent
Vincent

Reputation: 31

This sample is a follow-on to the SimpleScriptingObjects sample, and it uses many of the techniques from the SimpleScriptingVerbs sample. After completing the steps defined in the SimpleScriptingObjects sample to set up and create a scriptable application, you can continue with the steps in this sample to add both scripting plugin capabilities to the application and an example scripting plugin.

The techniques presented here illustrate a number of interesting things you can do with a scripting plugin. These include:

(a) adding new scripting classes

(b) extending existing scripting classes

(c) adding new scripting commands

Briefly said, once an application is scriptable, allowing for scripting plugins is easy work. The modifications to the host application are minimal and very generic. No special code needs to be added to the existing scripting classes to allow for plugins. And, creating the scripting plugins is no more difficult than adding some additional scripting to the application. The scripting plugin itself is a simple Cocoa Loadable Bundle that contains one or more .sdef files describing its scripting functionality.

https://developer.apple.com/library/archive/samplecode/SimpleScriptingPlugin/Introduction/Intro.html

Upvotes: 1

Ted Wrigley
Ted Wrigley

Reputation: 3194

The technical answer to this is 'yes', though I've never actually tried to do it so I can't swear it works. That's the intended purpose of the registerClassDescription: and registerCommandDescription: methods. However, the sdef system is meant to make the process of coding and implementing script commands easier, since it's just a plain-text plist file.

If you want to give it a try, create objects of NSScriptClassDescription and NSScriptCommandDescription, setting all the properties and behaviors of the class or command within them, and then register those objects through the register commands. I believe the scripting system will infer suites from the objects themselves (at least, the initializers are of the form initWithSuiteName:…) and create readable sdefs on the fly when the user asks for them in Script Editor. But as I said: haven't done it; can't swear to it.

Upvotes: 1

Related Questions