• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Building applescript records with lists, accessing applescript records with strings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Building applescript records with lists, accessing applescript records with strings


  • Subject: Building applescript records with lists, accessing applescript records with strings
  • From: Malcolm Fitzgerald <email@hidden>
  • Date: Fri, 4 Feb 2005 13:44:08 +1100

Two of the FAQs on this list are, "How do I build an applescript record from a list?" and "How do I coerce a string to a variable?".

The most frequent answer is "AppleScript is not Perl". However, you can set a string to a variable using do script and you can build a record using do script. Do script happily sends strings to the compiler and returns variables and records.

(* coerce string to variable *)
set theVar to do script ("a reference to " & "x")
--> x (a variable)

(* build an applescript record from strings *)
set theStrings to {"x", "y", "z"}
set theValues to {3, 2, 1}
set s to {}
repeat with i from 1 to (count theStrings)
set s to s & (do script ("{" & item i of theStrings & ":" & item i of theValues & "}"))
end repeat
--> {x:3, y:2, z:1} (a record)


The problem not that Applescript cannot or doesn't do it. It plainly does. The problem that AppleScript doesn't make it easy to do.

The most obvious problem is that you cannot access these variables or records from outside the "do script" unless you can ask them for their values. To do that you need to ask using their variables. Catch 22! However, if you're able to pass the query into "do script" at the same time as you pass the data in you are able to get the results you'd expect.

The sub-routines below provide a way to use strings and lists as records. They may appeal to those of you that don't have better ways to do the same thing.

(*
-- build some data
-- it was Jason Bourque's item pricing problem that started this
set theItemPrices to {}
set theItemLabels to {}
repeat with i from 97 to 122
	set end of theItemPrices to i
	set end of theItemLabels to ASCII character i
end repeat
*)

-- build the hash
set theHash to BuildHash(theItemLabels, theItemPrices)
-- select a key
set theKey to item (random number from 1 to 27) of theItemLabels -- a string selected at random
-- example 1
GetHashValue (theKey, theHash)
-- example 2
set priceList to {}
repeat with theVar in theItemLabels
if theVar is in {"j", "m", "t"} then set the end of priceList to GetHashValue(theVar, theHash)
end repeat
get priceList
--
--
on BuildHash (theKeys, theValues)
-- returns a string which the compiler can cast to a record
set s to {}
repeat with i from 1 to (count theKeys)
set end of s to ","
set end of s to item i of theKeys
set end of s to ":"
set end of s to item i of theValues
end repeat
set s to (rest of s) as string
return "{" & s & "}"
end BuildHash


on GetHashValue (theKey, theHash)
	return do script (theKey & " of " & theHash)
end GetHashValue





Malcolm Fitzgerald                       ph: 02 93180877
Database Manager                        fax: 02 93180530
The Australian Society of Authors      www.asauthors.org

_______________________________________________
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


  • Follow-Ups:
    • Re: Building applescript records with lists, accessing applescript records with strings
      • From: Takaaki Naganoya <email@hidden>
  • Prev by Date: Re: applescript batching itself
  • Next by Date: Re: Building applescript records with lists, accessing applescript records with strings
  • Previous by thread: use itunes to reorder tracks in a mysql database
  • Next by thread: Re: Building applescript records with lists, accessing applescript records with strings
  • Index(es):
    • Date
    • Thread