A Script Helper
If I'm going to be writing scripts, I need fast access to iTunes' Scripts folder in the Finder. I named this " Open Scripts Folder" with a space as the first character in the name so that it appears at the top of iTunes' Scripts Menu:
tell application "Finder" select folder "Scripts" of folder "iTunes" of folder "Applications" of startup disk open selection activate end tell [upm] click to open in Script Editor
I made this by using the recording feature of Script Editor 1.4.1 in OS 9.
Okay, back to work.
Random Tracks
In OS X, I pretty much use one Smart Playlist called "100 Songs" (I use other Smart Playlists for housekeeping tasks, but not for playing tracks). It simply gathers 100 random songs that haven't been played in the past two weeks. With Live Updating, it keeps the music fresh and familiar. My goal was to create a script that does something similar with iTunes 2.0.4 in OS 9.
"Make 100 Random Songs" is a script that goes through the library grabbing a random track. If the index of that track is not already stored in a list, then it is added to the list. This goes on until the list has 100 track indexes. Then a new playlist is created (if an older version exists it is deleted) and the 100 songs are copied to it, it's shuffled, and then played.
property myPlaylist : "100 Songs" property number_of_songs : 100 tell application "iTunes" set allThese to {} set lib to a reference to library playlist 1 set cntr to 0 try with timeout of 3000 seconds repeat until cntr is number_of_songs set x to index of (get some track of lib) (* --you can filter even more: set x to index of (get some track of lib whose genre is "Rock" or genre is "Blues") *) if allThese does not contain x then set the end of allThese to x set cntr to (cntr + 1) end if end repeat end timeout end try if playlist myPlaylist exists then delete playlist myPlaylist set newPlaylist to (make new user playlist with properties {name:myPlaylist}) repeat with thisIdx in allThese duplicate track thisIdx of lib to newPlaylist end repeat tell newPlaylist repeat 3 times set shuffle to false set shuffle to true end repeat end tell stop -- stop just in case play newPlaylist set view of front window to newPlaylist end tell [upm] click to open in Script Editor
Saved as a Classic Applet, it seems to work a little faster than a plain compiled script. As an Applet, it can still run from iTunes Scripts Menu.
The script can also do some simple filtering using whose. For instance I could filter for a particular Genre or length or Artist. But remember that too much Stuff will slow the script down. It takes a minute or two to run as it is now.
Here are some other scripts we have posted that also perform randomness:
- Random Album v1.5
- Creates playlist based on random album
- Random Classical v1.2
- Pre-iTunes 3 - keeps Classical tracks in order
- Random Library
- Shuffle Library tracks to new playlist
- Random to the End
- Creates random playlist and plays all tracks
- Randomize
- Randomize tracks in playlist
- Shuffle and Play by Genre
- New playlist based on Genre
Unfortunately last played date and play count hadn't been invented as of iTunes 2.0.4. Unless I wrote a Stay-Open script that kept track of the number of times the current track has been played...I could stuff a play count number in a track's comments...but a script like that would have to be running all the time, which means I would have to start it when iTunes started...and I really didn't want to be baby sitting the computer...OK, I could write a script for the Startup Items folder that did it...
OK, I snapped out of it and didn't make that script. But it could be done.