How do I tell iTunes to search my Library for something?
December 8, 2005 10:06 PM Subscribe
AppleScript: How do I tell iTunes to search my Library for something?
I'm finding AppleScript rather difficult which is weird because I know a lot of programming languages. This just has some funky syntax.
Anyway. I've tried:
tell application "iTunes" to search library for "search string"
and various alternatives. But the library doesn't understand my search. I would like to have a script that enters a particular search in the search bar of the main library window, and start playing the first track or some random track (if shuffle is selected), ultimatly.
Thanks for any help!
I'm finding AppleScript rather difficult which is weird because I know a lot of programming languages. This just has some funky syntax.
Anyway. I've tried:
tell application "iTunes" to search library for "search string"
and various alternatives. But the library doesn't understand my search. I would like to have a script that enters a particular search in the search bar of the main library window, and start playing the first track or some random track (if shuffle is selected), ultimatly.
Thanks for any help!
You would, possibly, want to use a variable for the name of the playlist...
There is a bit of logic in the script to start playing after the first track has been added, so you don't have to wait for the music to start until all the tracks have been added.
Also, take note of the repeat loops that wait for iTunes to do various things. iTunes is asynchronous -- if you tell iTunes to do something, it'll let your script continue while it does it. So at a couple points I had to put in a loop to make sure iTunes is finished with what the script told it to do. It's actually not a factor on my Mac (dual G5) but it could be on yours.
posted by kindall at 10:57 PM on December 8, 2005
There is a bit of logic in the script to start playing after the first track has been added, so you don't have to wait for the music to start until all the tracks have been added.
Also, take note of the repeat loops that wait for iTunes to do various things. iTunes is asynchronous -- if you tell iTunes to do something, it'll let your script continue while it does it. So at a couple points I had to put in a loop to make sure iTunes is finished with what the script told it to do. It's actually not a factor on my Mac (dual G5) but it could be on yours.
posted by kindall at 10:57 PM on December 8, 2005
If you really want to script the UI, there is Applescipt GUI Scripting.
posted by trevyn at 11:05 PM on December 8, 2005
posted by trevyn at 11:05 PM on December 8, 2005
Yeah, GUI scripting is a possibility, but unfortunately you will find iTunes isn't very easily scriptable that way. Its UI object hierarchy is pretty poor. You could try scripting it by simulating clicking and typing, but making that robust would be terribly icky.
posted by kindall at 11:25 PM on December 8, 2005
posted by kindall at 11:25 PM on December 8, 2005
My command of Applescript is pretty weak, but even I've encountered the ways in which it is hard to script iTunes to do what you want.
That said, there's a good discussion board on the topic of scripting itunes over at ipod lounge.
posted by adamrice at 7:22 AM on December 9, 2005
That said, there's a good discussion board on the topic of scripting itunes over at ipod lounge.
posted by adamrice at 7:22 AM on December 9, 2005
Response by poster: Thanks for the input everyone. Kindall, your script gets the job done, but not 100% what I had in mind. I think I'll tinker with getting the UI scripted. I thought that AppleScript did UI scripting.
I'm coming from Linux, and trying to get my Mac Terminal (shell) the way I like it. I've setup aliases to pass files to my Mac applications. I wanted to setup so if I give iTunes a path to a file or folder, iTunes plays it. If I just give it a string, then it searches its library and plays the first thing it comes up with. So Kindall's script will work for that.
So now..if I type (in the shell):
itunes Deftones
iTunes will copy what it searched for into the scripting playlist and play the music. If I type:
itunes deftones.mp3
it plays that particular deftones.mp3 from my current working directory. GREAT!
posted by nickerbocker at 9:56 AM on December 9, 2005
I'm coming from Linux, and trying to get my Mac Terminal (shell) the way I like it. I've setup aliases to pass files to my Mac applications. I wanted to setup so if I give iTunes a path to a file or folder, iTunes plays it. If I just give it a string, then it searches its library and plays the first thing it comes up with. So Kindall's script will work for that.
So now..if I type (in the shell):
itunes Deftones
iTunes will copy what it searched for into the scripting playlist and play the music. If I type:
itunes deftones.mp3
it plays that particular deftones.mp3 from my current working directory. GREAT!
posted by nickerbocker at 9:56 AM on December 9, 2005
AppleScript does do UI scripting, but it's an afterthought (because people kept complaining that you couldn't do it even though it's not what AppleScript is for) and it only works with the standard system UI controls. iTunes uses a lot of custom controls; if you run the UI Inspector on it, the main iTunes window is basically opaque to scripting; its only children are the scrollbars. As I said, you could do it by simulating clicking (the search field) and then typing (what you're searching for) and then clicking (the play button), but this is fairly "fragile."
Frankly I find UI scripting somewhat of an abomination because it gives developers an excuse not to give their applications a proper scripting object model and dictionary. "Just use UI scripting!" Bleah.
posted by kindall at 11:17 AM on December 9, 2005
Frankly I find UI scripting somewhat of an abomination because it gives developers an excuse not to give their applications a proper scripting object model and dictionary. "Just use UI scripting!" Bleah.
posted by kindall at 11:17 AM on December 9, 2005
The best iTunes scripting resource I know of is Doug's AppleScripts for iTunes. It sounds like this one does most of what you want.
posted by sudama at 7:13 PM on December 10, 2005
posted by sudama at 7:13 PM on December 10, 2005
« Older How can I do "track changes" stuff without using... | How do I give donations in others' names without... Newer »
This thread is closed to new comments.
So something like this ought to work:
tell application "iTunes"
if not (exists playlist "Script Playlist") then
make new playlist with properties {name:"Script Playlist"}
else
delete every track of playlist "Script Playlist"
repeat while (number of tracks of playlist "Script Playlist") > 0
delay 0.1 -- wait for the library to clear out
end repeat
end if
set trax to (search library playlist 1 for "Peter Gabriel")
set p to false
repeat with t in trax
try
duplicate t to playlist "Script Playlist"
if not p then
repeat while (number of tracks of playlist "Script Playlist") = 0
delay 0.1 -- wait for track to be added
end repeat
play track 1 of playlist "Script Playlist"
set p to true
end if
end try
end repeat
end tell
posted by kindall at 10:49 PM on December 8, 2005