Communicate with other Applications (OS X 10.5+)

BlitzMax Forums/Brucey's Modules/Communicate with other Applications (OS X 10.5+)

Brucey(Posted 2009) [#1]
While investigating how one might add AppleScript support to a BlitzMax app, I came across a newish API called Scripting Bridge. It requires 10.5 minimum, but is incredibly easy to use. So thumbs up from me :-)

BaH.ScriptingBridge implements the API so that you too can communicate with scriptable Mac applications - like iTunes, Mail, etc etc.

You want to see what track is currently playing on iTunes?
Local itunes:SBApplication = SBApplication.applicationWithBundleIdentifier("com.apple.iTunes")

Local track:SBObject = itunes.propertyAsObject("currentTrack")

Print "Track    : " + track.propertyAsString("name")

As you see, it's easy to use :-)

Here's some output from the provided example :
Executing:example_itunes.debug
iTunes
Album    : Home (Original Motion Picture Soundtrack)
Track    : C*m Dederit
Duration : 303.79998779296875
Size     : 10084034 bytes


It also allows you to retrieve lists of things, like tracks of a particular playlist.

At the moment, I've only got properties working, so you can't currently call methods on the App (like pausing iTunes, or selecting a new track), but once I figure it out, I'll add it.

It's currently available in my googlecode SVN, and requires at least OS X 10.5.

Fun fun :-)


Brucey(Posted 2009) [#2]
I've added a more interesting iTunes integration example, which shows how you might access data in "realtime", as well as things like album art.

Here's a screenshot :



The image data is pulled straight from iTunes, and loaded into a TImage via a RamStream.

Note that due to limitations in the default bitmap font (not unicode friendly!), some of the characters in the album name (above) do not show, but they are in the String retrieved from iTunes :-p


Brucey(Posted 2009) [#3]
A bit about AppleScript
Just FYI, when an application supports AppleScripts, it defines what functionality it has in .sdef files within its application bundle.
There is a command-line tool you can use to generate a header (.h) file of this, which you can then use to determine the names of classes, properties, methods and enumerations.

For example, to generate the details of iTunes you can run this in Terminal :
sdef /Applications/iTunes.app | sdp -fh --basename iTunes

It will create a file called iTunes.h in the current directory.

Happy hunting :-)


Brucey(Posted 2009) [#4]
Oh, and remember... this is applicable for ANY scriptable application on your Mac :-)


Difference(Posted 2009) [#5]
Working perfect. Incredible useful.

Your example is just what I was looking for.
sdef /Applications/iTunes.app | sdp -fh --basename iTunes
tip is very usefull

I can't believe how fast you code these things!

1000 thanks again.


Panno(Posted 2009) [#6]
hmm what should i say ?

cool ? great ?? thx brucey


Brucey(Posted 2009) [#7]
I can't believe how fast you code these things!

Well, most of the time is spent researching - googling, reading reference documentation, searching for more useful examples. Once I have a good idea in my head how I want it all to work, I'll knock out a quick module which calls into the API, and an example app which is empty other than the Framework line. If that compiles, then it's fairly quick to bang the rest together, although with this Objective-C, I'm having to refer back to the Apple docs a lot to make sure I'm using it right.

I'm still working out how to call a method.
I think I need to use "class_getMethodImplementation". Which is fine, except I'm not sure how parameters or returned values work yet. I'll get back to you on this one, after I've tinkered for a while :-)


Brucey(Posted 2009) [#8]
I think I need to use "class_getMethodImplementation"

... or not, in fact.

Updated again, adding the ability to call methods that don't have parameters.
With the example, you can now pause/play iTunes, and change the volume setting.


DavidDC(Posted 2009) [#9]
Awesome.


Brucey(Posted 2009) [#10]
...Has been released and is now available for download from my maxmods Googlecode site.

:o)

Have fun !


Difference(Posted 2009) [#11]
Got it from SVN, REV 902 vorking well.


Nigel Brown(Posted 2009) [#12]
Just added it to modules and built, example returns SBApplication not found? Still investigating so will let you know if I find anything.


Brucey(Posted 2009) [#13]
@Nigel. Did you get that working?
I'm on 10.6 here, but everything refers to 10.5 as the version it was available from.


Nigel Brown(Posted 2009) [#14]
@Brucey, sorry been off with a cold and have been rather unresponsive. Yes installed the latest BlitzMAX was working 2 version behind and all started working, very impressive. Is there an easy way to find out what commands are available from the application?


Brucey(Posted 2009) [#15]
Is there an easy way to find out what commands are available from the application?

See post #3 above, for a command-line tool you can use to generate a header which lists all functions and enums of an appropriate app.
I tried it on Eudora, which is an old app, and it worked on that too.

Of course, having a list of functions is only half the job. Working out how to use them all properly is something else entirely ;-)


Nigel Brown(Posted 2009) [#16]
oops, how did I miss that one, thank you.


Nigel Brown(Posted 2009) [#17]
Is it possible to write a MAX program with it's own a SDEF file associated with it, to enable the reverse of this and call my MAX program from a script?


Brucey(Posted 2009) [#18]
From my investigations, adding AppleScript support to BlitzMax is non-trivial...


FabriceW(Posted 2011) [#19]
Sorry to dig up this old thread but to stating the obvious, the ScriptingBridge seams to be broken in Lion. Due to the Sandboxing I think. I hope it can be fixed because i have an Application which relied on this module.

Thanks for any helm.


burpy(Posted 2012) [#20]
hi brucey. Any way to trigger mac system events (keypress simulation) from blitz? thanks.
Marc