• 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
Re: Convert Text to Variable Name
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Convert Text to Variable Name


  • Subject: Re: Convert Text to Variable Name
  • From: David Jacopille <email@hidden>
  • Date: Sat, 15 Jan 2011 01:32:42 -0500


On Jan 14, 2011, at 7:57 PM, Mark J. Reed wrote:

The principal problem is that these script objects can't access
variables from the surrounding context.  If they could, then this
would work:

set myVar to 1
set varName to "myVar"
set varValue to (run script varName)

But instead you get this:

55:73: execution error: The variable myVar is not defined. (-2753)


Mark is right - scope is the problem.  myVar is outside the scope of the script inside the "run script".

The solution is to pack BOTH the variable name and value together, and then evaluate them together inside the scope of the "run script".

This can be done in several ways.  Here the first line is effectively combining line 1 and 2 from Mark's script into string that holds a record which holds both the variable name and value as a string.  Scope is not an issue because both parts of myVar are in the string.

set myRecordAsString to "{myVar:1}"

eval(myRecordAsString) of me

on eval(superString)
set myRecord to run script superString
return myVar of myRecord
end eval


Now that probably isn't exactly what you wanted because "myVar" is never a string by itself that can literally be converted into a variable name.  Here's something that looks a lot closer:

property allMyVariables : "{myVar:1, yourVar:8, myHome:\"" & (path to home folder) & "\"}"
on evalVar(variableName)
return run script "return " & variableName & " of " & allMyVariables
end evalVar

display dialog evalVar("myVar") -- displays 1
display dialog evalVar("myHome") -- displays the path to your home folder

evalVar converts text into real variable name, retrieves that variable's value, and can assign (or display) the value in one line, seemingly without scope problems.  The scope problem are there, and are solved in the same way. We've just swept them under the carpet a bit further.

Dave





 _______________________________________________
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

References: 
 >Convert Text to Variable Name (From: Christopher Stone <email@hidden>)
 >Re: Convert Text to Variable Name (From: AndrĂ© Renault <email@hidden>)
 >Re: Convert Text to Variable Name (From: Christopher Stone <email@hidden>)
 >Re: Convert Text to Variable Name (From: Stan Cleveland <email@hidden>)
 >Re: Convert Text to Variable Name (From: Christopher Stone <email@hidden>)
 >Re: Convert Text to Variable Name (From: Matthew Smith <email@hidden>)
 >Re: Convert Text to Variable Name (From: "Mark J. Reed" <email@hidden>)

  • Prev by Date: Re: Convert Text to Variable Name
  • Next by Date: Re: Convert Text to Variable Name
  • Previous by thread: Re: Convert Text to Variable Name
  • Next by thread: Re: Convert Text to Variable Name
  • Index(es):
    • Date
    • Thread