Help w/ Adding Files to iTunes

Help w/ Adding Files to iTunes

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set this_name to the name of this_folder
	end tell
	repeat with i from 1 to count of items in these_items
		set this_item to item i of these_items
		dealWithFile(this_item)
-- Unsure, but do I need to delete this_item now?
	end repeat
end adding folder items to

on dealWithFile(the_file)
	set ext to getExtension(the_file)
	if ext is "Text File" then
		copyFile(the_file, getiPodPath() & "/Notes/")
	else if ext is "iCal Calendar" then
		copyFile(the_file, getiPodPath() & "/Calendars/")
	else if ext is "AddressBook Card" then
		copyFile(the_file, getiPodPath() & "/Contacts/")
	else if ext is "iTunes Song" then
		addToLibrary(the_file)
	end if
end dealWithFile

on addToLibrary(this_file)
	--This is where I don't know what to do! 
	--this_file is a file, and it is already known to be a QuickTime compatible format
	
end addToLibrary

on copyFile(the_file, the_dir)
	do shell script "mv " & unix_path(POSIX path of the_file) & " " & the_dir
end copyFile

on getExtension(the_file)
	set i to info for the_file
	if name extension of i is "txt" then
		return "Text File"
	else if name extension of i is "" then
		return "Text File"
	else if name extension of i is "ics" then
		return "iCal Calendar"
	else if name extension of i is "vcf" then
		return "AddressBook Card"
	else if name extension of i is "mp3" then
		return "iTunes Song"
	else if name extension of i is "aif" then
		return "iTunes Song"
	else if name extension of i is "aiff" then
		return "iTunes Song"
	else if name extension of i is "mp4" then
		return "iTunes Song"
	else if name extension of i is "m4a" then
		return "iTunes Song"
	else if name extension of i is "m4p" then
		return "iTunes Song"
	else if name extension of i is "aac" then
		return "iTunes Song"
	else
		return "File"
	end if
end getExtension

on getiPod()
	-- I'll prolly replace this with a loop that goes through the volumes looking for an ipod_control folder
	tell application "iTunes"
		repeat with i from 1 to the count of sources
			if the kind of source i is iPod then
				return source i
			end if
		end repeat
	end tell
end getiPod

on getiPodPath()
	set i to getiPod()
	set s to name of i
	set s to POSIX path of s
	return unix_path(s)
end getiPodPath

-- Thanks to Dirk for adding this subroutine
-- This ripped from Rip to iPod 
on unix_path(iPodPOSIX)
	set chars to every character of iPodPOSIX
	repeat with i from 1 to length of chars
		if "!$&"�'*(){[|;<>?~` \" contains (item i of chars as text) then
			set item i of chars to "\" & (item i of chars as text)
		end if
	end repeat
	return every item of chars as string
end unix_path

Ok, I need some help with the addToLibrary subroutine.

Thanks!!!

TheMadRedHatter

You could do it simply by opening the_file in iTunes.
It isnt very elegant but it works.

tell application "iTunes"
	open the_file
	delay 1
	if the player state is playing then
		stop
	else
		
	end if
end tell

I added a bit of code that will stop playback of the song because iTunes will start to play the file cause you have added it.
good luck,
JO

Ok, here’s what I have:

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set this_name to the name of this_folder
	end tell
	repeat with i from 1 to count of items in these_items
		set this_item to item i of these_items
		dealWithFile(this_item)
	end repeat
end adding folder items to

on dealWithFile(the_file)
	set ext to getExtension(the_file)
	if ext is "Text File" then
		copyFile(the_file, getiPodPath() & "/Notes/")
	else if ext is "iCal Calendar" then
		copyFile(the_file, getiPodPath() & "/Calendars/")
	else if ext is "AddressBook Card" then
		copyFile(the_file, getiPodPath() & "/Contacts/")
	else if ext is "iTunes Song" then
		addToLibrary(the_file)
	end if
end dealWithFile

on addToLibrary(this_file)
	try
		set myipod to getiPod()
		if myipod is not "" then
			tell application "iTunes"
				set this_track to add this_file to source myipod
			end tell
			try
				do shell script "rm " & unix_path(POSIX path of this_file)
				
			end try
		end if
	end try
end addToLibrary

on copyFile(the_file, the_dir)
	do shell script "mv " & unix_path(POSIX path of the_file) & " " & the_dir
end copyFile

