-- syncPcTivoToApple v 1.0 - An AppleScript to automatically import a network -- share of PC transferred iTune files to your Mac. -- -- Copyright 2008 Scott Selikoff -- Video Format property format_items : {"MPG4"} -- File extensions property ext_items : {"mp4"} -- Whether or not to perform import property okflag : false -- List of 'special' shows property convertNameToDateRecordedIdentifiers : {"The Daily Show With Jon Stewart", "The Colbert Report"} on run -- Set name of network share via the file system set network_folder to "my-ipod-share" -- Proceed if network share exists and iTunes is open tell application "Finder" set okflag to ((exists folder network_folder) and ((get name of every process) contains "iTunes")) end tell if okflag then import_files_to_itunes(network_folder) remove_duplicates("TV Shows") clean_track_name("TV Shows") end if end run -- Perform Import on import_files_to_itunes(this_folder) set these_items to list folder this_folder without invisibles repeat with i from 1 to the count of these_items set this_item to alias ((this_folder as text) & ":" & (item i of these_items)) set the item_info to info for this_item if (alias of the item_info is false) and  ((the file type of the item_info is in the format_items) or  the name extension of the item_info is in the ext_items) then tell application "iTunes" add this_item to playlist "Library" of source "Library" end tell end if end repeat end import_files_to_itunes -- Cleanup library of diplicates on remove_duplicates(this_playlist_name) tell application "iTunes" set this_playlist to user playlist this_playlist_name set all_tracks to the number of this_playlist's tracks set temp1 to {} set delete_list to {} if all_tracks > 1 then set this_location to the location of track 1 of this_playlist repeat with i from 2 to all_tracks set next_location to the location of track i of this_playlist if this_location is equal to next_location then copy i to end of delete_list -- then this track is a dupe; copy its index to our delete_list list end if set this_location to next_location end repeat -- total number of tracks to nix for dialog set to_nix to delete_list's length --If you must delete, do it backwards, ie: repeat with x from to_nix to 1 by -1 copy (item x of delete_list) to j delete file track j of this_playlist end repeat end if end tell end remove_duplicates -- Cleanup special tracks on clean_track_name(this_playlist_name) tell application "iTunes" set this_playlist to user playlist this_playlist_name set all_tracks to the number of this_playlist's tracks repeat with i from 1 to all_tracks set artistName to the artist of track i of this_playlist set titleName to the name of track i of this_playlist if ((titleName begins with artistName) and (titleName contains "\"")) then if (artistName is in convertNameToDateRecordedIdentifiers) then -- Case 1: Daily Show and Colbert Report, just use date as name set pos1 to ((length of artistName) + 2) set pos2 to ((length of artistName) + 9) else -- Case 2: All other shows strip out the artist name and date set pos1 to ((length of artistName) + 12) set pos2 to ((length of titleName) - 1) end if -- Position invariants must quality for renaming if ((pos1 < pos2) and (pos1 < (length of titleName)) and (pos2 > 1)) then set newTitleName to (text items pos1 thru pos2 of titleName) as text set name of track i of this_playlist to newTitleName end if end if end repeat end tell end clean_track_name