It is currently Sat Oct 31, 2020 8:16 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Integrating AppleScript to your app
PostPosted: Tue Oct 11, 2016 10:35 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 217
Location: Serbia
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

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
 Post subject: Re: Integrating AppleScript to your app
PostPosted: Sat Jun 03, 2017 4:13 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 217
Location: Serbia
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

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
 Post subject: Re: Integrating AppleScript to your app
PostPosted: Wed Jun 17, 2020 7:29 pm 
Offline
User
User
User avatar

Joined: Fri Feb 02, 2018 10:33 pm
Posts: 18
Location: New Jersey
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)

_________________
Mac OSX Sierra 10.13.6 & Windows 10, PB 5.71


Top
 Profile  
Reply with quote  
 Post subject: Re: Integrating AppleScript to your app
PostPosted: Fri Jun 19, 2020 8:38 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jan 12, 2011 3:48 pm
Posts: 217
Location: Serbia
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>

_________________
PB examples collection
pb-osx-globalhotkeys
pb-osx-notifications


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye