The Code
I really have two problems: I need a
script that does what I want it to do and something needs to see the
iPod is attached to my computer and run the script.
Choosing which language can be pretty important, especially when
you're trying to connect multiple programs. Most of
Apple's programs can work with shell scripting or
Perl , and I prefer
their speed and flexibility to other scripting languages. But Apple
has built AppleScript into iTunes, the program I want, so
it's a little easier to work with AppleScript for
this task.
Here's the AppleScript I use to
copy my files to my iPod:
property thePlaylist : "TestPlaylist"
property theiPod : "Professor Processor"
property theRecordingFolder : "Powerbook ¬
G4:Users:stevko:Desktop:hijack"
property theStorageFolder : "Powerbook ¬
G4:Users:stevko:Desktop:hijack:storage"
property tempPlaylist : "TempPlaylist"
property timeToKeepFilesInDays : 7
set fileList to (list folder theRecordingFolder)
tell application "iTunes"
set onList to {}
try
set pod to source theiPod
on error
display dialog ¬
"Could not find iPod " & theiPod buttons {"Cancel"}
end try
try
set podPl to user playlist thePlaylist of pod
on error
display dialog ¬
"Could not find playlist " & thePlaylist & " on iPod " &
theiPod buttons {"Cancel"}
end try
if fileList is not {} then
-- create temp playlist
try
set tempPl to (make new user playlist)
set name of tempPl to tempPlaylist
on error
display dialog ¬
"Could not create playlist with name " ¬
& tempPlaylist buttons {"Cancel"}
end try
-- add files to temp playlist
repeat with q from 1 to (count of fileList)
set thisFile to item q of fileList
if thisFile is not ".DS_Store" then
try
set pth to theRecordingFolder & ":" & thisFile
add file pth to tempPl
on error
-- ignore; if we can't get one,
-- we might be able to get more
end try
end if
end repeat
-- find if any of the names of the temp playlist items
-- are the same as the ones on the iPod playlist
set deleteList to {}
repeat with r from 1 to (count of tracks in tempPl)
set temptrack to track r of tempPl
repeat with s from 1 to (count of tracks in podPl)
set podtrack to track s of podPl
if name of podtrack is name of temptrack then
set n to name of temptrack
set deleteList to deleteList & n
exit repeat
end if
end repeat
end repeat
repeat with y in deleteList
try
delete track y in tempPl
on error
-- again, ignore; it's nice to delete extras,
-- but not necessary
end try
end repeat
-- remove any from iPod that are more than a week old,
-- or that have already been heard.
-- THIS DOES DELETE FILES OFF OF YOUR IPOD WITHOUT A WARNING!!
-- (That's one of the points of this Applescript)
set delList2 to {}
repeat with p from 1 to (count of tracks in podPl)
set ptt to track p of podPl
if (played count of ptt is greater than 0) or ¬
(played date of ptt is ((current date) ¬
- (timeToKeepFilesInDays * days))) then
set pptn to name of ptt
set delList2 to delList2 & name of ptt
end if
end repeat
repeat with y in delList2
try
delete track y in podPl
on error
-- again, ignore
end try
end repeat
-- copy everything that's on playlist to the iPod
-- optionally, this will also convert the files
-- at the same time; uncomment the line below to do so.
repeat with m from 1 to (count of tracks in tempPl)
set mt to track m of tempPl
-- convert mt
duplicate mt to podPl
end repeat
-- remove temp playlist
set delList3 to every track in tempPl
repeat with t in delList3
delete track (name of t) in library playlist 1 ¬
in source "Library"
end repeat
delete tempPl
else
display dialog ¬
"Could not find any files in folder " ¬
& theRecordingFolder buttons {"Cancel"}
end if
end tell
-- this moves the files from the recording folder to a storage folder
-- if you use delete instead of move, you can trash the files instead.
tell application "Finder"
repeat with t in fileList
try
move file t in folder theRecordingFolder to
theStorageFolder
on error
display dialog ¬
"Could not move file" & name of t & " from " ¬
& theRecordingFolder & " to " ¬
& theStorageFolder buttons {"Cancel"}
end try
end repeat
end tell