Tuesday, January 19, 2016

AppleScript: iTunes: Creating a playlist with songs with lyrics

--Variables
set outputPlaylistName to "Songs with lyrics"
set maxTrackCount to 100000

-- Dialog Text input
display dialog "Enter the playlist name you want to search from" default answer ""
set inputPlaylist to text returned of result

-- Check input text
if inputPlaylist is not "" then

tell application "iTunes"

-- Playlist name present?
if (not (exists playlist inputPlaylist)) then
display dialog "No such playlist"
return
end if

-- Check number of result
set trackCount to count of tracks of playlist inputPlaylist

-- If number of tracks are over max
if (trackCount ≥ maxTrackCount) then
display dialog "Too many files in the playlist: " & trackCount
return
end if

-- Does the output playlist name exists?
if (exists user playlist outputPlaylistName) then
try
delete tracks of user playlist outputPlaylistName
end try
else
make new user playlist with properties {name:outputPlaylistName}
end if

-- Iterate through the input playlist
set resultTracks to every file track of playlist inputPlaylist
repeat with aTrack in resultTracks
if ((location of aTrack as string) is not equal to "missing value") then
-- if lyrics exists in the input track
if (not (lyrics of aTrack is equal to "")) then
-- Add to the output playlist
try
duplicate aTrack to user playlist outputPlaylistName
on error
log ("unknown error: " & (artist of aTrack as string) & " - " & (name of aTrack as string))
end try
end if
else
log (location of aTrack as string)
log ("file couldn't found: " & (artist of aTrack as string) & " - " & (name of aTrack as string))
end if
end repeat
end tell
end if