Last active
February 24, 2016 11:00
-
-
Save aleemb/97b4f5f8510f397c4a08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- https://gist.github.com/aleemb/97b4f5f8510f397c4a08 | |
-- V3 | |
-- http://dougscripts.com/itunes/scripts/ss.php?sp=copytinforackstotracks | |
-- http://dougscripts.com/itunes/itinfo/info03.php | |
set root to "/Users/aleemb/Music/aleem" | |
set musicExtensions to {"mp3", "m4a"} | |
tell application "Finder" | |
-- remove xattr from file (ls -al@) since iTunes throws errors when adding these files | |
do shell script "xattr -c -r " & root | |
set _root to (POSIX file root) as alias | |
set _folders to every folder of _root | |
end tell | |
tell application "iTunes" | |
repeat with _folder in _folders | |
set _path to my text_to_list(_folder as text, ":") | |
set _genre to item -2 of _path | |
-- create playlist | |
if exists playlist _genre then | |
delete tracks of playlist _genre | |
set _playlist to playlist _genre | |
else | |
set _playlist to make new playlist with properties {name:_genre} | |
end if | |
-- add tracks | |
set _files to (every file of _folder where name extension is in musicExtensions) | |
repeat with _file in _files | |
set _path to my text_to_list(_file as text, ":") | |
set _genre to item -2 of _path | |
set _title to item -1 of _path | |
set _track to (add (_file as alias) to _playlist) | |
-- display dialog "Added " & _title & " to " & _genre | |
end repeat | |
end repeat | |
end tell | |
on text_to_list(txt, delim) | |
set saveD to AppleScript's text item delimiters | |
try | |
set AppleScript's text item delimiters to {delim} | |
set theList to every text item of txt | |
on error errStr number errNum | |
set AppleScript's text item delimiters to saveD | |
error errStr number errNum | |
end try | |
set AppleScript's text item delimiters to saveD | |
return (theList) | |
end text_to_list | |
return | |
-- update tags | |
tell application "iTunes" | |
set mainLibrary to library playlist 1 | |
set s to file tracks of mainLibrary | |
if s is not {} then | |
-- display dialog "\"Tags Mirror Genre\\Album-Artist-Title.ext\"" & return & return & ¬ | |
-- "For all selected tracks in iTunes, this script will update the Genre, title tags based on folder and filename:" & return & return & ¬ | |
-- "Genre\\Album - Artist - Title.ext" & return & return & ¬ | |
-- "(Album and Arist are both optional)" buttons {"Cancel", "Continue..."} default button 2 | |
set oldfi to fixed indexing | |
set fixed indexing to true | |
repeat with i from 1 to length of s | |
set t to item i of s | |
if class of t is file track then | |
tell t | |
try | |
if location is not missing value then | |
set loc to (get location as text) | |
set b to my text_to_list(loc, ":") | |
set n to my fixextension(loc) | |
try | |
set d to my text_to_list(n, " - ") | |
set name to item -1 of d | |
set artist to item -2 of d | |
set album to item -3 of d | |
end try | |
try | |
set grouping to item -2 of b | |
end try | |
end if -- is file track and not missing? | |
end try | |
end tell | |
end if -- file track? | |
end repeat | |
-- done | |
set fixed indexing to oldfi | |
try | |
-- display dialog "Tags Updated!" & return & return & ¬ | |
-- "Based on:" & return & ¬ | |
-- "Genre\\Album - Artist - Title.ext" buttons {"Thanks"} default button 1 | |
end try | |
else -- no tracks selected | |
display dialog "Did not fix any tags. You must first select some tracks in iTunes to fix tags." buttons {"Okay"} default button 1 with icon 0 -- giving up after 30 | |
end if | |
end tell | |
to fixextension(loc) | |
set finfo to (get info for alias loc) | |
return (text 1 through -((length of (name extension of finfo)) + 2) of name of finfo) as text | |
end fixextension | |
-- remove dead files | |
property progress_factor : 500 | |
tell application "iTunes" | |
-- display dialog "Super Remove Dead Tracks" & return & return & ¬ | |
-- "Removes tracks whose files are missing or deleted, so-called" & ¬ | |
-- "\"dead\" tracks designated with (!) next to the track name." buttons ¬ | |
-- {"Cancel", "Proceed..."} default button 2 | |
set mainLibrary to library playlist 1 | |
set totalTracks to count of file tracks of mainLibrary | |
set all_playlists to user playlists whose smart is false -- don't delete Smart Playlists later | |
set deleted_tracks to 0 | |
set all_checked_tracks to 0 | |
set countem to "" | |
set oldfi to fixed indexing | |
set fixed indexing to true | |
repeat with t from totalTracks to 1 by -1 | |
try | |
set this_track to file track t of mainLibrary | |
-- set _duration to duration of this_track | |
-- set _title to name of this_track | |
set loc to "." & this_track's location | |
if loc is missing value or loc contains ".Trash" or _title is missing value then | |
-- display dialog "Deleting: " & _title as string | |
delete this_track | |
set deleted_tracks to deleted_tracks + 1 | |
end if | |
set all_checked_tracks to all_checked_tracks + 1 | |
if frontmost then | |
if (progress_factor is not 0) and (all_checked_tracks mod progress_factor) is 0 then | |
if deleted_tracks is greater than 0 then ¬ | |
set countem to (deleted_tracks & " dead tracks removed so far...") | |
if frontmost is true then display dialog (all_checked_tracks as string) & ¬ | |
" tracks checked..." & ¬ | |
return & countem buttons {"Cancel", «data utxt266B»} giving up after 1 | |
end if | |
end if | |
end try | |
end repeat | |
set fixed indexing to oldfi | |
repeat with this_playlist in all_playlists | |
if (get count of tracks of this_playlist) is 0 then | |
try | |
delete playlist this_playlist | |
end try | |
end if | |
end repeat | |
if deleted_tracks is greater than 0 then | |
set ps to " was" | |
if deleted_tracks is not 1 then set ps to "s were" | |
display dialog "Removed " & deleted_tracks & " dead track" & ps & ¬ | |
"." buttons {"Thanks"} default button 1 with icon 1 | |
else | |
-- if gave up of (display dialog "No dead tracks found." buttons {"Thanks"} ¬ | |
-- default button 1 with icon 1 giving up after 15) is true then error number -128 | |
end if | |
end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment