Help with list query please

Hi There,

I’ve posted on this before but I think I’m still getting my wires crossed somewhere.

I’ve tried to simplify my query and have written the script below to try and illustrate my query.

If I take two files, named “1234_1.txt” and “1234_2.txt”, and drop them on this script app I get the answer “1234_1.txt1234_2.txt” in the first dialog box.
The second dialog box has the value 20 which is the number of chars in ‘donelist’.

What I’m trying to do is get the number of filenames in the donelist i.e. 2, not total number of chars in donelist.

Please can someone help me, I’ve been reading through a number of posts trying to gain a better understanding but as yet haven’t had any luck.

Here’s the script:-

on open (someitems)
	
	set donelist to {}
	
	repeat with theitem in someitems
		set filename to name of (get info for theitem without size)
		set donelist to (donelist & filename) as text
	end repeat
	
	set numberOfProcessed to (length of donelist)
	
	display dialog donelist
	display dialog numberOfProcessed
	
end open

Thanks in advance,

Nick

Model: G5
Browser: Safari 312.3
Operating System: Mac OS X (10.4)

Nick:

You’re coercing the list as text, then requesting the length (of the text string you created), which returns the number of characters.

Getting the number of items is as simple as this:


on open (someItems)
		display dialog "" & (count someItems) -- Or push (count someItems) into a variable, or whatever.
end open

The Open handler returns a list so you can just count the list as is. If you want to get info for the individual items, that’s when you run them through the repeat loop.

Jim Neumann
BLUEFROG

Hi there,

Thanks for the reply. I’m not sure if that is going to help me.

I’ve created a script that processes files dropped onto it. The script enables certain properties on the file.
What I’m wanting to do is create 2 lists, a processed list and an error list, both containing a list of filenames.
I then want to find out how many filenames are in each list.

Hope that helps to clarify it a little further.

Thanks again for your help,

Nick

P.S. Just remembered, the reason I’d added ‘as text’ is that I get the error ‘Can’t make {“1234_1.txt”,“1234_2.txt”} into type string.’ if i don’t put that in.

Nick:
Here’s a very simple example which just checks if the kind of file is a script file.


on open (theFiles)
	set errorList to {} -- Empty list to contain names of errored files
	set goodList to {} -- Empty list to contain names of processed files
	tell application "Finder"
		activate
		repeat with thisFile in theFiles
			if kind of thisFile ≠ "Script" then
				copy (name of thisFile) to end of errorList -- Push the name into the error list
			else
				copy (name of thisFile) to end of goodList -- Push the name into the good list
			end if
		end repeat
	end tell
	display dialog "Processed files: " & (count goodList) & return & "Errored Files: " & (count errorList)
end open

Jim
BLUEFROG

Jim, using copy is a bit slower (as far as milliseconds go) than using set. (See the end of this post.)

on open theFiles
	set errorList to {} -- Empty list to contain names of errored files
	set goodList to {} -- Empty list to contain names of processed files
	
	tell application "Finder"
		launch
		repeat with thisFile in theFiles
			if kind of thisFile ≠ "Script" then
				set errorList's end to (name of thisFile) -- Push the name into the error list
			else
				set goodList's end to (name of thisFile) -- Push the name into the good list
			end if
		end repeat
	end tell
	
	display dialog "Processed files: " & (count goodList) & return & "Errored Files: " & (count errorList)
end open

Thanks for the nudge, Bruce. You know it’s odd… I rarley used the copy method in these instances but, for whatever reason today, it was the first one that came to mind.

Good explanation at the end of the post on the appropriateness of the syntax (avoiding data sharing, etc.)

Cheers,
Jim
BLUEFROG

Your best bet, Nick, is to retain the original list as is, but to just modify it when you wish to display it in a dialog ” perhaps something like this:


on text_list from l around s
	set {d, text item delimiters} to {text item delimiters, s}
	set {l, text item delimiters} to {l as text, d}
	l
end text_list

set my_list to {"1234_1.txt", "1234_2.txt", "1234_3.txt", "1234_4.txt"}

display dialog "The produces a vertical list of the " & (count my_list) & " items:" & return & return & (text_list from my_list around return)
display dialog "The uses a forward slash as a separator:" & return & return & (text_list from my_list around " / ")
display dialog "This simulates an AppleScript list of quoted strings:" & return & return & "{\"" & (text_list from my_list around "\", \"") & "\"}"


Bluefrog, Kai and Bruce,
Thanks for all your help and for the examples, they’ll give me plenty to play around with. :slight_smile:
It’s good having a few examples to work forward and learn from.

I’ll let you know how I get on, once again a big thankyou to you all.

Cheers!

Nick