Trouble deleting a file

I have a script that I wrote (my first AppleScript) that automates the process of re-encoding downloaded TV shows for comparability with iTunes and AppleTV. Everything works great except for the final “clean up” step.

There are two files when all is said and done that need deleted.

  • The original downloaded file named tv.show.avi
  • The new encoded file named tv.show.avi.m4v

I can delete tv.show.avi with no problem but the m4v file won’t delete. I am sure it is just my method, but have not been able to figure out a better way. any help is appreciated.

The specific line that fails below is "delete the theFile & “.m4v”


tell application "Finder"
	-- Define folder containing TV shows.
	set ShowFolder to alias ((path to downloads folder as text))
	
	-- Create list of all files in the folder.
	set allFiles to files of ShowFolder
	
	-- Loop through all files in the folder
	repeat with theFile in allFiles
		copy name of theFile as string to FileName
		
		-- Loop through all TV shows
		repeat with Ext in {".avi", ".mkv"}
			if FileName ends with Ext then
			
				-- Get the POSIX path of file.
				set theShow to POSIX path of ((path to downloads folder as text)) & FileName
				
				-- Encode the show with Apple TV compatibility.
				do shell script "HandBrakeCLI -i " & theShow & " -o " & theShow & ".m4v" & " --preset=\"AppleTV\""
				
				-- Tag and import the show into iTunes
				tell application "iFlicks"
					import theShow & ".m4v" without gui
					delay 120
					quit
				end tell
				
				-- Clean up files
				tell application "Finder"
					try
						delete the theFile
						delete the theFile & ".m4v"
					end try
				end tell
			end if
		end repeat
	end repeat
end tell



What about:

tell app "finder" to delete alias ((theFile & ".m4v") as string)

PS: I recommend you to keep code as much as possible out of application tell-blocks, and even don’t use the Finder at all. I’ll insert my own version in a minute.

EDIT:
My script (NOT TESTED):

-- INIT
set showFolder to path to downloads folder
set AllowedExtensions to {".avi", ".mkv"}

-- SCRIPT
-- get all files
set allFiles to list folder showFolder

repeat with aFileName in allFiles
	if aFileName is in AllowedExtensions then
		-- construct full posix path of original file
		set fileHSF to ((showFolder as text) & aFileName) as text
		set filePOSIX to POSIX path of fileRef
		
		-- construct full posix path of dest file
		set destFileName to (nameWithoutExtensionOfAlias(alias fileHSF) & ".m4v") as string
		set destFileHSF to ((showFolder as text) & destFileName) as text
		set destFilePOSIX to POSIX path of destFileHSF
		
		-- do shell script
		do shell script "HandBrakeCLI -i " & quoted form of filePOSIX & " -o " & quoted form of destFilePOSIX & " --preset=\"AppleTV\""
		
		-- import
		(*-- Tag and import the show into iTunes
		tell application "iFlicks" to import (alias destFileHSF)
		delay 120
		tell application "iFlicks" to quit
		*)
		
		-- cleanup
		-- !!!! DELETE THE FILES IMMEDIATELY (UNCOMMENT TO USE) !!!! 
		(*
		do shell script "rm " & quoted form of filePOSIX
		do shell script "rm " & quoted form of destFilePOSIX
		*)
		
		-- move files to trash
		do shell script "mv " & quoted form of filePOSIX & " ~/.Trash"
		do shell script "mv " & quoted form of destFilePOSIX & " ~/.Trash"
	end if
end repeat

on nameWithoutExtensionOfAlias(anAlias)
	set fInfo to (info for anAlias)
	set fExt to name extension of fInfo
	set fName to name of fInfo
	
	if fExt is missing value then return fName
	
	set extCount to count (every character of fExt)
	set fName to (characters 1 thru -(extCount + 2) of fName)
	
	return fName as string
end nameWithoutExtensionOfAlias

Note that I made the iFlicks part into a comment, that’s because I don’t have it.

Hope it works,
ief2

Thanks, I will give this a try when I get home to night (don’t have a Mac at work)

Why is that?

Why the as text coercion at the end of the lines ?
The concatenation operator & works this way

“a” & “b” → “ab”
“a” & {“b”} → “ab”
{“a”} & “b” → {“a”, “b”}
“a” & 1 → “a1”
1 & “a” → {1, “a”}

The rule is:
The class of the result is the class of the left operator.
If the classes of both operators are different, the class of the right operator will be coerced to the class of the left one.
If the class of the right operator can’t be coerced to the class of the left operator, the class of the result is list.

Simply because I didn’t know! Thanks, this will save me a lot of time by not having to type all those parentheses and “as class”-lines.

