RE: "a reference to"
RE: "a reference to"
- Subject: RE: "a reference to"
- From: Scott Babcock <email@hidden>
- Date: Thu, 17 Jan 2008 10:57:26 -0800
- Acceptlanguage: en-US
- Thread-topic: "a reference to"
Sorry... I failed to explain the purpose of the 'reference' coercion. This code is derived from "AppleScript: The Definitive Guide" by Matt Nueburg, in the section titled "Identifying References".
AppleScript provides no direct way to determine that it's handed you a reference instead of a local object. One of the workarounds for this deficiency is to attempt to coerce the item in question to 'reference'. Since non-reference items cannot be coerced to 'reference', the absence of an error gives indirect evidence that an item is a reference.
As you noted, I didn't save the result of the coercion in any way that the script could use; I merely logged the error message if it failed. I could have set an isRef variable to 'true' or 'false', but I didn't need to since the behavior of the script didn't vary based the inferred type of thisItem.
The thing that puzzles me is that the behavior of the script indicates that thisItem must be a reference, yet the 'reference' coercion still fails. I guess this is what Matt meant when he stated that techniques for identifying references "seem *mostly* to work."
set theList to {{foo:"foo"}}
repeat with itemRef in theList
set recordRef to (contents of itemRef)
set foo of recordRef to "ref"
set localItem to (contents of recordRef)
set foo of localItem to "local"
return {theList, itemRef, recordRef, localItem}
end repeat
--> {{{foo:"ref"}}, {item 1 of {{foo:"ref"}}, {foo:"ref"}, {foo:"local"}}
This result clearly shows that recordRef is a reference to the original record in theList, whereas localItem is merely a local copy.
-----Original Message-----
Date: Wed, 16 Jan 2008 22:04:57 -0500
From: deivy petrescu <email@hidden>
Subject: Re: "a reference to"
To: applescript <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
On Jan 16, 2008, at 17:50, Scott Babcock wrote:
> Here's a bit of code that still mystifies me, although I can get it
> to do what I want:
>
> set foo to {{foo:"foo1"}, {foo:"foo2"}}
> repeat with itemRef in foo
> try
> itemRef as reference
> on error errmsg
> log errmsg
> end try
>
> -- de-reference list-item reference
> set thisItem to (contents of itemRef)
> try
> thisItem as reference
> on error errmsg
> log errmsg
> end try
>
> -- set value of record property
> set fooVal to (foo of thisItem)
> set foo of thisItem to "bar" & (text -1 thru -1 of fooVal)
> end repeat
>
> get foo
>
> If you run this script, you'll see that the final result shows that
> the values of the [foo] properties of the items within the original
> list have been altered. This means that [thisItem] must contain a
> reference to the original record instead of being a local copy.
> However, the event log shows that the 'reference' coercion fails:
>
> (*Can't make {foo:"foo1"} into type reference.*)
>
> Further, if you de-reference itemRef twice (i.e. - contents of
> contents of itemRef), the contents of the original list of records
> remains unaltered. This double de-reference causes the code within
> the 'repeat' loop to operate on local copies.
Scott, I don't believe you are doing what you think you are doing.
itemRef as reference does not change it.
You can check that.
Try this;
set foo to {{foo:"foo1"}, {foo:"foo2"}}
repeat with itemRef in foo
set itemref1 to itemRef as reference
set itemref2 to (a reference to contents of itemRef)
return itemref1 = itemRef
end
---> true
now this:
set foo to {{foo:"foo1"}, {foo:"foo2"}}
repeat with itemRef in foo
try
set itemref1 to itemRef as reference
set itemref2 to (a reference to contents of itemRef)
on error errmsg
log errmsg
end try
return {itemRef, itemref1, itemref2}
end
-->{item 1 of {{foo:"foo1"}, {foo:"foo2"}}, item 1 of {{foo:"foo1"},
{foo:"foo2"}},contents of item 1 of {{foo:"foo1"}, {foo:"foo2"}}}
So, itemref2 is the one that is really a reference.
------------------------------
_______________________________________________
AppleScript-Users mailing list
email@hidden
http://lists.apple.com/mailman/listinfo/applescript-users
End of AppleScript-Users Digest, Vol 5, Issue 34
************************************************
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden