trying to trash .html files from a specific folder

Greetings. I’m fairly new to Applescript and would like to know if anyone can help me with writing a script that would move all of the .html files from a directory into the trash. Right now, I have put together the following script:

tell application "Finder"
	set thePath to (startup disk as text) & "Users:rototheg:Documents:testScript:"
	set theCount to count of items in thePath
	if theCount > 0 then
		repeat with i from 1 to theCount
			set theFileName to item i in folder thePath
			set theFileNameText to the name of theFileName as text
			if the name extension of theFileName is equal to "html" then
				move file theFileNameText in folder thePath to trash
			end if
		end repeat
	end if
end tell

The script compiles without a problem, but when I run the script, some of the .html files move to the trash, but I get this error:

Finder got an error: Can’t get item 2 of folder “Macintosh HD:Users:rototheg:Documents:testScript:”.

Does anyone have an idea what this error means? Any help would be greatly appreciated.

You might try simplifying your script like this:

tell application "Finder"
	set thePath to ((path to home folder) as string) & "Documents:testScript:"
	try
		move (every file in folder thePath whose name ends with ".html") to trash
	end try
end tell

Excellent solution, T.J. It worked and is certainly simpler.

If anyone else has insight on the “Can’t get item” error, I’d love to read it. I’ve been searching Google and other Applescript sites and haven’t really found anything.

Thanks for your help.

First problem with your code: thePath is incomplete. AS is returning this as thePath:

startup diskUsers:rototheg:Documents:testScript:

There are two problems with this. First, your path should actually start with the name of your startup disk, not the actual words ‘startup disk’ as a string. Second, in order to construct a valid HFS path, you need to add the “:” after your startup disk name, so What you actually want is code like:

set thePath to ((name of startup disk as text) & ":Users:rototheg:Documents:testScript:")

===NOTE the additional “:” right before ‘Users’. This is because name of startup disk doesn’t return a path itself, just a string.===

…which returns:

nameOfMyDisk:Users:rototheg:Documents:testScript:

Another problem is how you’re using ‘thePath’.
When you say ‘every item in thePath’, what AS thinks you want is the number of items in whatever is contained in thePath the variable. So, its result ends up counting the number of text items in the string thePath, which is 45 in my example case. As in, 45 characters in

myHDname:Users:rototheg:Documents:testScript:

What you want is every item in the folder at the location described by thePath. Like this:

set theItems_count to count of items in folder thePath

Your current error lies in the way you’re using and wording the repeat loop.

repeat with i from 1 to theCount

In this line, you’re setting up the repeat from 1 to however many items are in thePath.
So, the repeat just goes 1, 2, 3… and so on. That, in itself is fine, BUT it doesn’t end up giving you what you want because this line…

set theFileName to item i in folder thePath

… just isn’t valid. What you’re saying with the line above is:

set theFileName to item 2 in folder thePath

  • The Finder doesn’t know what “item 2 in folder thePath” means, because it doesn’t keep track of files that way. That line is syntactically invalid.

So, a wordier, but more valid version of your script (what I think you were after in the first place) would be:

tell application "Finder"
	set thePath to (startup disk as text) & "Users:rototheg:Documents:testScript:"
	set theList to every item in folder thePath
	repeat with i from 1 to count of items in theList
		set theFile to item i of theList
		set theFileName to the name of theFile as text
		-- Do more stuff here.
	end repeat
end tell