Creating Applescript to add Files to iTunes

Hello,

I am trying to create a droplet to add videos to iTunes. I am having problems passing the Destination variable to the set movedFile statement. The statement works if I explicitly list the destination, but if I pass the variable I get a class cfol error.

Also, if I explicitly list the destination the set new_file statement fails with


I have successfully used this line in an Automator workflow so I am puzzled.

Thanks in advance for any help.

on open droppedItems
	
	set Destination to missing value
	set videoKind to missing value
	set videoGenre to missing value
	set movedFile to missing value
	set new_file to missing value
	
	choose from list {"Climbing", "Movie", "TV Show"} with prompt "Please choose from" with title "Add to iTunes" without multiple selections allowed and empty selection allowed
	
	set choice to result
	
	if choice is "Climbing" then
		set Destination to "Little Big:Movies:_CLIMBING"
		set videoKind to "movie"
		set videoGenre to "Climbing"
	end if
	
	if choice is "Movie" then
		set Destination to alias "Little Big:Movies"
		set videoKind to "movie"
		set videoGenre to ""
	end if
	
	if choice is "TV Show" then
		set Destination to "Little Big:Movies"
		set videoKind to "TV show"
		set videoGenre to ""
	end if
	
	repeat with i in droppedItems
		tell application "Finder"
			set movedFile to move i to Destination
		end tell
		
		tell application "iTunes"
			set new_file to add (movedFile as alias) to playlist "Library"
			set video kind of new_file to videoKind
			set genre of new_file to videoGenre
		end tell
	end repeat
	
end open

Hi knhn. Welcome to MacScripter.

There are a few issues with your script.

Firstly, ‘choose from list’ returns a list of the chosen items, even when there’s only one item ” that is, not “Climbing” but {“Climbing”}. So none of the following comparisons in your script is ever true. If the “Cancel” button’s clicked, it returns ‘false’, so you have to be ready for that too.

Your 'Destination" values are just strings, so they need to be converted to folder references for the moves.

The result returned from each move is a Finder reference, which iTunes won’t understand. The ‘as alias’ you’ve used should do the trick, but it may be better to do it in the Finder ‘tell’ statement rather than the iTunes one.

I haven’t been able to test the version below, but it’s more likely to work. Let us know how it goes.

on open droppedItems
	
	set Destination to missing value
	set videoKind to missing value
	set videoGenre to missing value
	set movedFile to missing value
	set new_file to missing value
	
	choose from list {"Climbing", "Movie", "TV Show"} with prompt "Please choose from" with title "Add to iTunes" without multiple selections allowed and empty selection allowed
	
	set choice to result
	if (choice is false) then error number -128 -- Stop the script if Cancel button clicked.
	-- Otherwise, get the only item from the returned list.
	set choice to item 1 of choice
	
	if choice is "Climbing" then
		set Destination to "Little Big:Movies:_CLIMBING"
		set videoKind to "movie"
		set videoGenre to "Climbing"
	end if
	
	if choice is "Movie" then
		set Destination to alias "Little Big:Movies"
		set videoKind to "movie"
		set videoGenre to ""
	end if
	
	if choice is "TV Show" then
		set Destination to "Little Big:Movies"
		set videoKind to "TV show"
		set videoGenre to ""
	end if
	
	repeat with i in droppedItems
		tell application "Finder"
			-- NB. the referene 'folder Destination'. Coerce Finder result to alias
			set movedFile to (move i to folder Destination) as alias
		end tell
		
		tell application "iTunes"
			set new_file to add movedFile to playlist "Library"
			set video kind of new_file to videoKind
			set genre of new_file to videoGenre
		end tell
	end repeat
	
end open

Thanks Nigel for replying. Thanks for explaining the list result, false catcher, and alias issues.

Script now works. The only issue is that it throws a descriptor type mismatch at the end. I am commenting out lines of the script to try to find where this is coming from.

UPDATE:
the line below is the culprit

set video kind of new_file to videoKind

This is the entry from the iTunes dictionary:

I have an Automator workflow that uses this line of code, but does not have the videoKind variable. It works, no error.

set video kind of new_file to movie

Maybe pushing movie into the variable is tripping it up. Does applescript complain because “movie” is a string? In the Automator example, it compiles to purple. So maybe it’s an object of some kind?

Hi,

the value of the video kind property in iTunes is an enumerated constant, it’s not a string
So you have to set the variable in an iTunes tell block.
for example


 if choice is "Climbing" then
       set Destination to "Little Big:Movies:_CLIMBING"
       tell application "iTunes" to set videoKind to movie
       set videoGenre to "Climbing"
   end if

Thanks Stefan for pointing out the enumerated constant.

Here is the finished, working script for anyone that would like to use it for themselves. Enjoy! :smiley:

on open droppedItems
	
	set movedFile to missing value
	set new_file to missing value
	
	choose from list {"Climbing", "Movie", "TV Show"} with prompt "Please choose from" with title "Add to iTunes" without multiple selections allowed and empty selection allowed
	
	set choice to result
	if (choice is false) then error number -128 -- Stop the script if Cancel button clicked.
	set choice to item 1 of choice -- Otherwise, get the only item from the returned list.
	
	if choice is "Climbing" then
		repeat with i in droppedItems
			tell application "Finder"
				set movedFile to (move i to folder "Little Big:Movies:_CLIMBING") as alias
			end tell
			
			tell application "iTunes"
				set new_file to add movedFile to playlist "Library"
				set video kind of new_file to movie
				set genre of new_file to "Climbing"
			end tell
		end repeat
	end if
	
	if choice is "Movie" then
		repeat with i in droppedItems
			tell application "Finder"
				set movedFile to (move i to folder "Little Big:Movies") as alias
			end tell
			
			tell application "iTunes"
				set new_file to add movedFile to playlist "Library"
				set video kind of new_file to movie
			end tell
		end repeat
	end if
	
	if choice is "TV Show" then
		repeat with i in droppedItems
			tell application "Finder"
				set movedFile to (move i to folder "Little Big:Movies") as alias
			end tell
			
			tell application "iTunes"
				set new_file to add movedFile to playlist "Library"
				set video kind of new_file to TV show
			end tell
		end repeat
	end if
end open