Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DoubleJGames

macrumors newbie
Original poster
Sep 30, 2013
4
0
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
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,750
8,422
A sea of green
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.
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
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
 
Last edited:

superscape

macrumors 6502a
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.