Can I make a scriptable A.S.S. application?

Is there any way to put hooks into an Applescript Studio app for scripts to call my custom functions?

I can get there via interface scripting, but I’d rather make some nice easy methods that folks can call up.

I think there was some source code at Jon’s page:

If you can’t find it, perhaps you can ask him directly (?)

This would be the quick way.

Hmm, I don’t think I made code for this available on my site. Making an application truly scriptable is a lot of work. You have to make an SDEF document, an NSApplication object with methods with which to link and listen for the SDEF-defined commands, generate the *.scriptSuite and *.scriptTerminology plists, and, if you want those methods to run AppleScript code from your main script, some method for loading that script AND getting its current properties (since you can’t load the same script that’s instantiated when the app runs, you have to get creative and kludgy by either reading attributes from the interface or from user defaults – the later is preferable but then you have to make sure that both versions of the script are in synch). It is not for the faint of heart. Still, if this is the way you want to go, check out Donn Briggs’ Suite Modeler or Jean-Daniel Dupas’ Sdef Editor and join the AppleScript Implementers mailing list from Apple. (While code for making an AS Studio app scriptable is not available on my site, the AS_HotKey demo does show how to call a script handler from an Obj-C method, and this may be helpful.)

However, as I’ve advocated here before, AppleScript Studio apps already are scriptable to an extent. You can easily tell an object in your application to perform an action. Using this approach, if you have a handler in your script that you want to call, create a button in your app that, when clicked, runs that handler. You can make this button outside the bounds of your window if you want it hidden. Then, from elsewhere, just tell that button to perform its action. You can pass parameters to the handler by setting the title of the button or changing other properties of that button or other elements in your script. So, for instance, say you had a handler that you wanted to call from your AppleScript Studio app that looked like this:

on dd(m)
	display dialog m buttons {"OK"} default button 1 with icon 1
end dd

Now, if you want to call this handler from an external script, create a button in your application named “dd” and attach it to an “on clicked” action that included the following code:

on clicked the_object
	set object_name to name of the_object as Unicode text
	if object_name = "dd" then
		set m to title of the_object
		my dd(m)
	end if
end clicked

Finally, in your external script, you can call this like so:

tell application "MyApp"
	tell button "dd" of window "main"
		set title to "I've been called from an external script!"
		perform action
	end tell
end tell

If you don’t need parameters for your handler, just tell the button to perform its action and, if you need more than a single parameter, there are ways to get creative by passing delimited strings that the handler then parses to get the various parameters.

This method is a bit kludgy but for just making a single handler available to other scripts, it’s a whole lot easier than making your app really respond to AppleScript calls.


That is pretty kludgy, but I’m damned if it doesn’t get the job done! Also looks as though I can just make a button with the “hidden” property checked and do the same thing. Then it’s easier to find if I need to edit it later on.

I might just make a little helper application scriptlet that has that code built in but does a more normal sort of method call to make everything work.

Shame that Apple doesn’t make it possible for studio applications to publish methods to external apps. Seems like it should be a shoo-in.