It is currently Fri Jul 20, 2018 6:03 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 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: 182
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: 182
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  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 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