Explain "as alias" please.

Hi, can someone explain the “as alias” bit of code? I wrote this simple code but why does it only work when the “as alias” bit is used?

This does NOT work; returns something like "can’t set " whatever “to 1”.

tell application “Finder”
set myFile to selection
set label index of myFile to 1
end tell

But it works when you add “as alias”, as in

tell application “Finder”
set myFile to selection as alias
set label index of myFile to 1
end tell

I just stumbled upon this and it made it work but what does it mean?


Hi, clorox.

An alias is a way of referring to a file, folder, or volume in a script. It has the unique property that if the item is moved after the alias is set, the alias will keep track of it. This isn’t quite the same as an “alias” in the Finder’s user interface, which is known in Finder scripting as an ‘alias file’.

The expression ‘as alias’ is a “coercion”. It produces an alias version of some other means of referring to a file, folder, or volume.

The ‘selection’ in Finder scripting is a list of all the items that are selected. If only one item is selected, then the list will contain just one item ” in the form of a Finder reference. So the list will look something like this:

Your first script tries to set the label index of the list, whereas you really want to set the label index of the file in it. A single-item list can be coerced to anything to which the item in that list can be coerced. So by coercing the selection to alias in your second script, you get an alias version of the single file in the selection. (This won’t work if more than one item is selected.) Since the Finder understands aliases as well as its own references, the second script works.

Another, possibly better way would be to get the item directly:

tell application "Finder"
	set theSelection to selection
	set myFile to item 1 of theSelection
	set label index of myFile to 1
end tell

This is good when more than one item is (or may be) selected, as you can then work through the list, doing whatever’s necessary to the enclosed items.

PS. I see this is my 500th post to the BBS. I hope it’s helpful. :rolleyes:

Wow - quality and quantity! I’ve cracked open a can of beer here to celebrate the occasion, Mr G. :cool:


That was the EXACT answer I was looking for and explained extremely well. Thank You. So… What do you know about the Theory of Relativily? :wink:



Nigel, please go here

Pick out something that interests you, and then contact me. :cool:

It’s not much of course, and I wish I could offer more. It’s only a fractional small token of just how much I, as well as everyone here appreciates your help, and supporting this forum.

Thanks so much for supporting MacScripter Nigel, your participation on this BBS is appreciated more than you will ever know.

Sincerely -

Thanks, kai. I thought I might get a telegram from the Queen today, but apparently not… :confused:

Stop press: But wait! A message of appreciation from Ray! Even better! :slight_smile:

The theorist, the year the first part was published, and, of course, “the equation”. :slight_smile:

Apparently, if you go up in a fast lift, and observe a train going past with a man rolling a ball along the corridor, you’ll bend a light beam round your head and come down again several years younger than your twin. Fortunately I don’t have one, but I’ve warned my nieces to be careful…

:lol::lol::lol::lol: I wish that I hadn’t been taking a drink when I read that. (Now, what did I do with that screen cleaner…) :wink: