Re: Basic script blues ;-)
Re: Basic script blues ;-)
- Subject: Re: Basic script blues ;-)
- From: Neil Faiman <email@hidden>
- Date: Sun, 27 Mar 2005 19:34:32 -0500
On Mar 27, 2005, at 4:59 PM, kai wrote:
On Sun, 27 Mar 2005 21:38:18 +0100, I wrote:
The '&' symbol denotes a concatenation operator - which can only
operate on text (plain text, Unicode text, styled text, etc.) - it
can't combine objects of different classes without some form of
implicit or explicit coercion to text first.
Just to be clear about this, I was referring to concatenation in the
context of the question. Obviously, other classes can be concatenated
- and the results will depend on the rules of concatenation outlined
in the ASLG.
There are two concatenation operators in AppleScript (both written with
the ampersand): the string concatenation operator and the list
concatenation operator. In the expression
X & Y
(1) if X is a string (or a variable whose value is a string), then Y
will be coerced to a string, and the result of the expression will be
the string which is the concatenation of the two strings. (2) If X is
anything else, then both X and Y will be coerced to lists, and the
result of the expression will be the list which is the concatenation of
the two lists. This will always work, because anything at all can be
coerced to a list -- the result of the coercion is just the singleton
list containing the thing that was coerced.
So in Bernard's script 2, where Folder1 was folder("IBM
01:Users:parents:Desktop:Foo"), the expression
(Folder1 & ":Folder2")
Folder1 is not a string, so rule 2 is applied: Folder1 is coerced to
{folder("IBM 01:Users:parents:Desktop:Foo")}, ":Folder2" is coerced to
{":Folder2"}, and then the two lists are concatenated to yield the list
{ folder("IBM 01:Users:parents:Desktop:Foo"), ":Folder2" }.
Kai's suggested ("" & Folder1 & ":Folder2") parses as (("" & Folder1) &
":Folder2"). Since the first operand of ("" & Folder1) is a string,
rule 1 is applied, the second argument is coerced to the string "IBM
01:Users:parents:Desktop:Foo", and the two strings are concatenated to
yield the string "IBM 01:Users:parents:Desktop:Foo". This is the first
argument of the second & operator, so rule 1 applies again, and we get
the result "IBM 01:Users:parents:Desktop:Foo:Folder2".
The important asymmetry is that
non-string-thing & string-thing
always yields a list, while
string-thing & non-string-thing
either yields a string or gives an error, if non-string-thing cannot be
coerced to a string.
By the way, there is an alternative solution to Bernard's original
problem. Instead of reformulating the concatenation to make it yield a
string, you can do away with it completely. Since Folder1 has been
assigned a Finder folder object, change
if not (exists folder (Folder1 & ":Folder2")) then
make new folder at folder Folder1 with properties {name:"Folder2"}
to
if not (exists folder "Folder2" of Folder1)
make new folder at Folder1 with properties {name:"Folder2"}
Regards,
Neil Faiman
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden