Simple Script Question

Hey, I’m new to AppleScripting and creating a script to use in an Automator workflow.

The idea of the script is to delete a movie from iTunes (the UI) as well as the source file. I’ve put some code together just by looking around on the internet and pulling things from different scripts, but cannot find anything like this out there.

So here’s the code:


on deleteMovie(movieName)
	tell application "iTunes"
		set theMovie to track named movieName of playlist "Movies"
		set movieFile to location of theMovie
		delete theMovie
	end tell
	tell application "Finder"
		delete movieFile
	end tell
end deleteMovie

on run {input, parameters}
	
	tell application "iTunes"
		launch
		if (input is not missing value) then
			set numberOfMovies to number of items in input
			repeat (numberOfMovies - 1) times
				deleteMovie(input)
			end repeat
		end if
	end tell
	
	return input
end run

This script will compile and run, but will not do the intended task. Any help would be appreciated :slight_smile:

Oh, and Movies is a smart playlist that pulls all my movies from my iTunes library into a playlist.

The input is from automator.

Hi,

there are some issues in the script.

First of all, the script throws an error in the deleteMovie(input) line and then aborts immediately.
In an application tell block the keyword my is required in front of the handler call.
The iTunes block in the main handler is not needed anyway.

Second, input is a list of objects, you’re passing this list in each iteration of the loop instead of just one item

The question is, what kind of objects contains the variable input?
Assuming it’s a list of strings, try this. If input contains something else, the script won’t work


on deleteMovie(movieName)
	tell application "iTunes"
		set theMovie to track movieName of playlist "Movies"
		set movieFile to location of theMovie
		delete theMovie
	end tell
	tell application "Finder"
		delete movieFile
	end tell
end deleteMovie

on run {input, parameters}
	
	tell application "iTunes" to launch
	repeat with anItem in input
		deleteMovie(anItem)
	end repeat
	return input
end run

Stefan, Thanks for the quick reply :smiley:

All your changes make perfect sense to me. I’m a freshman in college coding in C++, so I’m not used to the almost less-formal syntax of C++. I feel like it’s too straight-forward sometimes, haha.

Although, when plugging the code provided into the automator workflow it says:
“iTunes got an error: A descriptor type mismatch occurred.”

and indicates the line:
set theMovie to track movieName of playlist “Movies”.

I’m assuming this is due to the fact that it says “track” which is most likely only for music when “movieName” is a movie file in iTunes. But I don’t know what the syntax would be to fix this, as Apple does not provide documentation and google turns up very little on the matter. Any ideas?

what is the class of the output of the preceding Automator action?

I’m using one of the presets “Find Finder Items” which results in the files’ paths.

Hmm, so I’m guess I would need to truncate the beginning of the path?

the class of the output is alias (AppleScript alias), so either you have to extract the name without the extension
or you check for the location which is also an alias.

try this (latter option)


on deleteMovie(movieLocation)
	tell application "iTunes"
		set theMovie to 1st track of playlist "Movies" whose location is movieLocation
		delete theMovie
	end tell
	tell application "Finder"
		delete movieLocation
	end tell
end deleteMovie

on run {input, parameters}
	
	tell application "iTunes" to launch
	repeat with anItem in input
		deleteMovie(anItem)
	end repeat
	return input
end run

The automator is just giving me an error that says “The action “Run AppleScript” encountered an error. Check the actions properties and try running the workflow again” and indicates:

1st track of playlist “Movies” whose location is movieLocation

then the other way (untested)


on deleteMovie(movieName)
	tell application "iTunes"
		set theMovie to track movieName of playlist "Movies"
		set movieFile to location of theMovie
		delete theMovie
	end tell
	tell application "Finder" to delete movieFile
end deleteMovie

on run {input, parameters}
	
	tell application "iTunes" to launch
	repeat with anItem in input
		tell application "Finder" to set {fileName, fileExtension} to {name, name extension} of anItem
		if fileExtension is not missing value then
			set offSetOfFileExtension to offset of fileExtension in fileName
			set fileName to text 1 thru (offSetOfFileExtension - 2) of fileName
		end if
		deleteMovie(fileName)
	end repeat
	return input
end run

Worked like a charm :smiley:

THANK YOU SO MUCH!!

you’re welcome