Storing Variables outside of Applescript

Discussion in 'Mac Programming' started by DoubleJGames, Feb 3, 2014.

  1. DoubleJGames, Feb 3, 2014
    Last edited: Feb 3, 2014

    DoubleJGames macrumors newbie

    Joined:
    Sep 30, 2013
    #1
    I am currently adding voice recognition to spotify. I have added a way to play songs but this has to be changed in the script itself. I need a way to store a variable outside of applescript itself as the script closes after each command. (Im using speakable items)

    Edit: I am using a .txt file atm and its working but i would rather a neater way of doing it.

    Thanks

    DoubleJGames
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Try an AppleScript property:
    https://developer.apple.com/library...criptLangGuide/conceptual/ASLR_variables.html
    The value set by a property definition is not reset each time the script is run; instead, it persists until the script is recompiled.

    You cannot declare a property in a handler but a handler can access a property defined in its containing script object.

    A global variable should also work.

    Other approaches are possible, but a property or global variable should provide persistence, which is what you seem to be asking for, having posted no code.
     
  3. Mark FX, Feb 7, 2014
    Last edited: Feb 7, 2014

    Mark FX macrumors regular

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #3
    As chown33 as correctly stated, use a property at the top level of your script, and make sure it's declared outside of any handlers, including any explicit on run handler.
    The way you write a property is shown below.

    Code:
    property propertyName : missing value -- A property with no or nil value.
    property secondProperty : "Some text" as text -- A property with an intitial text value.
    property thirdProperty : true as boolean -- A property with an initial boolean value.
    property fourthProperty : 2.0 as real -- A property with an initial real value.
    property lastProperty : 1 as integer -- A property with an intitial integer value.
    
    --Other code and handlers written at this point.
    
    In the above examples, the values will be retained between script executions, and can also be modified from anywhere in your script.
    In the first property example, missing value is Applescript's version of nil or NULL, so you can also assign a value to it from anywhere within your script.

    One word of caution, if your explicitly using scripting additions, you cant use the as boolean keywords as it will raise an error in OSX 10.9 Mavericks, like in the example below.

    Code:
    use scripting additions
    property thirdProperty : true as boolean -- Will raise an ERROR so write like below.
    
    property thirdProperty : true -- This is OK
    
    It's a bug that has been reported to Apple.

    Hope this helps.

    Regards Mark
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Or defaults write?

    Hi,

    You could also maybe use something like the defaults command, e.g.

    To write "bar" to the user defaults unde the name "foo"
    do shell script "defaults write com.myname.myapp foo bar"


    To read it back in
    set myValue to do shell script "defaults read com.myname.myapp foo"

    …just another option!
     

Share This Page