Search code examples
variablesapplescriptitunes

Play music (song album or artist) with variables in AppleScript


I'm making trying to make an apple script that understands a text command "Play song We will Rock You" or "Play artist Queen" or "Play album A Night at the opera"

using songName artistName and albumName as variables, but i don't know how to set them, as they depend on the previuos word (song,artist,album) in an answer which is "Play song songName" (PLAY must be in the field).

I know how to make them play I just need to set those variables in order to use just one textfield.

Thanks for you help (hope I was clear)


Solution

  • Try:

    set xxx to text returned of (display dialog "Play song, Play artist, Play album " buttons {"Cancel", "OK"} default button "OK" default answer "Play song Here I Come")
    
    set myChoices to {"Play song ", "Play artist ", "Play album "}
    
    tell application "iTunes"
        repeat with i from 1 to count myChoices
            set aChoice to (item i of myChoices)
            if xxx starts with aChoice then
                set AppleScript's text item delimiters to aChoice
                set theRest to text items 2 thru -1 of xxx
                set AppleScript's text item delimiters to {""}
                set theRest to theRest as text
    
                if i = 1 then
                    play (first track whose name = theRest)
                else if i = 2 then
                    play (first track whose artist = theRest)
                else if i = 3 then
                    play (first track whose album = theRest)
                end if
                exit repeat
            end if
        end repeat
    end tell