2

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?

Jeff J
  • 139
  • 8

1 Answers1

1

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.

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Interesting. ASOC looks far more intuitive, but in the absence of decent documentation, I’m dreading yet another learning curve just now. As a quick alternative, I did some experimentation with iTunes and a little straight-up AppleScript: `tell application "iTunes" to play current track with once` Result: same behavior. The iTunes user interface has changed markedly in the past few years. Dare I suspect its Apple Event handling hasn’t received commensurate attention? It may not be possible at the moment to tell iTunes to play just one track. I’ll keep ASOC in mind for the future, though. – Jeff J Jul 05 '15 at 21:38
  • ASOC's simple enough if you already know Cocoa, and AppleScript, unlike SB, knows how to talk to apps correctly, so if something doesn't work then you know it's the app that's the cause, and not a problem in the bridge. Here's a [quick how-to on adding ASOC 'classes' to your ObjC project.](http://appscript.sourceforge.net/asoc.html) – foo Jul 07 '15 at 11:03
  • I finally concluded that for a one-line script, just using an NSAppleScript object made more sense than either ASOC or Scripting Bridge. But thanks for putting me on to ASOC: I’m sure it will come handy in the future. As for my primary question, after considerable experimentation I’ve concluded that there’s a lot in the “iTunes Dictionary” displayed by the AppleScript Editor that is either extremely quirky, or in a few cases doesn’t work at all. I’m tentatively attributing this to iTunes user interface development outpacing iTunes Apple Event handling. Thanks to all. – Jeff J Jul 12 '15 at 16:34