error after finishing running of script

set x to 1
tell application "Finder"
	set apath to alias "mac 3:B"
	
	
	repeat with aItem in folder apath
		repeat with x from 1 to 9
			set dpath to "mac 3:B:" & x
			set dpathfolder to alias dpath
			if name of aItem ends with "00" & x & ".pdf" then
				move aItem to folder dpath
			end if
		end repeat
	end repeat
end tell

it does what i want but throws me the error after finishing.
Finder got an error: Can’t get item 22 of folder “B” of disk “mac 3”.
–item 22 or any item number after the last item it was supposed to work with
and sometimes even before the last item in which case the work remains unfinished and i have to run the script.
what is this error about?

Hi Lance,

Folder paths always need to end with a colon (“mac 3:B” vs. “mac 3:B:”). I have cleaned up your script a bit, which now looks like this:


-- folder paths always end with a colon!
set apath to "mac 3:B:" as alias

tell application "Finder"
	set pdffiles to every file in apath whose name ends with ".pdf"
	repeat with pdffile in pdffiles
		repeat with i from 1 to 9
			if name of pdffile ends with ("00" & i & ".pdf") then
				set pdffolder to ("mac 3:B:" & i & ":") as alias
				move pdffile to pdffolder
			end if
		end repeat
	end repeat
end tell

thank you, Martin

Hi, Lance.

When you use the ‘repeat with . in .’ repeat form, the loop variable contains an index reference to the to the item in the container (in your case, a folder). So in your repeat .

repeat with aItem in folder apath 
	
end repeat

. aItem has the successive values:

item 1 of folder “B” of disk “mac 3”
item 2 of folder “B” of disk “mac 3”
item 3 of folder “B” of disk “mac 3”
etc.

If item 1 passes the test and gets moved to folder dpath, item 2 becomes the new item 1 in folder apath, item 3 the new item 2, and so on. Next time round the repeat, the reference is to ‘item 2 of folder “B” of disk “mac 3”’ and it’s the new item 2 that gets examined. The original is skipped because it now has a used index number.

The repeat will attempt to reach the original index number of the last item in the folder, but at some point will hit an index that’s higher than the number of items left in the folder. That’s when you get the error you’ve been getting.

Martin’s clean-up of your script gets the items in the folder as an AppleScript list. This is doubly secure because: 1) The list contains name references to the items. 2) The repeat’s index reference now indexes the list, not the folder, and the items aren’t removed from that.

Hi Nigel,

Thank you very much for explaining the important details! I also wanted to do this, but unfortunately my English was just not good enough :wink:

thanks Nigel for giving the explanation.
It will help me avoid making the same mistake when using repeat loop.

Martin, your script looked good when i read it and so i did not bother to check whether it works.
Now when i have done that, i find that your script moves only one file and then throws up the error:

Finder got an error: Can’t get document file “” of folder “B” of disk “mac 3”.

I must state the purpose of my script.
There is a folder B.
B contains pdf files and folders named 1,2,3…14 (i know i just took 9 in my script because i was still working on what to do when it comes to double digits because in that case the file name ends with “0” & x & “.pdf” instead of “00” & x & “.pdf” )
I want to move pdf files of folder B into these folders(1,2,…14) based on the name of the pdf file.

Hi again.

A slightly more efficient approach would be:

  1. Get all the file names and examine them textually, rather than filtering the files by name and then getting the names of the successful files again.

  2. Derive the target folder name directly from each file name rather than using an inner loop to generate the numbers.

set apath to "mac 3:B:"

tell application "Finder"
	tell folder apath
		set fileNames to name of every file
		repeat with fileName in fileNames
			if (fileName ends with ".pdf") then
				try
					set folderName to (text -7 thru -4 of fileName) as integer as text
					move file fileName to folder folderName
				end try
			end if
		end repeat
	end tell
end tell

Thank you, Nigel. I have tested your script. It works fine.
By the way, do you know what the problem was with Martin’s script.

Let’s see. Yes. The inner repeat isn’t stopping after a file with a number less than 9 is moved. It tries to test the name of the file again to see if it has the next number up, but the file’s no longer there, so an error is generated. An ‘exit repeat’ is needed after the ‘move’ line.


-- folder paths always end with a colon!
set apath to "mac 3:B:" as alias

tell application "Finder"
	set pdffiles to every file in apath whose name ends with ".pdf"
	repeat with pdffile in pdffiles
		repeat with i from 1 to 9
			if name of pdffile ends with ("00" & i & ".pdf") then
				set pdffolder to ("mac 3:B:" & i & ":") as alias
				move pdffile to pdffolder
				exit repeat
			end if
		end repeat
	end repeat
end tell

This script needs further modification to handle numbers greater than 9.