iTunes Scripting Bridge - How to play a specific track
I am writing a mac application that uses the scrip开发者_运维知识库ting bridge to control iTunes. I have generated the header file iTunes.h, and the SBApplication object can tell me all kinds of wonderful things.
However, the commands seem very limited. There is a 'playpause' function, but I see no way to do this
// iTunes is my SBApplication Object [iTunes play:@"Smoke On The Water"];
The Applescript to do this is the fairly simple
tell iTunes play songName
and this Scripting bridge is supposed to be the super cool and powerful way for me to control scriptable objects in Objective C, so what am I missing here?
You should just send the message "playOnce:(BOOL)" on your iTunesTrack object. Here's an example:
iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
iTunesPlaylist *playlist = [iTunes currentPlaylist];
SBElementArray *tracks = [playlist tracks];
[[tracks objectAtIndex:0] playOnce:YES];
This will play the first track on the current playlist. Adapt to your situation.
Search the web for equivalent AppleScript snippets or view iTune's scripting dictionary in the AppleScript Editor app. Then translate the resulting script into scripting bridge commands. A possible AppleScript equivalent is:
tell application "iTunes"
play first item of (search first library playlist for "Smoke On The Water")
end tell
P.S. You might find objc-appscript easier to work with for this (or, if Objective C is not a requirement, its cousins, py-appscript or rb-appscript).
精彩评论