on getExtension(the_file)
	set i to info for the_file
	if name extension of i is "txt" then
		return "Text File"
	else if name extension of i is "" then
		return "Text File"
	else if name extension of i is "ics" then
		return "iCal Calendar"
	else if name extension of i is "vcf" then
		return "AddressBook Card"
	else if name extension of i is "mp3" then
		return "iTunes Song"
	else if name extension of i is "aif" then
		return "iTunes Song"
	else if name extension of i is "aiff" then
		return "iTunes Song"
	else if name extension of i is "mp4" then
		return "iTunes Song"
	else if name extension of i is "m4a" then
		return "iTunes Song"
	else if name extension of i is "m4p" then
		return "iTunes Song"
	else if name extension of i is "aac" then
		return "iTunes Song"
	else
		return "File"
	end if
end getExtension

on getiPod()
	set e to AppleScript's text item delimiters
	set s to ((path to startup disk as string) & "Volumes") as alias
	set AppleScript's text item delimiters to ASCII character 13
	do shell script "ls " & POSIX path of s
	set b to result
	repeat with i from 1 to count of text items in b
		try
			do shell script "ls " & s & unix_path(text item i of b) & "/iPod_Control"
			set x to text item i of b
			set AppleScript's text item delimiters to e
			return x
		end try
	end repeat
	set AppleScript's text item delimiters to e
end getiPod

on getiPodPath()
	set i to getiPod()
	set s to name of i
	set s to POSIX path of s
	return unix_path(s)
end getiPodPath

-- Thanks to Dirk for adding this subroutine
on unix_path(iPodPOSIX)
	set chars to ""
	repeat with i from 1 to count of items in iPodPOSIX
		if "!$&"’'*(){[|;<>?~` \" contains (item i of iPodPOSIX) then
			set chars to chars & "\" & (item i of iPodPOSIX)
		else
			set chars to chars & (item i of iPodPOSIX)
		end if
	end repeat
	return chars as string
end unix_path

The problem now seems to be that it just isn’t fired when attached to the iPod (or even a normal folder), I have folder actions turned on.

TheMadRedHatter

Looks like you figured out the correct way to add tracks to a playlist on your own. As far as what’s going on with your folder action, I’m assuming you added the action to your iPod itself. If you manually configured it, you should make sure that the name for the iPod is correct. When iTunes names iPods, it doesn’t use a normal apostrophe in the name (e.g., Bob Smith’s iPod) so this may be causing the problem.

To make sure that the script isn’t running, add a display dialog command to the very beginning of the script to see if it gets triggered. If it does, the problem is elsewhere.

There should really be a way to test non-standard scripts (e.g., idle handlers, folder actions, etc.) in Script Editor. Troubleshooting like this is way too much work.

Yep, tried the display dialog thing and it didn’t fire.

SirG3

There is already a standard Apple applescript for adding files to itunes.

Here it is:

property type_list : {“MPG3”, “MIDI”, “AIFF”, “MPG4”}

– since file types are optional in Mac OS X,
– check the name extension if there is no file type
– NOTE: do not use periods (.) with the items in the name extensions list
– eg: {“txt”, “text”, “jpg”, “jpeg”}, NOT: {“.txt”, “.text”, “.jpg”, “.jpeg”}
property extension_list : {“mp3”, “mid”, “aif”, “m4p”, “m4a”}
property dialog_timeout : 30

global counter, playlist_status, this_playlist

on adding folder items to this_folder after receiving these_items

-- set the following variable to the path to the folder whose contents
-- you want to add to the iTunes Music Library
set temp_folder to this_folder

-- set the following variable to 0 or 1 depending on what you want done
-- with the files after they are added to the library

-- 0 - files will be left alone (DEFAULT)

-- 1 - files will be deleted
--	(DO NOT use this option if you DO NOT have the "Copy files to iTunes Music folder
--	 when adding to library" option checked in the advanced section of the iTunes
--	 preferences)

set del_song to 1

process_folder(temp_folder, del_song)

end adding folder items to

– this sub-routine processes folders
on process_folder(this_folder, del_song)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as text) & “:” & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item, del_song)
else if (alias of the item_info is false) and ¬
((the file type of the item_info is in the type_list) or ¬
the name extension of the item_info is in the extension_list) then
process_item(this_item, del_song)
end if
end repeat
end process_folder

– this sub-routine processes files
on process_item(this_item, del_song)
– NOTE that the variable this_item is a file reference in alias format
– FILE PROCESSING STATEMENTS GOES HERE
try
tell application “iTunes”
launch
set this_track to add this_item to playlist “Library” of source “Library”
if the playlist_status is “OK” then
duplicate this_track to this_playlist
end if
end tell
set counter to counter + 1
end try
if del_song is 1 then
tell application “Finder”
delete this_item
end tell
end if
end process_item