如何在Applescript中检查Itunes曲目的存在而不抛出错误

如何在Applescript中检查Itunes曲目的存在而不抛出错误,applescript,itunes,Applescript,Itunes,该脚本(简化以显示问题)的思想是检查是否存在轨迹 tell application "iTunes" set matchtrack to get first track in playlist 1 whose persistent ID is "F1A68AD90AA66648" if matchtrack is not {} then print name of matchtrack else

该脚本(简化以显示问题)的思想是检查是否存在轨迹

    tell application "iTunes"
        set matchtrack to get first track in playlist 1 whose persistent ID is "F1A68AD90AA66648"
        if matchtrack is not {} then
            print name of matchtrack
        else
            print "no track found"
        end if
    end tell
不幸的是,如果轨迹不存在,则会出现错误

"iTunes got an error: Can’t get track 1 of playlist 1 whose persistent ID = \"F1A68AD90AA66648\"." number -1728 from track 1 of playlist 1 whose persistent ID = "F1A68AD90AA66648"
'

最简单的方法是使用
try block
,而不是只打印“找不到曲目”

,如下所示:

set persistentID to "F1A68AD90AA66648"
tell application "iTunes"
    try
        set matchtrack to get first track in playlist 1 whose persistent ID is persistentID
        if matchtrack is not {} then
            log (get name of matchtrack)
        else
            log "no track found"
        end if
    on error the error_message number the error_number
        log "Error (probably no track found)"
        -- display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
    end try
end tell
顺便说一句:使用
log
代替
print
(获取matchtrack的名称)
代替
(matchtrack的名称)

这会起作用,但会遍历命名播放列表中的所有曲目:

set flagFound to false
set mTrack to ""


# SETUP THOSE FIRST
# -------------------------------------------------------
property Library_Name : "Library" # "Mediathek" in german
property Playlist_Name : "Bob Marley"

set persistentID to "F1A68AD90AA66648"
# -------------------------------------------------------

tell application "iTunes"

    tell source Library_Name
        tell playlist Playlist_Name
            repeat with i from the (count of tracks) to 1 by -1

                if (the persistent ID of track i is persistentID) then
                    set mTrack to track i
                    set flagFound to true
                    exit repeat
                end if

            end repeat
        end tell
    end tell

    if flagFound then

        tell mTrack
            log "Found…"
            log "Name: " & (get name)
            log "persistent ID: " & (get persistent ID)
        end tell

    else

        log "no track with persistent ID " & persistentID & " found"

    end if

end tell

获取第一首曲目
给出一个曲目对象或一个错误

曲目
给出一个曲目列表或空列表

tell application "iTunes"
    set matchtrack to tracks in playlist 1 whose persistent ID is "F1A68AD90AA66648"
    if matchtrack is not {} then
        return name of item 1 of matchtrack
    else
        return "no track found"
    end if
end tell

好的,我明白了,但是在你的例子中,永远不会调用'log“no track found”'行,是吗?因为如果找不到track,它总是会给出错误?是的,当然,这就是为什么我们有on error部分,它会被记录下来。但是,else部分的意义是什么?我希望解决方案能够以不必抛出错误的方式进行检查。else语句是您代码中的遗留语句,我刚刚将其包装到一个try块中,但如果iTunes停止抛出此类错误,它很方便,并且应该保持在那里|-另一种方法见添加。使用try块来防止脚本不会因错误对话框而停止是绝对正常的。它不会改变脚本的运行方式,它将以相同的方式运行。AppleScript与其他语言在“try”这样的词上并不十分相似。