0
votes

I've created an Applescript that lets me type in a bunch of commands. I'm working on the section for playing music. Basically, it removes the word play (which is the command) from the string with text delimiters and then searches for the rest of the string in iTunes. If the song is there, it plays it. But if it's not, it just quits. So, I'm wondering if I can display a dialog that says something like "Song not found" if the song isn't in the iTunes library. Here is my code:

display dialog "Enter a command" default answer "" with title "Enter a command"
set userInput to text returned of the result
if userInput contains "Play" then set {TID, text item delimiters} to {text item delimiters, {"Play "}}
set resultString to text item 2 of userInput
set text item delimiters to TID
set playSong to (resultString as string)
if userInput contains "Play" then tell application "iTunes"
    set mySongs to every track of library playlist 1 whose name is playSong
    repeat with aSong in mySongs
        play aSong
    end repeat
end tell

Thanks :)

1

1 Answers

0
votes

Just count the returned tracks:

if (count of mySongs) = 0 then
    display alert "Song not found"
end if

Full script:

display dialog "Enter a command" default answer "" with title "Enter a command"
set userInput to text returned of the result
if userInput contains "Play" then set {TID, text item delimiters} to {text item delimiters, {"Play "}}
set resultString to text item 2 of userInput
set text item delimiters to TID
set playSong to (resultString as string)
if userInput contains "Play" then tell application "iTunes"
    set mySongs to every track of library playlist 1 whose name is playSong
    repeat with aSong in mySongs
        play aSong
    end repeat
    if (count of mySongs) = 0 then
        display alert "Song not found"
    end if
end tell