Script to add certain files to iTunes

Hi all,

I’m a bit new to AppleScript, so I’m not quite familiar with it’s nuiances.

I wrote this script that is to be used as a folder action, and adds any file under 30mb to a certain iTunes playlist. The code works fine in a script with a specific file, but it’s not working as a folder action, and I can’t quite figure out how to debug it. Any help would REALLY be appreciated right now because I’m at my wits end!

Here is the code:

-- set filetypes which you want iTunes to import
property myFiletypes : {".mp3", ".aac"}
on adding folder items to this_folder after receiving added_items
	-- successReporting variable is used to report whether or not it was a successful import. 0 is off. 1 is on. 
	set successReporting to 1
	-- get items
	set myFinderItems to added_items

	tell application "System Events"
		set allItems to every item of myFinderItems
	end tell

	-- create string in the format of mm-Month-year
	copy (current date) as string to z
	copy (((offset of (the month of (current date)) in "jan feb mar apr may jun jul aug sep oct nov dec ") + 3) / 4) as integer to intMonth
	if intMonth < 10 then copy "0" & intMonth as string to intMonth
	copy intMonth & "-" & (the month of (current date)) & "-" & (the year of (current date)) to str
	
	--create playlist title strings
	copy str & " Singles" to singlesPlaylist
	copy str & " Sets / Albums" to setsPlaylist
	
	--check if the correct playlists exist, if not, creates them
	tell application "iTunes"
		if not (user playlist singlesPlaylist exists) then
			make new user playlist with properties {name:singlesPlaylist}
		end if
		if not (user playlist setsPlaylist exists) then
			make new user playlist with properties {name:setsPlaylist}
		end if		
	end tell
	
	-- loop
	repeat with i from 1 to the count of allItems
		try
			-- get an item
			set myFinderItem to (item i of allItems)
			-- check if it is a folder
			tell application "Finder" to set myItemIsFolder to (kind of myFinderItem = "Folder")
			-- if it isn't a folder...
			if myItemIsFolder is false then
				-- see if the file has the right extension
				tell application "Finder" to set myItemExtension to "." & (name extension of myFinderItem) as string
				if myFiletypes contains myItemExtension then
					--check to make sure it's not a mix
					if ((size of myFinderItem) as integer) is less than 30000000 then
						-- add track to iTunes
						display dialog "added to itunes"
						--tell application "iTunes" to add myFinderItem to user playlist singlesPlaylist
					else
						--move to albums & mixes folder
						display dialog "moved to mixes"
						--	move myFinderItem to (this_folder & ":!albums & mixes")
						--	display dialog "Moved " & myFinderItem & " to albums & mixers folder"
					end if
				end if
			end if
			
			-- check if user requested a positive report. defaults as 1 - reports shown.
			if successReporting is 1 then
				display dialog "Your track(s) has been added to the iTunes Library."
			end if
			
		on error
			-- if there was an error report regardless of successReporting
			display dialog "Could not add file to iTunes library"
		end try
	end repeat
end adding folder items to

Hi amb 1545,

I don’t see how it could work. You set myFinderItems in this line using the Finder:

set myFinderItems to added_items
tell application “System Events”
set allItems to every item of myFinderItems
end tell

You get a list of Finder references which have form something like this:

file x of folder y of folder z …

Then you use the same rerference in iTunes:

set myFinderItem to (item i of allItems)

iTunes doesn’t know how to use Finder references. Here’s the iTunes dictionary for add command:

add  a list of alias  -- the file(s) to add
	[to  location reference]  -- the location of the added file(s)
Result:   track  -- reference to added track(s)

You see how it asks for a list of alias. You could probably just use an alias reference instead of a list, but I like to use list. So you can try changing the Finder reference to alias reference:

set myFinderItem to (item i of allItems) as alias

I didn’t try your script, but another thing is using nested tell blocks is not so good sometimes.

I think this was your situation that yuo were asking about. You have the line commented out:

–tell application “iTunes” to add (myFinderItem as list) to user playlist singlesPlaylist

and I assume :slight_smile: that this is the only thing that is not working.

Edit: I think I figured out why it worked on the single file. You probably used ‘choose file’ which returns an alias reference.

gl,

I appreciate the prompt reply, and I have noted your suggestions.

However, the problem specifically seems to be with my if statement that checks if the file is less than 30mb, when i have that commented out, the script works fine. However, if i try the same if command in a seperate script using a specific file, it works fine. Sooooo confused…

Ok, I see the line:

if ((size of myFinderItem) as integer) is less than 30000000 then

I’m kind of rushing, I think this statement is not in a Finder tell block. To use the size property something like this:

set the_file to choose file
tell application “Finder”
(size of the_file) as integer
end tell

I’ll post back with more tomorrow, if somebody else doesn’t.

iTunes probably still won’t be able to use the Finder references after your through testing with the dialogs.

Edit: also this won’t work because it is not in a tell block:

– move myFinderItem to (this_folder & “:!albums & mixes”)

gl,

You hit it right on the head!

The problem was that my if statement wasn’t in a finder tell. Doh!

Thanks for your help!