Whats the difference.....

Hi

Just a quick question to anyone with an idea about this one.
Why would i? and why would i want to? use the word “Length” in my script.

tell application "Finder"
	set x to every folder of desktop
	set y to length of x
	display dialog y
end tell

the above works same as the below script…
Got an idea its used very similar to “count” but can’t use it in all the same places

tell application "Finder"
	set x to every folder of desktop
	set y to count of x
	display dialog y
end tell

this works!

tell application "Finder"
	set x to count every folder of desktop
	display dialog x
end tell

this doesn’t

tell application "Finder"
	set x to length of every folder of desktop
	display dialog x
end tell

Any ideas please let me know!

cheers!

Hello

On an other forum this responce was given to a similar question:

The question was:
why this doesn’t work:
repeat with myMessage in messages of mailbox “myMailbox”
set myYear to year of date received of myMessage
log myYear
end repeat

----- I responded:

It seems that some application’s AppleScript’s implementation is lazy.

This slightly modified code works for me:

	set myYear to year of ( get date received of myMessage)

----- Has responded:
[i]
Right solution, though wrong reason. AppleScript’s ‘implicit get’ behaviour is the culprit here.

The line ‘set myYear to year of date received of myMessage’ doesn’t work because when AppleScript does its implicit get, it sends the entire reference - ‘year of date received of message 1 of mailbox “myMailbox”’ - to Mail. However, scriptable applications only understand references to the properties and elements of application objects; they don’t understand references to properties and elements of AppleScript values (strings, lists, dates, etc.). So Mail gets as far as resolving ‘date received of message 1 of mailbox “myMailbox”’, but because the ‘date received’ property contains an AppleScript value, not an application object, it can’t go any further; hence the error when it tries to resolve the ‘year of’ part of the reference.

You have to force AppleScript to send the right reference (‘date received of message 1 of mailbox “myMailbox”’) to Mail by inserting an explicit ‘get’ command into your code at the appropriate point. Mail wil then return an AppleScript date value, and AppleScript (which does know how to manipulate date values) can obtain the value of its year property.

Has[/i]

And of course this one works:

tell application "Finder"
	set x to length of (get every folder of desktop)
	display dialog x
end tell

Yvan KOENIG (from FRANCE lundi 16 octobre 2006 15:31:40)

Hi Yvan

Thanks for the pretty indepth reply… Woosh!! thats the sound of most of it going over my head…

is the “length” command pretty much the same as the “count” command then?

cheers

pidge1,

I believe that the length works for anything that is a list much like the count but doesn’t work for "length of folder ‘blah’ " because this is a reference to a specific item. So if you set up another variable as a list to the contents of “folder ‘blah’” you could use the length to count it.
Whether this is proper or not, I can’t say. I have seen it used here and there.

PreTech

Hello

As far as I know, COUNT is defined in the Finder’s dictionary while LENGTH is not.

This makes all the difference.

Yvan KOENIG (from FRANCE lundi 16 octobre 2006 20:34:27)

Hi,

‘length’ is an AppleScript property for ‘string’, while ‘count’ is a AppleScript or application command. What I do is use length for text and count for lists. e.g.

length of “xyz”

count {1,2,3}

gl,

Hi Guys

Thanks for all your input its starting to become a lot clearer…

Cheers

Hi.

length is a property that some things have and others don’t. AppleScript lists have a length, namely the number of items in them. The Finder’s folders don’t have a length. In this script:

tell application "Finder"
	set x to length of every folder of desktop
end tell

. you’re asking for the length of each and every folder of the desktop. Since folders don’t have lengths, you get an error. In this version:

tell application "Finder"
	set x every folder of desktop
	set y to length of x
end tell

. setting x to every folder sets x to a list of those folders. The line after that gets the length of the list.

count is a command that’s implemented both in vanilla AppleScript and in the scripting dictionaries of many applications. The Finder is one such application, so you can tell it to count the folders on the desktop. In its basic form, count counts the basic elements of the thing counted:

tell application "Finder" to count (get desktop)
--> The number of items on the desktop.

count myList
--> The number of items in a list.

count myText
--> The number of characters in a text.

But you can also specify what sort of element you want to count:

tell application "Finder" to count folders of desktop
--> The number of folders on the desktop.

count integers of myList
--> The number of integers in a list.

count paragraphs of myText
--> The number of paragraphs in a text.

An alternative syntax for counting specific elements uses each:

tell application "Finder" to count desktop each folder

I personally prefer to use count, because when I tested them many years ago, count was marginally faster than length (where they both worked) and both were a hell of a lot faster than number. But I haven’t compared them recently. :slight_smile:

Hey Nigel

Thanks alot! for the explanation.
Glad i posted on this one now…

Thanks to everyone else aswell…

cheers