如果找不到字符串,则返回显示对话框Applescript

时间:2014-08-11 10:46:10

标签: string applescript

我创建了一个Applescript,可以让我输入一堆命令。我正在研究播放音乐的部分。基本上,它从带有文本分隔符的字符串中删除单词play(这是命令),然后在iTunes中搜索其余字符串。如果歌曲在那里,它会播放它。但如果不是,它就会退出。所以,我想知道如果歌曲不在iTunes资料库中,我是否可以显示一个说“歌曲未找到”的对话框。这是我的代码:

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

谢谢:)

1 个答案:

答案 0 :(得分:0)

只计算返回的曲目:

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

完整脚本:

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