How to make persistent variables for iOS and iPadOS Shortcuts | Hacking Shortcuts

Store data across Shortcut runs with Memory

Shortcuts has proven to be an invaluable source of automation to me. It is in many ways a very powerful app. But it has its limitations (in particular the “automations” that are not actually automatic but that’s another story). One limitation that has cropped up for me multiple times is around persistent variables. What I mean by that is the ability to store data across Shortcut runs.

What I’d like to be able to do is to run a Shortcut on one day, process some data. And the following day, or even later that same day, run the Shortcut again and have the previous data available for use in the new Shortcut run.

Shortcuts doesn’t natively support this functionality – any variables initialised in a run disappear once the Shortcut ends. There are some workarounds to this.

Here is a video demonstration of me using the preferred method (method 3 below):

  1. Toolbox Pro. This app supports a feature called Global Variables and these are available across Shortcut runs. The app is free to download. Global variables are included in the free set of functionality. > This is good but I personally wanted something that wouldn’t require an app that I might not have installed all the time.
  2. Notes. Shortcuts supports appending to notes. I’d judge that a high proportion of iOS users have the stock Notes app installed. So a note can serve as a data repository. Just give a note a unique title and you can reliably access it using the Find Notes Where action. Set the limit to 1 note and assign it to a variable. > The major limitation here is that Shortcuts can only append to a note. Once data is inserted into the note it can’t be deleted by Shortcuts. This means you have to build in a routine to identify the last item that was appended to get the latest data payload. Also as the Shortcut is used over time the note contents will increase. Eventually this will have an impact on the performance of the Shortcut. > The above limitation also applies to storing variables in text files directly by a Shortcut – Shortcuts can only append to text files.
  3. Reminders. Reminders might seem a strange place to store data until you consider the fact that reminder notes can be changed; not just be appended to. So variables can be stored in a master dictionary* as text. To do this we can simply use the Edit Reminder action. > This is my preferred method of storing variables across Shortcut runs because I always have the Reminders app installed and it’s more efficient than the Notes method.

Here’s how I implement variable storage for a Shortcut I use to log electricity and gas meter readings. The Shortcut is called PyLog (because the Shortcut triggers a Python script to store my readings in text files). When adding a new meter reading the Shortcut will ask if it is an electricity / gas reading, and then it will prompt me for the reading. In the prompt is the previous reading, e.g. “Input meter read (previous: 3334)”.

  • In Reminders I’ve created a dedicated list called Reference. This list will only ever have reminders that I use for variable storage so it won’t become unwieldy.
  • In that list I have a reminder called PyLog variables.
  • When I run my Shortcut it gets that reminder by name (filters to first reminder match).
  • Then it gets the content of the notes of that reminder and extracts a dictionary from the note text.
  • One of the dictionary keys is the variable name I’m interested in, so I get the value of that key. E.g. Get value for variable name from dictionary name.
  • The dictionary value itself can be anything – including another dictionary – so the scope for storing variables can extend to cover some complex scenarios.
  • Once the variable is extracted from the reminder (the above processes are very fast btw) I can then do what I need with it – show it in a prompt for input or do a calculation with it etc.
  • Updating the stored variable. To store an updated variable I add the variable back to the master dictionary using the same variable name as its key, e.g. Set variable name to updated variable in master dictionary, then set the reminder notes to the master dictionary.

Generalising this process to expand its usefulness

Repeating the above steps across multiple shortcuts would prove tedious. So I’ve harnessed a useful feature of Shortcuts app to make persistent variables more readily accessible. One Shortcut action is to Run Shortcut. I made a dedicated Shortcut whose sole purpose is to get or add persistent variables by storing them in a reminder note in a reminder list called Reference. The Shortcut is called Memory. Whenever I want to work with a variable that needs to persist across shortcut runs I use the Run Shortcut action and call Memory passing in a dictionary specifying if I am getting the last stored value of the variable or updating its value.
The dictionary has three key-value pairs:

  • “variable name” | a string value.
  • “method” | a string specifying if the variable is being added to memory “add” or being retrieved “get”.
  • “payload” | a string, number or dictionary – the data being stored for this variable.
    If getting a stored variable the payload value is returned. If adding a variable to store the variable payload is added to the master dictionary using variable name as the key and the master dictionary is stored in the reminder note.

Below are screenshots of dictionaries for passing into the Memory Shortcut. Example 1 shows how to get a variable from storage (variable is called Cycle Log). Example 2 shows how to add data to storage. In example 2 the dictionary at the top is what I want to store in the payload; you can see it referenced in the lower dictionary payload field. The lower dictionary is what gets passed into the Memory shortcut to store the data.

Example 1. A Shorcuts dictionary for passing into Memory to get a stored variable.
Example 2. A Shorcuts dictionary for passing into Memory to add a stored variable.















This Shortcut has a lot of potential for my workflows. This week I integrated Memory into my Fuel Log Shortcut to track fuel cost changes between fill ups.

If you’d like to download Memory to your Shortcuts library and start using persistent variables you can do so here. The shortcut requires a Reminder list called Reference. Create that list before use.

Notes

* A dictionary is a format for laying out data in key-value pairs. The key is the unique name used to access the value. This is an example of a dictionary: {“key_1” : “value_1”, “key_2” : “value_2”}. To get the second value from that dictionary I would use Get value for “key_2” from dictionary. Dictionaries can be stored as text and later retrieved from text. So they are a useful way of storing data in text only fields like the notes of a reminder!

Download Memory

4 thoughts on “How to make persistent variables for iOS and iPadOS Shortcuts | Hacking Shortcuts

  1. Hi. I am struggling to implement this. I understand the concept of dictionaries but it would help to see a sample reminder note containing a dictionary and a sample shortcut which implements your memory subroutine. Thanks.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s