Re: Record Access osax
Re: Record Access osax
- Subject: Re: Record Access osax
- From: Paul Berkowitz <email@hidden>
- Date: Mon, 10 May 2004 18:19:48 -0700
Actually, you should encase the items from list 2 in quotation marks
(meaning escaped quotes if already text) if using this technique. The only
reason you didn't hit an error here was because the second list consisted of
numbers, which got coerced o strings, and then became numbers again under
'run script'.
I.e.
set list1 to {"a", "b", "c"}
set list2 to {"x", "y", "z"}
set cmd to "{"
repeat with i from 1 to (count list1)
set cmd to cmd & item i of list1 & ":" & item i of list2
if i < (count list1) then set cmd to cmd & ", "
end repeat
set cmd to cmd & "}"
run script cmd
--> ERROR: The variable x is not defined.
Instead you need to do it this way:
set list1 to {"a", "b", "c"}
set list2 to {"x", "y", "z"}
set cmd to "{"
repeat with i from 1 to (count list1)
set l2item to item i of list2
if class of l2item is string then
set l2item to "\"" & l2item & "\""
else if class of l2item is Unicode text then
set l2item to ("\"" as Unicode text) & l2item & "\""
end if
set cmd to cmd & item i of list1 & ":" & l2item
if i < (count list1) then set cmd to cmd & ", "
end repeat
set cmd to cmd & "}"
run script cmd
--> {a:"x", b:"y", c:"z"}
It still works with numbers, which coerce OK to string in the concatenation
line, then back to numbers in 'run script'. It won't work with application
objects at all, of course, and would flatten lists.
--
Paul Berkowitz
>
From: Graff <email@hidden>
>
Date: Mon, 10 May 2004 20:32:23 -0400
>
To: Applescript Users <email@hidden>
>
Subject: Re: Record Access osax
>
>
Hmm, that's a good idea. Build it up as a string and then treat the
>
string as a script. Works for me!
>
>
- Ken
>
>
On May 10, 2004, at 7:01 PM, Andrew Oliver wrote:
>
>
> Oops, you're right, I missed the 'record' part.
>
>
>
> How about:
>
>
>
> set list1 to {"a", "b", "c"}
>
> set list2 to {1, 2, 3}
>
>
>
> set cmd to "{"
>
> repeat with i from 1 to (count list1)
>
> set cmd to cmd & item i of list1 & ":" & item i of list2
>
> if i < (count list1) then set cmd to cmd & ", "
>
> end repeat
>
> set cmd to cmd & "}"
>
> run script cmd
>
> --> {a:1, b:2, c:3}
>
>
>
> Additional error checking is probably needed for production purposes
>
> to make
>
> sure the number of items in each list matches, and potentially to deal
>
> with
>
> other object types (what if list1 contains an integer, or list2 is a
>
> list of
>
> lists?
>
_______________________________________________
>
applescript-users mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/applescript-users
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.