Skip to content

Instantly share code, notes, and snippets.

@felixlindemann
Last active August 29, 2015 14:08
Show Gist options
  • Save felixlindemann/9e6fc68e9ae285a96cfc to your computer and use it in GitHub Desktop.
Save felixlindemann/9e6fc68e9ae285a96cfc to your computer and use it in GitHub Desktop.
Change TV-Shows Name for compatibility with DLNA-Server
--
-- Change TV-Shows Name for compatibility with DLNA-Server
-- by Felix Lindemann
-- no warrenty !
--
set iconPath to (path to applications folder as text) & "iTunes.app:Contents:Resources:iTunes.icns"
set counter to 0
-- init Progressbar
tell application "SKProgressBar"
activate
try
-- main window properties
set floating to false --> default is true
set position to {100, 100} --> default is {1000, 750}, origin point is bottom left
set width to 600.0 --> default is 500.0
set title to "Change TV-Shows' Sort Name by Felix Lindemann" --> default is "SKProgressBar"
-- header / footer properties
set header to "init" --> default is empty string
-- set header alignment to right --> default is left
set footer to "0/0" --> default is empty string
set footer alignment to center --> default is left
-- image path can be HFS or POSIX path, default is missing value (no image)
set image path to iconPath
set show window to true --> default is false
tell application "SKProgressBar"
tell progress bar
set indeterminate to false --> default is true
start animation -- for using Progressbar
end tell
end tell
tell application "iTunes"
set sel to selection -- these are the tracks, you have previously selected
if sel is not {} then
-- if selection is not empty
set TrMaxcounter to length of sel
set incrby to 100 / TrMaxcounter
set Trcounter to 0
set counter to 0
-- do for all tracks in selection
repeat with current_track in sel
set Trcounter to Trcounter + 1
-- if file is already handled - skip
set SortName to sort name of current_track
if SortName starts with "Original title: " then
-- update progressbar
tell application "SKProgressBar"
set footer to "Progress:" & Trcounter & "/" & TrMaxcounter --> default is empty string
set header to "Skipping: " & Trcounter --> default is empty string
tell progress bar
-- set current value to Trcounter --> default is 0.0
increment by incrby
end tell
end tell
else
-- get episode details
set EpName to (name of current_track)
set ser to (show of current_track)
set S to text -2 thru -1 of ("00" & (season number of current_track)) -- format leading zero
set E to text -2 thru -1 of ("00" & (episode number of current_track)) -- format leading zero
-- create new sortname
set SortName to ser & " S" & S & "E" & E & " - " & EpName -- & " -- kind: " & typ
-- keep original title
set origtitle to "Original title: " & EpName
-- only write if Track is TV-Show
if ("kind: " & (video kind of current_track)) is "kind: TV show" then
-- store original Title in Sort-Name
set sort name of current_track to origtitle
-- write new Episode name
set name of current_track to SortName
-- advance counter
set counter to counter + 1
end if
-- update Progess
tell application "SKProgressBar"
set footer to "Progress:" & Trcounter & "/" & TrMaxcounter --> default is empty string
set header to "New SortName: " & SortName --> default is empty string
tell progress bar
-- set current value to Trcounter --> default is 0.0
increment by incrby
end tell
end tell
end if -- done editing
end repeat -- done Loop
else -- Selection is empty
display dialog "Select the tracks you want to Change the Sortname of."
end if
end tell
-- display dialog "Done. " & counter & " Episode Sort-Names renamed"
on error errorMessage number errorNumber
-- Error Handling
display dialog "errorMessage: " & errorMessage & ", errorNumber: " & errorNumber
end try
tell progress bar
stop animation
end tell
quit -- terminate Progressbar
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment