Search code examples
cocoaitunesscripting-bridge

Scripting iTunes: playing single tracks


I’m working on an app where I want to signal iTunes at certain times to play single tracks from whatever playlist happens to be active. I’ve just started studying Scripting Bridge, I’ve used the command-line tools Apple references to create an “iTunes.h” file, and I’ve got this and ScriptingBridge.framework in my project. Now I thought all I would have to do is

iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];

and when I want to cue whatever track is up next:

[iTunes playOnce:YES];

iTunes does respond, but if I understand Apple’s sample code, the YES parameter should compel it to play one and only one track. In fact, it doesn’t seem to matter whether it’s YES or NO: iTunes always plays from the selected track to the end of the playlist, however many tracks that may be. Am I missing something? And since I need to send only one very simple Apple Event, is there a simpler way to do this than going through the whole Scripting Bridge framework?


Solution

  • In Mac OS X 10.6 Snow Leopard Apple introduced the AppleScriptObjC framework which provides a much simpler bridging between AppleScript and Cocoa. No more ugly header files.

    In the script classes you can mix AppleScript with an Objective-C like syntax. There is an AppleScriptObjC template in Xcode, but you can add ASOC to an existing Cocoa project by adding the AppleScriptObjC framework and call in main.m

    [[NSBundle mainBundle] loadAppleScriptObjectiveCScripts];
    

    The Apple documentation is quite poor, there are some resources on macosxautomation.com, a comprehensive book written by Shane Stanley is available there, too.