Random acts of coding: Moving Playlist Files

by Erica Sadun

On the off chance that this might prove useful to someone, I thought I'd share. Here's an AppleScript snippet that moves all the files from a playlist into a single folder you specify.

-- MoveItems:
-- Move all files associated with a playlist to a folder you specify
--
-- Erica Sadun, May 2007
--

tell application "iTunes"

-- Playlist to use
set myPlaylist to some playlist whose name is "tmp"

-- Location to move to. Assumes folder already exists
set newLocation to ("Data:tmp:" as alias)

-- Move each track file in this playlist
set tracklist to every track of myPlaylist
repeat with eachItem in tracklist
set nth to item ((location of eachItem) as string) of application "Finder"
move nth to newLocation
end repeat

end tell

13 Comments

hmm
2007-06-03 14:32:10
Seems redundant to create any iTunes applescript without first checking Doug's Applescripts for iTunes....so maybe not useful after all.


http://dougscripts.com/itunes/scripts/scripts07.php?page=2#syncplaylistfilestofolder

Mark Hunte
2007-06-03 16:22:08
So why are you pointing your url to a script that copies files, Erica,s is to move files.
I can only assume that it is a script to use as a way to tidy up your spread out files to one place.


I have written my own script for iTunes in the past that where to migrate the mess that iTunes left my library in to an ordered one.



Erica maybe it would be an idea to share why you wrote the script?


Erica Sadun
2007-06-03 21:08:04
Hi Mark, I'm working on a project to create multiple libraries for iTunes. This is just one of the scripts I built. I thought it was a nice one to share because it shows how to switch from iTunes terms into Finder terms. I spent a lot of time googling and didn't find any scripts online to do this before I ended up writing my own.
has
2007-06-04 03:33:59
Your code can be simplified a bit:


tell application "iTunes" to set aliasList to location of file tracks of playlist "tmp"
tell application "Finder" to move aliasList to folder "Data:tmp"


This asks iTunes to get all the locations as a list of aliases which Finder's move command is happy to accept.


HTH

Randy Smith
2007-06-04 05:48:53
I always love to see any AppleScript or any other script post here as often I don't have the time to check other places for scripts. Since I have kept an ordered library in iTunes since day one this script is only marginally useful. I can see using it to transfer some tracks from my main music mac, an eMac, to my MacBook.


Now if someone could write an AppleScript to copy the podcast playlist to an other playlist to make it easy for shuffle to sync podcasts would be nice. I sure would like an easy way to move podcasts to my shuffle. My regular iPod syncs them since I have everything synced but there is not way to choose podcasts as a way to fill the shuffle.

Scott Thompson
2007-06-04 06:20:58
Now wait a minute. I thought you were learning Ruby. You should now go back and rewrite your script in Ruby. :-) See
Erica Sadun
2007-06-04 08:07:26
Has: That's fabulous. Thanks.


Randy: I'll try to keep doing so.


Scott: There's actually some Ruby exploration in the pipeline. I've just got a lot of other things on my plate that I need to work through first and the multiple libraries is part of that.

has
2007-06-04 09:51:21
Here's the Ruby version, just for Scott.;)


#!/usr/bin/env ruby


require 'appscript'
include Appscript


aliases = app('iTunes').playlists['tmp'].file_tracks.location.get
app('Finder').duplicate(aliases, :to=>app.folders['Data:tmp'])

has
2007-06-04 09:58:03
s/duplicate/move/
Mark Hunte
2007-06-04 10:05:02
Erica, Is this what you mean for the shuffle, I do not have a shuffle so can only test this as far as making the playlist.


tell application "iTunes"
set Shuffle_List to "Pod_Shuffle"
set Pod_list to name of user playlist "Podcasts"
set pod_tracks to every track of user playlist "Podcasts"
if not (exists playlist Shuffle_List) then
make new playlist with properties {name:Shuffle_List}
end if
repeat with i from 1 to number of items in pod_tracks
set this_item to item i of pod_tracks
set track_location to location of this_item
add track_location to user playlist Shuffle_List
end repeat
set view of browser window 1 to playlist Shuffle_List
end tell

Mark Hunte
2007-06-04 10:10:49
Sorry posted the wrong script.
tell application "iTunes"
set Shuffle_List to "Pod_Shuffle"
set Pod_list to name of user playlist "Podcasts"
set pod_tracks to every track of user playlist Pod_list
if not (exists playlist Shuffle_List) then
make new playlist with properties {name:Shuffle_List}
end if
delete every track of user playlist Shuffle_List
repeat with i from 1 to number of items in pod_tracks
set this_item to item i of pod_tracks
set track_location to location of this_item
add track_location to user playlist Shuffle_List
end repeat
set view of browser window 1 to playlist Shuffle_List
end tell
Erica Sadun
2007-06-04 10:14:16
Hey Mark, we should really take this over to the other post--but since you posted here, here are the results of running your script. As far as I can tell, it bombs because I have items listed in my playlist that I have not yet downloaded.


ErrorShuffle2.jpg

Mark Hunte
2007-06-04 10:25:52
Ah I see, I will check that out,I thing a try block may solve that. I also just sent you a email with an updated faster script. But did not want to clutter the blog