Reputation: 13210
the idea of this script (simplified to show the issue) is checks if a track exists,
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
unfortunately if the track does not exist it gives error
"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"
'
instead of just printing 'no track found'
Upvotes: 3
Views: 500
Reputation: 7191
get first track
give a track object or an error
tracks
give a list of one track or an empty list
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
Upvotes: 4
Reputation:
Simplest way would be to use a try block
, like this:
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
BTW: use log
instead of print
and (get name of matchtrack)
instead of (name of matchtrack)
This would work but goes thru all the tracks in a named playlist:
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
Upvotes: 1