PureBasic Forum
https://www.purebasic.fr/english/

Integrating AppleScript to your app
https://www.purebasic.fr/english/viewtopic.php?f=19&t=66750
Page 1 of 1

Author:  deseven [ Tue Oct 11, 2016 10:35 pm ]
Post subject:  Integrating AppleScript to your app

Have you ever wanted to send some commands to your app via AppleScript? Well, that's totally possible with PureBasic! :)

First, edit you Info.plist like that:
Code:
<key>NSAppleScriptEnabled</key>
<true/>
<key>OSAScriptingDefinition</key>
<string>as.sdef</string>

Also make sure that your bundle id is unique.

Then, create as.sdef inside your app bundle's Contents/Resources dir, put this definition inside:
Code:
<?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>

After that, return to PB:
Code:
; 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)

And that's it.

Now you can call your app from AppleScript:
Code:
tell application "myCoolApp"
  test "hello"
end tell


Useful info:
creating sdef file
SO question

Author:  deseven [ Sat Jun 03, 2017 4:13 pm ]
Post subject:  Re: Integrating AppleScript to your app

More on that, string output example:
Code:
<command name="getTitle" code="cmndgtit" description="get title">
    <cocoa class="asGetTitle"/>
    <result type="text" description="current title"/>
</command>
Code:
ProcedureC asGetTitle(command.i)
  Shared currentTitle.s
  Protected answer = CocoaMessage(0,0,"NSString stringWithString:$",@currentTitle)
  ProcedureReturn answer
EndProcedure

Author:  robertfern [ Wed Jun 17, 2020 7:29 pm ]
Post subject:  Re: Integrating AppleScript to your app

Can you show a modified version that gets a list as a parameter, and returns a list that AppleScript understands.
Also how to modify the last three commands to handle multiple commands.

Code:
Define subClass = objc_allocateClassPair_(objc_getClass_("NSScriptCommand"),"myTestCommand",0)
class_addMethod_(subClass,sel_registerName_("performDefaultImplementation"),@myTestCommand(),"v@")
objc_registerClassPair_(subClass)

Author:  deseven [ Fri Jun 19, 2020 8:38 am ]
Post subject:  Re: Integrating AppleScript to your app

You can bind as many commands as you want, see here for example.

List input is something i never touched, but list output should be easy, i think you have to return NSArray, you can build it like that (untested):
Code:
list = CocoaMessage(0,0,"NSArray arrayWithObject:$",@val1)
list = CocoaMessage(0,list,"arrayByAddingObject:$",@val2)
list = CocoaMessage(0,list,"arrayByAddingObject:$",@val3)


Use something like that for definition:
Code:
<command name="getList" code="cmndglst" description="get list">
    <cocoa class="asGetList"/>
    <result type="text" list="yes" description="a list"/>
</command>

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/