0

I'm trying to save three distinct variables that my Applescript application would then be able to access on launch, and if the user wishes to he may manipulate those variables from within the application.

More specifically, I'm working on a script that runs a bunch of equations based on a user's selected latitude and longitude, and outputs the results in a string, including the city's name. If the user wishes to, he may choose a different city and restart the script based on that city.

Choosing a different city, setting the variables, and rerunning the script accordingly is fine. I can just set up a subroutine containing the following code:

set choice to choose from list {"City A","City B","City X"}
if choice is "City A"
    set city to "City A"
    set lat to (latitude of City A)
    set lon to (longitude of City A)
else if choice is "City B"
...

My problem is that I have to initialize these variables when the script starts. I can set a predefined value for these variables, or I can run this subroutine at the beginning of the script, but I can't find a way to save the choice for future runs, so that the result of this choice becomes the default. That is, if the script by default runs these equations for City A, and I pick City B from the list, the next time the script runs, it should run the equations for City B automatically, without any user input.

I tried setting up a second script with exactly three lines of code:

property city : "City A"
property longitude : (longitude of City A)
property latitude : (latitude of City A)

but subsequently realized that it fails for exactly the same problem: Applescripts can't save variables inside themselves between tasks. I can update an external variable once it's loaded into the current script, but that doesn't actually edit the original script.

Another thing I tried was creating a blank file somewhere and rename it according to the user's choice, then testing for the file's name and updating the variables accordingly, but I'd also like to share the application for others to be able to use, and that would require them to also have this file in a specific folder. For some reason, Script Editor doesn't like it when I try getting the file path of a file inside an application package, so unless there's a fix for that, I can't just hide the file inside the application itself.

I've seen here that if you save the file as .applescript you can have it write onto itself, but I'm unsure of how you would go about doing that.

I am aware that there is a thing called Database Events that I could possibly manipulate, but I'm not sure of how I would go about doing that.

The minimum I'm looking for is a way to save variables to access them on launch next time, but if someone could address all of the possibilities I've mentioned above, that would be excellent.

1

Add a property declaration at the start of your script. Changes to that property during the execution of your script will be saved and available the next time you execute.

The value set by a property definition is not reset each time the script is run; instead, it persists until the script is recompiled. [src]

  • As in, property city : "City A"? I tried that, but it doesn't seem to work. After the lines where I edit those values, I restart the script, and it outputs exactly the same thing as it did before. Quitting the script and reopening it changes the name of the city but not the latitude and longitude, even though I manipulate all of them exactly the same way. – DonielF Jul 26 '17 at 0:23
  • Properties work (and have always worked) fine every time I've used them. I'm thinking the issue is more likely to be elsewhere in your code but it's hard to say for sure without seeing it. From the snippets you supply above, I don't think it's doing what you think it should be doing. Specifically, you set the variable city to the string "City A" but then get the lat/long properties of an object City A. Where does this object come from? Maybe you need to ask about your code on SO. – Patrick Wynne Jul 26 '17 at 19:21
  • Not properties of an object. That was poor wording on my part. Those numbers are pre-specified. Pretend that it says set lat to 5 or something to that effect. – DonielF Jul 26 '17 at 19:25

You must log in to answer this question.

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