Making an applescriptobjc application scriptable

How can an AppleScriptObjc application be made scriptable?
I had applications in Applescript Studio, where i could talk to my applications from an external script and fill tables, text views etc.
Is this feasible in AppleScriptObjc and how.
Thanks for any help

AppleScriptObjC apps are “normal” apps, so you add scriptability as you do with any other app. In Objective-C, that varies from relatively simple to very complex, depending on what you want to do. For instance, adding properties to an app is at the simple end, and if you only need simple commands targeted at the app, that’s relatively simple. You write the appropriate methods, write an .sdef file, set some details in the Info.plist file, and it pretty much works.

In Objective-C, adding methods to the app is easy – you use things called categories. Unfortunately you can’t write categories in AppleScriptObjC. You would have to subclass NSApplication, and add the methods to your subclass. I haven’t done it, but it shouldn’t be too hard.

But you’re going to have to spend some time with the docs…

As for the old Studio method, I know it was simple, but it was also a massive weakness – a simple bit of scripting and you could render apps useless.

Thanks Shane for your answer.

After a lot of reading, i could make the app scriptable and implemented some properties.
I can set these properties and use them in the app. This is anyhow a first step and I can continue with my project.
What i tried and can not get working is a implementing a command to reach the same result as with the properties.

Here are the steps :
added to info.plist
NSAppleScriptEnabled
YES

<key>OSAScriptingDefinition</key>
<string>myApp.sdef</string>

added myApp.sdef file to project:

<?xml version="1.0" encoding="UTF-8"?> 

---------this is the app code
script myAppAppDelegate
property parent : class “NSObject”

property externalTrigger : "false"
property theMessage : "default message"
property theFunction : "default function"


on awakeFromNib()
	--
end awakeFromNib

on applicationWillFinishLaunching_(aNotification)
	--
end applicationWillFinishLaunching_

# scriptability
on application_delegateHandlesKey_(sender, theKey)
	log "thekey=" & (theKey as string) & " --->externalTrigger=" & externalTrigger
	set flag to ((theKey as string) is "externalTrigger" and ((externalTrigger as string) is "true")) as boolean
	if flag then
		log "trigger is true"
		-- this adds a new row to a table
		set {stat, msg, nr} to KDMessengerLib's addMessage_Function_(theMessage as string, theFunction as string)
		set externalTrigger to "false"
	end if
	return theKey is in {"externalTrigger", "theMessage", "theFunction"}
end application_delegateHandlesKey_

on handleMessage_(sender)
	log "Handling message from external script command"
	set {stat, msg, nr} to KDMessengerLib's addMessage_Function_(theMessage as string, theFunction as string)
	return true
end handleMessage_

end script


How can I define a property to target a list and one to target a record?
What I should like to do is implement a command to call the method handleMessage_(sender) after setting the properties or even better use a record like : {amessage:“xxx”, afunction:“yyy”}.

Any suggestions and help are welcome.

I don’t see any obvious way to implement a command in a delegate – I suspect you’d need to subclass the app or use an Objective-C category. But rather than subclass the app, it might be easier to subclass its window, and add commands to that. Just a thought…

I am trying to add scriptability to an app and used akader’s whole script and .sdef file above as the template. I added one property, valOne (property valOne:“this is value one”), and made the bottom of the .sdef file look like this:

<property access="rw" code="mtrg" description="the trigger" name="trigger" type="text" >
                <cocoa key="externalTrigger" />
            </property>
            <property access="rw" code="xxva" description="first value" name="valueone" type="text" >
                <cocoa key="valOne" />
            </property>
        </class>
    </suite>
</dictionary>

When I run a script, I can get akader’s values for trigger or message, etc, but I can’t get mine. I keep getting “AppleEvent handler failed.” number -10000"

I noticed that in akader’s app, if I commented out the all the code except for the property definitions, I get the same error message with his properties. So, I put in some code to set my property, but that didn’t help. Is there something else one has to do to get this to work? I just can’t see what’s different about my propeerty.

Ric

Have you edited on application_delegateHandlesKey_(sender, theKey) so that it handles a theKey of valOne?

No, I wasn’t aware of that requirement, but I did that, and now it works. Thanks for the heads up

Ric