every item in entire contents problem

Hi,
I’m experiencing problems when I try to get “every item in entire contents” of a folder. If the folder I choose contains only 1 item then applescript doesn’t seem to recognize it as an item, I’ve checked the Event Log and I can see AS referencing the file but when I count the items in the list it says there’re 0 items. If I duplicate the same item in the folder and run the script again it now knows there’re 2 items in the folder. Can anybody please explain what I’m doing wrong?
Thanks,
Nik

set original_element to choose folder

tell application "Finder"
	set contents_list to every item in entire contents of original_element
	display dialog (count of items of contents_list)
end tell

Model: MacBook Pro
AppleScript: 2.1.1
Browser: Safari 525.18
Operating System: Mac OS X (10.4)

Hi Nik,

if there is only one item in the folder, the result is the reference to the item (not a list!)
if there is more than one item in the folder, the result is a list containing the references to the items
if the folder is empty, an error occurs


set original_element to choose folder

tell application "Finder"
	try
		set contents_list to every item in entire contents of original_element
		if class of contents_list is list then
			display dialog (count of items of contents_list)
		else
			display dialog "1"
		end if
	on error
		display dialog "0"
	end try
end tell

Hi Stefan,

Many thanks for you clear and concise response. I have adjusted my code to now include this

set original_element to choose folder

tell application "Finder"
	if (every item of original_element) is not {} then
		try
			set contents_list to every item in entire contents of original_element as alias list
		on error
			set contents_list to {first item of entire contents of original_element as alias}
		end try
	end if
end tell

Hopefully this should cover every option: Empty Folder, more than 1 item or just 1 item

Nik

Hi, Nik.

Since you want everything in the entire contents, as an alias list, may I suggest this form?

set original_element to choose folder

tell application "Finder"
	try
		set contents_list to entire contents of original_element as alias list
	on error
		set contents_list to {entire contents of original_element as alias}
	end try
end tell
  1. It coerces the ‘entire contents’ directly, so there’s no error getting ‘every item’ when there aren’t any.
  2. It only resolves the ‘entire contents’ once ” or twice when there’s only one item, which won’t take long anyway.

Hi Nigel,

Thanks for your post, I’ll definitely use your code in my script, I do look like my scripts to be as efficient as possible!!

Nik

Hi there,

Just a quickie…

I’ve used this to get all the files in a folder:-

set theFolder to choose folder

tell application "Finder" to set FileList to (files of entire contents of theFolder whose kind is "QuarkXPress Project File" or kind is "InDesign® CS2 Document") as alias list

Obviously it only looks for InDesign or Quark files.

Is there a big difference when you ask for ‘files of entire contents of theFolder’ instead of ‘entire contents of original_element’, the ‘files of’ bit being the difference?

Just wondered :confused:

Regards,

Nick

Hi, Nick.

files of entire contents specifically means the files, not the folders or anything else, in the entire hierarchy of a container. entire contents by itself encompasses everything within that hierarchy apart from the container itself.

Nik’s script specified items of entire contents, which gives essentially the same result as entire contents by itself, since an item is any kind of Finder disk element. But when the container’s completely empty, entire contents by itself returns an empty list, whereas items of entire contents errors, presumably because the Finder’s trying to get the non-existent items. The same would happen with files of entire contents if the container was empty or contained only folders. It’s a peculiarity of entire contents in the Finder ” possibly a bug.

However, your example’s a filter reference. This has to specify an element type, even if it’s only item. In this case, the Finder thoughtfully returns an empty list if there are no matching elements of the required type.

However (again), your example will error if there’s only one file whose kind matches one of your strings. This is because of a long-standing issue with the Finder’s as alias list coercion, which errors if there’s only one item to coerce. There’s no problem with zero items or more than one. The try block structure used above to cope with this is virtually a tradition by now! :slight_smile:

Hi Nigel,

this bug has been fixed in Leopard

D’oh!

Thanks for the explanation Nigel, bit of a silly question. :expressionless:
I’d not realised there was, or should that be used to be, a bug. I’ve probably come across the bug but not realised and worked round it.

Regards,

Nick

Thanks for the information, Stefan. Life will never seem the same again! :wink: But of course we’re still stuck with the try syntax until everyone’s using Leopard or later ” even me! :slight_smile: