Sunday, December 17, 2017

#1 2005-10-13 02:40:50 pm

julifos
Administrator
From:: Malasaña, Madrid
Registered: 2002-11-20
Posts: 2014

Can I create a variable on-the-fly, given a random string?

Sure! But it can carry unexpected results in complex structures.

AppleScript has not a specific built-in function to create variables on-the-fly and attach them to the global running environment. But you can use the following trick:

Applescript:

run script "set my newVar to \"test\""

However, variables created this way are only accesible from "outside" only when the "run script" statement is running. When you invoke "run script", a new script object is created (initializated) and their variables, handlers, etc., are killed when it is executed the last statement.

Applescript:

script Harry
   to userVar(parameterFromOutside)
       display dialog "My name is " & my name & return & return & "I can read the property \"accessAtRunTime\": " & my accessAtRunTime
       display dialog "My name is " & my name & return & return & "I can read the local \"x\": " & my x
       return parameterFromOutside & " accessed from outside"
   end userVar
end script

run script "property name : \"Joe\"
property accessAtRunTime : \"something\"
property parent : my Harry
--> this will make Harry inherit these properties

set x to \"kaka\" --> dummy variable
set y to my userVar(\"I was\")
display dialog \"My name is \" & my name & \" and I retrieved this from \" & name of my parent & \": \" & return & return & \"\\\"
\" & y & \"\\\"\""

This script may work when run from an applet, and will fail from the Script Editor. As you see, inheritance and parental is very tricky and variates depending on the "run" environment (script editor, script menu, AS-Studio, applet...).

The best approach is that you make your own tests and see if it fits to your needs.

Some time ago, under OS 9, we had a cool scripting addition called Akua Sweets with a command called "universal", which was able to create system-wide persistent variables, available to all scripts while such variable was not expressely removed via the own "universal" command or after a restart.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)