Play Specific Song in iTunes Applescript

Discussion in 'Mac Programming' started by alternativemax, Jan 28, 2011.

  1. macrumors newbie

    Joined:
    Jan 28, 2011
    #1
    I have created a simple applescript to play a specific song in iTunes, play a random song, or play a playlist named "Sleep". I am having an issue with the random song function, where "play (next song)" is not playing the next song. Also, is there a better way to throw the error message at the bottom? If the text entered is part of the name of a song, then the song will play but then the error will come up anyway because there isnt a song that is named exactly what was typed in. Also, is there a way to create a choosable list of "results" so if there is more than one result you could choose the song you want? Sorry for asking so many questions, but I'm lost. Thanks everyone! Here's the code:


    repeat
    tell current application
    set song to the text returned of (display dialog "What song would you like to play? Or random?" default answer "")
    end tell

    tell application "iTunes"
    if song is "Random" or song is "random" then
    try
    set all_music to user playlist "Music"
    set shuffle of user playlist "Music" to false
    set shuffle of user playlist "Music" to true
    if current playlist is not "Music" then
    play user playlist "Music"
    else --FIX THIS
    play (next track)
    end if
    set shuffle of user playlist "Music" to false
    set shuffle of user playlist "Music" to true
    end try
    end if
    if song is "sleep" or song is "Sleep" then
    try
    set the_playlist to user playlist "Sleep"
    set shuffle of the_playlist to true
    if current playlist is not the_playlist then
    play the_playlist
    else
    play (next track)
    end if
    end try
    end if
    if song is not "Random" and song is not "random" and song is not "sleep" and song is not "Sleep" then
    try
    play track song
    on error errMsg number errorNumber
    try
    set results to (every file track of playlist "Library" whose name contains song)
    repeat with t in results
    play t
    end repeat
    end try
    end try
    if file track song in playlist "Library" exists then
    try
    end try
    else
    tell current application
    display dialog "Cannot locate song."
    end tell
    end if
    end if
    end tell
    end repeat
     

Share This Page