• 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: Axel Luttgens <email@hidden>
  • Date: Sat, 15 Jan 2011 13:43:37 +0100

Le 15 janv. 2011 à 01:37, Christopher Stone a écrit :

> [...]
>
> Well I thought I was reasonably clear in my first post but perhaps not.  What I wanted was for you to return the value of varName starting with the text value "varName".
>
> set theVariable to path to home folder
> set anotherVariable to "theVariable"
>
> Now I want to return the value of theVariable starting with anotherVariable if it's possible.
>
> The outcome on my machine should be: alias "Odin:Users:stone:"

Hello Christopher,

You already got many replies and useful suggestions, and consequently chose to go another path.

Now, above problem still remains open, and is an interesting/fun one; I mean, it's always amusing to play with AppleScript, provided one isn't too focused on efficiency. ;-)

So, for the fun, here is a way relying on the edge case where global variables really have something special:


	-- Not always needed, but it's anyway a good idea to explicitly
	-- list, and comment about, globals used in a script: they quickly
	-- tend to become very invasive thanks to AppleScript's scoping
	-- rules (note that the same could be said about properties
	-- declared in the top-level script).
	global var1 -- some comments about var1's role
	global var2 -- some comments about var2's role

	on AccessVarByName(VarName)
		local Accessor
		-- Build a script object on the fly, so as to have an entity
		-- somehow immersed in the execution context, even if
		-- not having the top-level script object in its inheritance
		-- path.
		set Accessor to run script "
			on run
				script S
					on FetchIt()
						global " & VarName & "
						return " & VarName & "
					end FetchIt
				end script
			end run
		"
		-- Ask that script object to access the global variable.
		tell Accessor to FetchIt()
	end AccessVarByName

	set var1 to path to home folder
	set var2 to "Hello World!"

	display dialog (AccessVarByName("var1") as text)
	display dialog AccessVarByName("var2")


HTH,
Axel

 _______________________________________________
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

  • Follow-Ups:
    • Re: Convert Text to Variable Name
      • From: Christopher Stone <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>)

  • Prev by Date: Re: Convert Text to Variable Name
  • Next by Date: Re: What is class furl?
  • Previous by thread: Re: Convert Text to Variable Name
  • Next by thread: Re: Convert Text to Variable Name
  • Index(es):
    • Date
    • Thread