How might I save a variable between Applescript runs

applescript

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.

Best Answer

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]