Thank you,
ief2

As you announce your website as a ASS developer I recommend to learn the language. :wink:

Well, I’m sorry I don’t know everything. And if you want to reference to my site so bad I can quote literally:

In my opinion, someone who writes applications in a language can call him self a developer in that language. And what’s wrong with safety? Nobody gets hurt by an extra coercing?

Hello

So are you saying Stefan that the first script should have worked if the last line in the finder tell block


                 delete the theFile & ".m4v"

Should have worked if it was coerced into something like


                 delete (( theFile as text ) & ".m4v" ) as alias

Just curious about that

I might try it out in the mean time

No, I’m not saying this. Indeed it cannot work
theFile is a Finder file specifier, “.m4v” a literal string.
As a literal string cannot be coerced to a file specifier, the result is a list
and deleting a list throws an error

Hello

It was the next line i wondered about the one with coercing.
Realizing that I can’t coerce a Finder specifier directly to text I’ll coerce it through an alias before coercing it to text this
time. Then coercing the resulting text of the concatenation to an alias. That should make Finder happy? :slight_smile:
My Finder is to busy scanning files at the moment so I can’t test it.

I think this should work


-- inside the finder block, the first line of it should go well.
  delete (((theFile as alias) as text ) & ".mp4") as alias


You can do that can’t you?

Best Regards

McUsr

Thanks to everyone for your input and explanations. I have revised my code taking into account some of the rewrite and advice by ief2, and also leaving some of my own structure. Again, I can’t test this until tonight, so I would appreciate pointing out any glaring issues.


-- Create list of all files in the folder.
set allFiles to files of (path to desktop folder as string)

-- Loop through all files in the folder
repeat with theFile in allFiles
	copy name of theFile as string to theFileName

	-- Loop through all TV shows
	repeat with theExt in {".avi", ".mkv"}
		if theFileName ends with theExt then

			-- Get the POSIX path of files.
			set theSourceFile to POSIX path of (path to downloads folder as string) & theFileName
			set theDestFile to theSourceFile & ".m4v"

			-- Encode the show with Apple TV compatibility.
			do shell script "HandBrakeCLI -i " & theSourceFile & " -o " & theDestFile & " --preset=\"AppleTV\""

			-- Tag and import the show into iTunes
			tell application "iFlicks" to import theDestFile without gui
			delay 120
			tell application "iFlicks" to quit

			-- Clean up files
			do shell script "mv " & theSourceFile & " ~/.Trash"
       			do shell script "mv " & theDestFile & " ~/.Trash"
		end if
	end repeat
end repeat


Of course you can coerce a Finder file specifier to text inside a Finder tell block

delete ((theFile as text) & ".mp4") as alias

or even better

delete file (theFile as text) & ".mp4")

Consider that file in pure AppleScript environment and within a Finder tell block
are two different things

Hello

So you can really coerce a Finder file object directly to text, that was a relief!
Thank you very much Stefan!
My finder is still busy so I can’t test it yet. But in ASTDG M. Neuberg states that the Finder alias file is of a race of its own as well, but I knew from beforehand that I could make that coercion. Just a little bit perplexed of how much the stuff
differ form Applescripts usual file and alias objects.

I think your script will work grglander, but you should use “quoted form” like Ief did and study how he supplied parameters to the Iflics.app as the do shell script command “evaluates” the expression and rips of stuff like double quotes before sending it to the shell.

I think you should try StefanK’s coercing in your original script as well. :slight_smile:

Best Regards

McUsr

No offense !!

I’m very happy about your dedication about helping other people here on MacScripter.
It was just a suggestion to have more fun while developing scripts.
The “grammar” of AppleScript is not very complicated but powerful.
Like a real language it’s much more fun to be able to use sophisticated phrases.

My highest priority in writing code is effectivity.
For me the ideal code is (exactly in this order)

¢ 100% reliable
¢ as fast as possible
¢ as short as possible.

Of course extra coercions don’t hurt anybody, but also don’t serve anybody
and they defeat ideal code “rule” #2 and #3 :wink:

Happy scripting!

I’m sorry. I shouldn’t have reacted like that. Your sentence just came over a little bit “pedantic” and felt like you were attacking me.

Hello

I want to add something to StefanK’s words about reliability.
And this isn’t about anybody but if it is is primarily about my self.

How can I be sure that my code is reliable if I’m not 100% certain about how the language works?
Only when you are confident about the language you are writing in, only then can you be sure that
your code is reliable.

And well Apple script is in it self rather readable, so my list is equal to StefanK’s priorities.

Best Regards

McUsr