I just finished a script to fill my mp3 player with random music from my iTunes library. the only problem is that occasionally the script ends with an error telling me that the file already exists on my player because the same iTunes song had been selected more than once by the random thingy. could someone please help me with a fix to check for the existance of the file before copying? this is my code so far:
tell application "Finder"
delete files in folder "Music" in disk "RCA_LYRA"
empty trash
tell application "iTunes" to set filePath to (location of some file track of playlist "General Music" of source "Library")
repeat until size of file filePath is greater than free space in disk "RCA_LYRA"
duplicate file filePath to folder "Music" in disk "RCA_LYRA"
tell application "iTunes" to set filePath to (location of some file track of playlist "General Music" of source "Library")
end repeat
eject "RCA_LYRA"
end tell
You can use this if you want. I rather get a set number of bytes first instead of getting a running free byte count because the free space might not update until the file is completely copied. But anyway shuffling the playlist is probably better:
set disk_names to (list disks)
set disk_name to (choose from list disk_names) as string
tell application “Finder”
set the_disk to disk disk_name
set max_size to free space of the_disk – in bytes
end tell
set cur_size to 0
tell application “iTunes”
set the_playlist to first playlist whose name is “Test Playlist”
set shuffle of the_playlist to false
set shuffle of the_playlist to true
set list1 to location of every track of the_playlist
repeat with this_loc in list1
tell application “Finder”
set this_size to size of this_loc
end tell
set cur_size to cur_size + this_size
if cur_size > max_size then
exit repeat
else
tell application “Finder” to duplicate this_loc to the_disk
beep 1
end if
end repeat
end tell
the script seems to pause while the file is transfering so I seem to be all right referring to the property every iteration of the copying loop. in my experience, storing values like that to variables tend to cause more problems than it solves (admittedly most of my coding experience is with visual basic :P). thanks for the script, but mine seems to be working all right with a few modifications (plus it’s easier for me to understand because I wrote it ;)). mostly our scripts seem to be more or less the same, except you like to structure your code a little differently than I do.
tell application "Finder"
-- erase existing music from player
delete files in folder "Music" in disk "RCA_LYRA"
empty trash
-- shuffle playlist and return the first song
tell application "iTunes"
set shuffle of playlist "MP3 Player" of source "Library" to true
set filePath to (location of file track 1 of playlist "MP3 Player" of source "Library")
set i to 1
end tell
-- copy this song to the player and randomly select another, repeating until full
repeat until size of file filePath is greater than free space in disk "RCA_LYRA"
duplicate file (filePath as string) to folder "Music" in disk "RCA_LYRA"
set i to i + 1
tell application "iTunes" to set filePath to (location of file track i of playlist "MP3 Player" of source "Library")
end repeat
-- remove shuffle property in preparation for next run of script
tell application "iTunes" to set shuffle of playlist "MP3 Player" of source "Library" to false
-- eject player so it can be unplugged
eject "RCA_LYRA"
-- beep to notify the user that it is now safe to unplug
beep
end tell
one more question… is it possible to connect the applescript to an interface with progressbars to indicate the total space remaining on the player and thus the approximate progress of the transfer?
You don’t want to reset the shuffle in the repeat loop because you’ll get duplicates. Basically, you shuffle the playlist outside the repeat loop, get a list of the tracks, and repeat through the list until you reach the max byte size.
I don’t know about progress indicators, but think there are scripting additions you can buy to do this or you can make your own with AppleScript Studio.
the repeat loop is limited to the block commented “copy this song to the player and randomly select another, repeating until full”, which doesn’t touch the shuffle property.
I don’t want status indicators enough to be willing to spend actual money for it :D, I was just curious if there was an easy way to tie it in with inferfacebuilder.
thanks for your help, my script is working very nicely now!