Example of passing parameters to a Shortcut and getting the return

For those interested:

Here is a sample script with the associated shortcut that shows how to pass parameters to a shortcut and get the return.

The shortcut calculates the duration of the route from your current location to the specified destination and outputs it as text.

Notes:

  • You must adjust the values for the target with your own values of your region.
  • After each change of the script accepts input must be replaced by with input.
  • At the first execution you have to give the shortcut rights
  • The AppleScript action in the shortcut is not absolutely necessary. Alternatively you can refer directly to the input of the shortcut. If records are no longer ignored as shortcut input, it may be even easier, since there is an action “Dictionary from input”.
tell application "Shortcuts Events"
	-- change this with your target values!
	set targetStreet to "Louisenstraße 90"
	set targetTown to "Bad Homburg"
	set targetZip to "61348"
	set targetRegion to "Deutschland"
	
	set theResult to item 1 of (run shortcut named "Calc Route" with input {targetStreet, targetTown, targetZip, targetRegion})

	--> "I am in the Sebastian-Kneipp-Straße 41 in Frankfurt am Main and will be with you in 9 Minuten! 😅"

end tell

Calc Route.shortcut (23.1 KB)

How useful the use of Shotcuts in AppleScript is, everyone has to decide for himself. The creation of a shotcut is absolutely horrible! You can only duplicate whole shotcut, but not copy parts of them. The assignment of variables is for developer an imposition. And then the many bugs (e.g. records are simply ignored as input)!

However, the range of functions is already considerable (e.g. instead of passing the destination, you could pass a contact name, get the address data from it and send the result to the person) and I’m afraid that this will still grow, since many applications in the future will support the Shortcut app rather than AppleScript.

Sooner or later you will have to deal with the app - unfortunately!

1 Like

Here is another variant with passing parameters as JSON string.

use framework "Foundation"
property parent : a reference to current application
property NSJSONWritingPrettyPrinted : a reference to 1

on toJSON(theData)
	set theJSONData to parent's NSJSONSerialization's dataWithJSONObject:theData options:NSJSONWritingPrettyPrinted |error|:(missing value)
	set theString to (parent's NSString's alloc()'s initWithData:theJSONData encoding:(parent's NSUTF8StringEncoding)) as text
	return theString
end toJSON

tell application "Shortcuts Events"
	-- change this with your destination data
	set theDestination to {Street:"Louisenstraße 90", City:"Bad Homburg", Country:"", Postcode:"61348", Region:"Deutschland"}
	
	set theResult to item 1 of (run shortcut named "Calc Route (JSON)" with input my toJSON(theDestination))

	--> "I am in the Sebastian-Kneipp-Straße 41 in Frankfurt am Main and will be with you in 9 Minuten! 😅"

end tell

This simplifies the shortcut considerably:

Calc Route (JSON).shortcut (22.7 KB)

1 Like

Why not :

to JSON(theData)
    .
    .
    .
end JSON

?

Do you mean the hander name? The hander is from one of my templates and I also have a fromJSON handler:

on fromJSON(strJSON)
	set {x, e} to parent's NSJSONSerialization's JSONObjectWithData:((parent's NSString's stringWithString:strJSON)'s dataUsingEncoding:(parent's NSUTF8StringEncoding)) options:0 |error|:(reference)
	if x is missing value then
		error e's localizedDescription() as text
	else
		return item 1 of ((parent's NSArray's arrayWithObject:x) as list)
	end if
end fromJSON

And the JSON string is used because a shortcut does not accept records as input. This makes parameter passing quite workable, even though accessing the values may be a bit unusual:

In this respect, I have to revise my statements from the first post a bit, as far as the handling of variables is concerned. Nevertheless, the shortcut app currently still contains many bugs and is very unstable.

Thanks for posting these examples, very helpful. However, I’m having a little difficulty getting it working. Something strange is happening.

If I type out the following:

tell application "Shortcuts Events"
	run shortcut named "test1234" with input "xyz"
end tell

This compiles (in Script Debugger) to:

tell application "Shortcuts Events"
	run shortcut named "test1234" accepts input "xyz"
end tell

(Note “with” replaced by “accepts,” it seems incorrectly.)

The strange thing is that the script and shortcut successfully run so long as I don’t change anything. However, if I make any changes to it post-compile (for example change “xyz” to “abc”) then SD stops recognising “accepts input” and gives the error “Expected end of line but found property.”

I was wondering if Shortcuts had ironed out enough bugs to be useable yet, and maybe not this year…

The issue is that they’ve used the same four-letter code for the input parameter as they have for the accepts input property of the shortcut class. Please log a bug with Apple about it.

You can try this workaround, which should solve the recompile problem:

set inputCode to «class inpt»
tell application "Shortcuts Events"
	run shortcut named "test1234" given inputCode:"xyz"
end tell
2 Likes

Hi Shane,

the shortcut is executed, but the input values are not passed (or possibly passed differently).

Tested with a simple shortcut example:

on run {input, parameters}
	(* Your script goes here *)
	return (input as text)
end run

The return value is “”.

1 Like

After the experiences with iOS, I don’t really believe that and some bugs have not been fixed for years or new ones are added with every update.

So I try to avoid this whenever possible, but unfortunately what I feared, that app vendors integrate shortcuts instead of providing an AppleScript dictionary, has come true.