iTunes/Applescript question

Am I just missing something, or is there nothing in the iTunes AppleScript suite to allow you to use the File menu items “Import…”, “Export Song List” and “Export Library”?

iTunes also doesn’t seem to be recordable via the Script Editor. Also sadly true?

I wouldn’t be surprised if these aspects of iTunes were deliberately left unaccessible from AppleScript, since they’re in a sense the most “valuable” aspects of the program. I’m sure there is some pressure on iTunes to avoid exposing things that make song piracy easier by automation.

What you’ve discovered with the recordability is typical of most apps on the Mac. It’s more work to design an app such that recording works, and for most people, it’s enough to ask that the app be scriptable at all, let alone recordable.

Daniel
www.red-sweater.com

Hmmm… I think you can “export” easilly duplicating the file “iTunes Music Library.xml” in your ~/Music/iTunes dir.
Export Song List maybe easy and more flexible asking iTunes for (eg):

{name, artist, year} of tracks of playlist x

And the “Import” function is easilly replicable using both “make new playlist” plus the “add” command.
About the recordability, what said Daniel is true and, additionally, I think there was still some bugs involving the record function (eg, Satimage disabled this function explicitly in Smile).

That’s the kind of work-around I’m imagining I’ll have to settle for, but far from having access to the real Import command. For a substantial sized library, all of the AppleEvent traffic of adding individual tracks would be much, much slower than telling iTunes to go parse a complete XML file by itself. An exported library can also contain multiple playlists, not just one playlist, yet another thing to have to hack around.

Not to mention, if you really wanted to take advantage of an existing XML file, having to parse the XML in AppleScript – not very pleasant, I imagine.

This sounds interesting to me, I am intending to do nearly the same thing (2 libraries, one on the internal hdd one on the external, of course much larger). Thus I want all new added albums copied from my external hdd to the internal hdd and all newly added to the internal hdd should go to the external hdd and, of course, the library. I am currently do in the following, to copy the last played albums and recently added albums from my external to the internal hdd.

following steps need to be done (i guess):

  1. create playlist of recently added albums
  2. copy trackfile to tempFolder of external hdd
  3. export .xml playlist of 1. (needed for playcount and rating info)
  4. search and replace the needed paths in the .xml
  5. delete internal iTunes library
  6. open external iTunes library
  7. add albums from tempFolder with the information of the .xml file (best case: import playlist)
  8. delete temFolder of external hdd after import
  9. create playlist of recently added and recently played albums (limitation will be max. 20gb of data)
  10. copy trackfile to tempFolder of internal hdd
  11. create new iTunes library on internal hdd
  12. import playlist via the .xml file of the external iTunes library
  13. delete tempFolder on internal hdd after import

this is what i got so far:


with timeout of 30000 seconds
	tell application "iTunes"
		if exists user playlist "export to internal drive" then
			delete every file track of user playlist "export to internal drive"
		else
			make new user playlist with properties {name:"export to internal drive"}
		end if
		tell me to createPlaylist("Recently Added")
		tell me to createPlaylist("Recently Played")
		-- start moving files
		set sel to every file track of user playlist "export to internal drive"
		set internal_drive to choose folder
		with timeout of 28800 seconds
			repeat with aFile in sel
				if aFile's class is file track then
					set art to artist of aFile
					set alb to album of aFile
					set file_path to (get aFile's location)
					tell me to move_it(file_path, internal_drive, art, alb)
				end if
			end repeat
		end timeout
		display dialog "Done!"
	end tell
end timeout

to createPlaylist(test)
	tell application "iTunes"
		repeat with aTrack in (file tracks of user playlist test)
			if genre of aTrack is not "Audiobook" then
				set tempAlbum to album of aTrack
				--Make sure we haven't already added this album
				set testAlbum to count (every file track of user playlist "export to internal drive" whose album is tempAlbum)
				if testAlbum = 0 then
					-- Nope, it's not in the playlist
					-- Let's check and see if there's room to add it
					-- Edit the next row (after: is less than) to set the maximum size limit. 15 GB for example is 1.5E+10, 7GB is 7E+9
					if (size of user playlist "export to internal drive" is less than 2.0E+10) then
						duplicate (every file track of library playlist 1 whose album is tempAlbum) to user playlist "export to internal drive"
					else
						exit repeat
					end if
				end if
			else
				exit repeat
			end if
		end repeat
	end tell
end createPlaylist

to move_it(file_path, internal_drive, art, alb)
	set artistFolder to art as Unicode text
	set albumFolder to alb as Unicode text
	set l_folder to internal_drive as Unicode text
	
	--check if artist & album folder exists
	tell application "Finder"
		if not (folder (l_folder & artistFolder) exists) then
			make new folder at l_folder with properties {name:artistFolder} --create artist folder
			if not (folder (l_folder & artistFolder & albumFolder) exists) then
				make new folder at l_folder & artistFolder with properties {name:albumFolder} --create album folder
			end if
		end if
		
		set the finalPath to folder (l_folder & artistFolder & ":" & albumFolder) as Unicode text
		
		try
			duplicate file file_path to folder finalPath with replacing --copy files to album folder
		on error eM number eN
			display dialog "Error " & eN & return & eM with icon 2
		end try
	end tell
end move_it

the search and replace .scpt is not yet implemented but it look like this:



tell application "TextEdit"
	activate
	open "/Users/goeste/Desktop/export to internal drive.xml"
	tell application "System Events"
		tell process "TextEdit"
			keystroke "f" using {command down}
		end tell
	end tell
	
	tell application "System Events"
		tell process "TextEdit"
			keystroke "file://localhost/Users/goeste/Music/local/iTunes%20Media/Music/"
			keystroke tab
			keystroke "file://localhost/Users/goeste/Desktop/Music/"
			tell window "Suchen"
				click button "Alles ersetzen"
				tell window "Find"
					keystroke "w" using {command down}
				end tell
			end tell
		end tell
	end tell
	
	tell application "TextEdit"
		set thedoc to document 1
		save thedoc
		close thedoc
	end tell
end tell

is my idea even possible? I appreciate any help :slight_smile:

cheers
goeste