如何在不抛出错误的情况下检查 Applescript 中是否存在 Itunes 曲目
How can I check for existence of Itunes track in Applescript without throwing error
此脚本的想法(经过简化以显示问题)是检查曲目是否存在,
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"
'
而不只是打印 'no track found'
最简单的方法是使用 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
和 (get name of matchtrack)
而不是 (name of 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
get first track
给出轨迹对象或错误
tracks
给出一首曲目的列表或空列表
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
此脚本的想法(经过简化以显示问题)是检查曲目是否存在,
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"
'
而不只是打印 'no track found'
最简单的方法是使用 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
和 (get name of matchtrack)
而不是 (name of 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
get first track
给出轨迹对象或错误
tracks
给出一首曲目的列表或空列表
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