0

Let's say I have a script:

set hi to 7
run script "display dialog hi"

When I run it, it says that hi is undefined.

Is there a way to make hi accessible from the run script command? If not, what else can I do? The real application for this is to change a variable name. What I do now is:

    repeat (random number from 0 to 5) times
    set rand2 to (random number from 0 to 7) + 97
    set rand to (random number from 0 to 7) + 97
    set switch to rand2
    run script ("set " & (ASCII character rand) & "1 to " & (ASCII character rand2) & "1")
    run script ("set " & (ASCII character rand2) & "1 to " & (ASCII character switch) & "1")
    run script ("set " & (ASCII character rand) & "2 to " & (ASCII character rand2) & "2")
    run script ("set " & (ASCII character rand2) & "2 to " & (ASCII character switch) & "2")
end repeat

But that doesn't work. Is these I workaround for this?

Thanks

1 Answer 1

1

For the variables to persist in the same scope, you have to run your whole variable switcharoos in the same run script command.

This demonstrates how to add variable to the text of the run script. It would return a list of results from the variables. However, this/your script doesn't work because you're assigning rand+1 to rand2+1, before the variable rand2+1 has been assigned. I do not know what it is you're trying to accomplish, so can't fix more of your script. But, this answers your specific original question, syntax for adding variables into the text of a run script string.

repeat (random number from 0 to 5) times
    set rand2 to (ASCII character ((random number from 0 to 7) + 97))
    set rand to (ASCII character ((random number from 0 to 7) + 97))
    set switch to rand2

    set scriptText to ("set " & rand & "1 to " & rand2 & "1" & return & ¬
        "set " & rand2 & "1 to " & switch & "1" & return & ¬
        "set " & rand & "2 to " & rand2 & "2" & return & ¬
        "set " & rand2 & "2 to " & switch & "2" & return & ¬
        "return {" & rand & "1, " & rand2 & "1, " & rand & "2, " & rand2 & "2}")
    set theResult to run script scriptText
end repeat
7
  • But that doesn't allow me to access variables outside run script, which is what I need (I'm sorry if that wasn't clear). So basically is there a way to set a variable via run script that exists outside of run script?
    – APCoding
    Commented Oct 31, 2015 at 0:44
  • Basically I just want an answer to my first script
    – APCoding
    Commented Oct 31, 2015 at 0:48
  • If you want an actual answer to your main problem, you need to provide more details/context for your script to get help with what you're trying to accomplish. As I said below, your script you provided makes no sense, since you're setting one variable to another variable before it is defined. And, you should ask it in its own topic.
    – jweaks
    Commented Oct 31, 2015 at 2:42
  • I think you're not being clear about setting what variable in front of or behind a "run script". You can set a variable via run script: set x to run script "return 2"
    – jweaks
    Commented Oct 31, 2015 at 2:43
  • Basically I'm picking two random variables that are predefined and switching them around.
    – APCoding
    Commented Nov 1, 2015 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .