Applescript to update iTunes library

I have run into a problem that I need help on. I have 2 computers that are in seperate locations. I use a utility called chronosync to sync files between those computers. My only problem is this: I sync my itunes libraries between the 2 computers and I have yet to figure out an applescript to make itunes (post sync) look in the itnues folder for any music added that day. Can someone please give a suggestion or a code example to something that is probably so simple that I just can’t seem to get it… Thanks!

My suggestion is to shunt the music files to a folder outside of the iTunes library when you do your sync.
Then have a script to tell iTunes to ADD the music file in the folder to the library.

tell application "Finder"
	set the_file to items of (alias "Macintosh HD:Users:User:music-folder:")
end tell
the_file
tell application "iTunes"
	activate
	repeat with i from 1 to number of items in the_file
		set this_item to item i of the_file as alias
		add this_item to playlist "Library" of source "Library"
	end repeat
end tell

You most likely want to also set it to delete the files added from the music-folder. (this is assuming that your iTunes is set to copy files to its own folders.)

Hi,

the disadvantage to check all tracks in the library is: It takes a real long time.
This script runs more than 12 minutes for ca. 2800 tracks on a G5 dual 2,5.
Each 250th track a progress message will be displayed.
All added tracks appear in a new playlist named “New sync”.
If your music folder is somewhere else than the original location, you have to change the path in line 3.

set progress_factor to 250
set p_Name to "New sync"
set iTunesFolder to ((path to music folder) as string) & "iTunes:iTunes Music"

tell application "iTunes"
	if not (exists playlist p_Name) then
		make new playlist with properties {name:p_Name}
	end if
	delete every track of playlist p_Name
	set loc to location of tracks of playlist "Library"
	set cTracks to count tracks of playlist "Library"
end tell
set all_tracks to 0
tell application "Finder" to set theFolders to folders of (iTunesFolder as alias)
repeat with ix in theFolders
	try
		tell application "Finder" to set theFiles to files of entire contents of ix
		if class of theFiles is not list then set theFiles to {theFiles}
		repeat with iy in theFiles
			set all_tracks to all_tracks + 1
			if (iy as alias) is not in my loc then
				tell application "iTunes" to add (iy as alias) to playlist p_Name
			end if
			if (progress_factor is not 0) and (all_tracks mod progress_factor) is 0 then
				display dialog (all_tracks as string) & ¬
					" tracks checked..." buttons {«data utxt266B»} giving up after 1
			end if
		end repeat
	end try
end repeat
tell application "iTunes" to set syncCount to (count tracks of playlist p_Name) as string
display dialog (cTracks as string) & " tracks checked" & return & ¬
	syncCount & " new tracks added" buttons {"Done"} default button 1

It’s also good for finding dead music files (I found some on my machine)
Maybe here is some genius who makes the script faster :wink:

:cool:

set p_Name to "New sync"
set iTunesFolder to ((path to music folder as Unicode text) & "iTunes:iTunes Music") as alias

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
tell application "Finder" to set fPaths to paragraphs of ((files of entire contents of iTunesFolder) as Unicode text)
tell application "iTunes"
	if (playlist p_Name exists) then
		delete every track of playlist p_Name
	else
		make new playlist with properties {name:p_Name}
	end if
	set view of front browser window to playlist p_Name
	try
		set tPaths to paragraphs of ((location of tracks of library playlist "Library") as Unicode text)
	on error
		-- No tracks in the library!
		set tPaths to {}
	end try
end tell
set AppleScript's text item delimiters to astid

repeat with thisFilePath in fPaths
	if (thisFilePath is in tPaths) then
	else
		set thisFilePath's contents to thisFilePath as alias
	end if
end repeat

tell application "iTunes" to add (fPaths's aliases) to playlist p_Name

I knew it :slight_smile:

the way to coerce all aliases or file references to paths “paragraphs of (…)” is amazing

time: 2:25

Sorry, I’m a total newb and am just getting started… I was wondering how I would go about creating a script that does the following:

  1. Quit iTunes.
  2. Delete entire music folder (where iTunes library resides).
  3. Copy over a music folder from another computer.
  4. Restart iTunes.