Shortcuts functions/subroutines with multiple arguments

For ages, I couldn’t figure out for the life of me how to create a Shortcuts subroutine that required multiple inputs. (Toy example: a subroutine that calculates the product of two numbers that are both inputs.) I was even going to post on here trying to figure it out.

The answer turns out to be surprisingly simple: create a Shortcuts subroutine that requires a single argument that is a list. Make the first actions of the Shortcut extracting each list element, then do what you like with them!

Hope this is helpful for someone!

3 Likes

Or, alternatively, a dictionary! (This might be easier in some cases because you can name the values.)

3 Likes

I highly recommend dictionaries, lists are technically unordered (whole Shortcuts was still Workflow they switched to preserving the order) and in general it’s good practice to be specific. And this way if something unexpected happens it’s easier to find out what.

2 Likes

Woah, didn’t know that. But that would break so many things.

There is a problem with dictionaries when you want to store images or file references. Dictionaries can only store primitives like numbers, strings and booleans. Lists can store them all.

You can Base64 encode any complex data types to add them to a list :slight_smile:

1 Like

Yes, that’s a possibility, but you’ll loose the file reference and it adds additional overhead to every function call.
As we know, Shortcuts is not the fastest language interpreter. If you have a shortcut that calls many functions, then every additional action adds to the execution time. Of course this is only an edge case and the design decision has to be made by the Shortcut author. I just wanted to highlight the case that a dictionary doesn’t always work. It isn’t the eierlegende Wollmilchsau, as we call it in Austria :wink:

If you loose the file reference, then actions like Delete File or Delete Photo won’t work anymore.

Don’t you mean lists are ordered and dictionaries are technically not? I recall a change for this in the Workflow era, and dictionaries as key value pairs don’t need to maintain a position as they are ID referenced, but list processing everywhere I’ve come across is ordered (allows for FILO/FIFO processing, etc.)

In general, traditional programming approaches use a list based argument approach for functions and routines. Shortcuts is using this behind the scenes, but abstracting the use for us so we can use single variables, lists, dictionaries or even a combination of the latter two :wink:

As long as you set defaults and include some error handling the choice shouldn’t matter, but I would personally only use dictionaries for alphanumeric data when passing to another shortcut in a routine or function like manner.

If for example the dictionary refers to the files being sent over, you can store the dictionary, and the files eg images in a variable and send the variable with both in Run Shortcut. You can then use the filename reference in the dictionary to retrieve the corresponding file with Find File.

Yes. But that doesn’t work with photos from the Photos app for example.

I’m not sure I understand the objection.

Photo albums photos still have a filename, so could be referenced if needed, or even renamed from the Shortcut it’s up to what you’re trying to achieve, and if you need to reference each individually. Passing a dictionary and multiple photos in a variable doesn’t have to reference each individually to still have value eg dictionary selecting a function to apply to all photos passed.

Also the method can be used for other objects like contacts, articles etc.

I’m thinking here of the action Delete Photos. It only works if it gets a reference to a photo from the photos app. If you base64 encode and decode, or rename, or edit this photo in any way, the reference is lost and this action gives a warning. Also the Save Photo to Album action duplicates the photo if it has no reference, but you just wanted to add it to a specific album, without duplicating it.

Necroing this post to give another example where dictionaries don’t work:
It want to pass a Things item to a sub-shortcut and could only get it to work with lists.