Problems with the Repeat

Hi All,

I am currently working on a project that files various pdfs away to a server in different folders, i have a repeat command set up but it doesn’t repeat more than twice, after the second repeat (and sometimes the first) I get this error within applescript “Can’t make name of file 3 of «class cfol» “PDFConvert” of «class cfol» “Desktop” of home directory of application “Finder” into type string.” now when i run the file again straight away it will run through the script fine but then error again when it repeats the next file in line…

This is where the error comes…

set countitems to files of folder "PDFConvert" of folder "Desktop" of home whose name contains "pdf"
	set count1 to (count of countitems)
	repeat with n from 1 to count1
		set fileName to name of file n of folder "PDFConvert" of folder "Desktop" of home as string -- this is when the script errors
-- then the rest of the script
end repeat

i don’t know if i’ve set the repeat up wrong…

my script works as a folder action so when a file is dropped into the script then it will auto run so it shouldn’t be too much of a problem if this isn’t resolved, but it’s frustrating that i can’t get the repeat spot on.

Hi Kyle,

maybe you can repeat throught the PDFs like this:

tell application "Finder" to set PDFDocs to files of folder "PDFConvert" of folder "Desktop" of home whose name ends with ".pdf"

repeat with thisPDF in PDFDocs
	tell application "Finder" to set fileName to name of thisPDF
	-- .... whatever
	display dialog "Processing: " & fileName giving up after 2
	-- ...
end repeat

D.

The references in your script fragment are Finder references. Is this in a Finder tell block? Better would be this:

[See Dominik beat me to it, but this is an alternative]


tell application "Finder"
	set myPath to ((path to desktop folder as text) & "PDFConvert") as alias
	set countitems to files of myPath whose name contains "pdf"
	repeat with n from 1 to count countitems -- you don't use the variable count1 again, so don't need it.
		set fileName to name of file n of myPath -- without the 'as string' fileName is unicode text, with it, it's plain text. Don't know what else you're doing.
	end repeat
end tell

What, exactly, is point of having as string on that line? I’m going to guess that you don’t even need it; However, if you do need to do some kind of coercion, it’s probably choking on the (painfully-common Finder-item-name-acting-as-a-.) reference.

If you’re certain you need something like as string in this instance, then try something like this:

set fileName to (get name of file n of folder "PDFConvert" of folder "Desktop" of home) as string

Dominik, if you’re not repeating inside of a Finder block like Adam, then there’s a good chance that you’ll want to add on as alias list to your script.

Works perfectly now thanks alot!!!

I thought i needed the ‘as string’ because i use various parts of the name later to create more variables before filing the pdf away…

Thanks for your help…

Bruce,

why should this be necessary? I assumed Kyle wants to process the pdfs using other applications than the Finder. So I suggested in my example not to enclose the whole loop in a tell app “Finder” block but using single 'tell’s instead - here an example:

tell application "Finder" to set pdfDocs to (files of folder "Desktop" of folder "PDFConvert" of home whose name ends with ".pdf")
repeat with thisPDF in pdfDocs
	tell application "Finder" to set fileName to name of thisPDF
	display dialog "Processing: " & fileName giving up after 2
	tell application "Acrobat Reader 5.0"
		open thisPDF as alias
	end tell
	-- ...
end repeat

Dominik

That way you don’t have to coerce the value (possibily multiple times) in the repeat loop; If you’re going to use/need an alias, just get that in the first place.

Hi

Sorry to Butt in on your thread kyle.
I just want to ask a quick question to bruce or adam. since this was mentioned in your thread!

“as alias list” excuse my thickness. How? what? Why? where? and When? would i use this. i’m getting to grips with coercion abit but have not yet come across
a script with “alias list” and what it returns.
any chance you guys can shed some light on this to make it abit clearer for us guys who are very much still learning…

cheers

Hello pidge1. Compare the result for these two scripts:

tell application "Finder" to return items of home

tell application "Finder" to return items of home as alias list

The second one returns an alias instead of a document file reference. Note that as alias list is only used with Finder when getting a list of objects (in situations such as the above).

Bruce

Thank you!
Thats very interesting and a lot clearer.

cheers

Occasionally, ‘as alias list’ will fail (haven’t sorted out when exactly), but then ‘as alias as list’ usually works.

Hi, Kyle.

Apart from the observation that the code should be in a Finder tell block, your repeat counter is based on the number of files in the folder whose name contains “pdf”, whereas in the repeat, you’re accessing the nth file of the folder, “pdf” or not. That’s wrong and could cause unexpected results. Also, your description suggests that the “rest of the script” moves the files to other folders. In that case, the nth file of the folder will be removed and (n + 1)th file will become the nth file. When the loop counter is then incremented to (n + 1), it indexes the erstwhile (n + 2)th file (ie. the new (n + 1)th file) instead of the old (n + 1)th. Some files will therefore be missed and the repeat will run out of files to index before it finishes itself. That’s one of the reasons behind Dominik’s reply. You should loop through the items in the list you’ve obtained, not through the numbered files in the folder.

as alias list has always failed when the Finder reference only returns one item. It’s a long-standing issue. With more (or less!) than one, it should work. as alias as list is a double coercion that works when there’s just one item. Neither should be necessary for Kyle’s script unless some other app needs to work with the files.

How did I miss that?!. Thanks for the input Nigel. :slight_smile:

Thanks for the explanation Nigel, and all the input from you guys…

I’ve got a much better understanding of the repeat process…

Thanks again. :smiley: