Making AS projects scriptabe

I’ve got a fairly simple call I’d like to make to my AS project, and I’m not sure how to enable it. Basically I want to be able to write a script that says:


tell application "MyApp"
select contact "john smith"
end tell

In the app, the list of contacts is a combo box full of names. Would I need cocoa for this, or can it be done all in applescript? I’m very new to AS, so my familiarity with all of the interface otpions is pretty low. I was looking in the ‘Files Owner’ appleascript menu in interface builder for an “tell” trigger. But it was not there. So I’m fearing that it’s a little more complex than I’d hoped.

Alternately, If I could just activate the app with a specified parameter (eg: activate myApp with contact equal to “john smith”) that would also work.

There are tutorials online, but they all talk about Cocoa apps. And I’m not exactly sure how I would implement that in my AS project (and I know nothing of cocoa). Any suggestions?

One important detail is unclear. Do you wish to send it variable data, or do you wish to merely activate a hardcoded action? In other words… using your example… would you like to be able to send select contact “XXXXX” and provide some other mechanism for filling in the XXXXX part? Or are you always sending a command to access the entry for “john smith”?

What I did was… Set up an ASS app that sets the contents of the general pasteboard to your desired contents (you can also use a basic script and not an ASS app, but you’ll need to access the ‘clipboard’ and not the pasteboard). Then send a command to your main app to process the pasteboard contents.

(* This code lives in the application sending the message *)
on clicked theObject
	if name of theObject is "sendMessage" then
		set preferred type of pasteboard "general" to "string"
		set contents of pasteboard "general" to "John Smith"
		tell application "MyApp"
			tell button "theButton" of window "theWindow" to perform action
		end tell
	end if
end clicked

Then, add a button to your main project (MyApp). This will be a dummy button that will receive your commands from other processes and do their requested actions. I made it borderless and placed it off the edge of the screen so it was ‘invisible’. Connect the button to the code below…which reads the pasteboard, and does something with the contents it receives. In this case, it just displays the contents in a text field, but you could do anything with it.

(* This code lives in the receiving application *)
on clicked theObject
	if name of theObject is "theButton" then
		set preferred type of pasteboard "general" to "string"
		set theContents to contents of pasteboard "general"
		set contents of text field "theText" of window "theWindow" to theContents
	end if
end clicked

You’ll have to determine what you want to achieve with your call to the main app… whether you want to just highlight john smith in the list or perhaps initialize/startup the app with john smith as the default… and then adjust your code accordingly. For a hardcoded implementation where you’re always selecting john smith, just get rid of all the pasteboard references and write your desired actions into the clicked handler for the ‘invisible’ button in the main app.

j

Variable data – not hardcoded.

Interesting technique using the clipboard… hadn’t thought of that (as is the “secret button” technique). Though also potentially troublesome, as it would wipe out anything currently in the clipboard, no?

Though more interesting is the bit at the end of your suggestions where you tell a button in the app to do something. Kinda indicates that the app is applescriptable, which it turns out it is (to some degree) as I can write:


tell application "MyApp"
	set contents of combo box "contact_name" of box "contact" of window "Main" to ("John Smith")
	activate
end tell

And it will do exactly what I want. I was thinking too complex in terms of applescripting. Thanks very much for the advice, it did the job.

Glad it worked. If you’re worried about setting the pasteboard you could read it and store it’s value then write it back to the pasteboard when your done. You could also write/read from a custom user defaults file, which does not conflict with any public resources.

j