First, edit you Info.plist like that:
Code: Select all
<key>NSAppleScriptEnabled</key>
<true/>
<key>OSAScriptingDefinition</key>
<string>as.sdef</string>
Then, create as.sdef inside your app bundle's Contents/Resources dir, put this definition inside:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="myCoolApp">
<suite name="myCoolApp Suite" code="mApp" description="myCoolApp Scripts">
<command name="test" code="cmndtest" description="test command">
<cocoa class="myTestCommand"/>
<direct-parameter description="argument">
<type type="text"/>
</direct-parameter>
</command>
</suite>
</dictionary>
Code: Select all
; there is a big room for improvement, but that should be a good start
ProcedureC myTestCommand(command.i)
Protected argument.i = CocoaMessage(0,command,"evaluatedArguments")
If argument
Protected string = CocoaMessage(0,CocoaMessage(0,argument,"valueForKey:$",@""),"UTF8String")
If string
MessageRequester("test",PeekS(string,-1,#PB_UTF8))
Else
MessageRequester("test","no string")
EndIf
Else
MessageRequester("test","no arguments passed")
EndIf
EndProcedure
Define subClass = objc_allocateClassPair_(objc_getClass_("NSScriptCommand"),"myTestCommand",0)
class_addMethod_(subClass,sel_registerName_("performDefaultImplementation"),@myTestCommand(),"v@")
objc_registerClassPair_(subClass)
Now you can call your app from AppleScript:
Code: Select all
tell application "myCoolApp"
test "hello"
end tell
creating sdef file
SO question