Calling a shortcut from a script using the x-callback-url protocol

Hello once again. In trying to find the best way to call a shortcut from a script, I found documentation on the CallbackURL class. I did a little research and discovered that the Shortcuts app is reported to support the x-callback-url protocol so I decided to try it. In the script, I used lines such as the following:

  let showMatchesShortcutURL = new CallbackURL("shortcuts://x-callback-url/ShowMatchesInHTML");
  showMatchesShortcutURL.addParameter("url", URLText);
  showMatchesShortcutURL.addParameter("regex", regExText);  
  showMatchesShortcutURL.addParameter("html", html);
  showMatchesShortcutURL.getURL();
  await showMatchesShortcutURL.open();

When I run the script, execution gets as far as opening the Shortcuts app, but the shortcut itself is not run. To make sure that I am not doing something wrong, I tried using a URL scheme to the shortcut as follows and that does run the shortcut itself:

shortcuts://run-shortcut?name=ShowMatchesInHTML&input=TestInput

Might anyone know why I cannot get the shortcut to run using the x-callback-url protocol? I would really appreciate it. Thanks so much.

Here is the example x-callback-url from the Shortcuts documentation.

shortcuts://x-callback-url/run-shortcut?name=Calculate%20Tip&input=text&text=24.99&x-success=...&x-cancel=...

Note the inclusion of run-shortcut and the specification of the Shortcut name parameter.

In your code you have not specified the name parameter, or the run-shortcut action. You also seem to have added several non-standard parameters but excluded the input parameter (and presumably the text parameter too).

Oh, I see. I made the changes as follows but now, when I run the script, the Shortcuts app opens and then quickly returns to Scriptable. When I look at the error log, I get the error below.

  let showMatchesShortcutURL = new CallbackURL("shortcuts://x-callback-url/run-shortcut?name=ShowMatchesInHTML&input=Input&text=Text");
  showMatchesShortcutURL.addParameter("url", URLText);
  showMatchesShortcutURL.addParameter("regex", regExText);  
  showMatchesShortcutURL.addParameter("html", html);
  showMatchesShortcutURL.getURL();
  await showMatchesShortcutURL.open();
2022-05-09 12:02:24: Error: Received error from target app: Could not find the specified shortcut..

I tested with another shortcut name that I created in the Shortcuts app, but I still get the same error message.

Do you have any spaces in the name of your shortcut?

Pass the shortcut name (including any spaces and symbols) through encodeURIComponent(string) and then include it in the URL or add it via .addParameter(...). This encodes all special characters correctly for the URL.

Edit: Ah, @sylumer beat me :sweat_smile:

Nah, just an educated guess as to the specific error, yours is much more well rounded.

I totally agree that adding the name using addParameter is the way to go. I’m left a little confused by the existing ones. As I noted earlier, they not standard, recognised parameters for Shortcuts, so my expectation is that they will be ignored.

Only input of text or clipboard should be recognised, the former then passed via the text parameter. So above, once the first issue of the name is resolved, I would only expect the word “Text” to be passed as viable input to the shortcut.

I checked and the shortcut name has no spaces in it. I am copying the name by opening the shortcut, clicking in the name, selecting all of the text and copying it, and pasting the name below:

ShowMatchesInHTML

I made the following changes, but when I run the script, I still get the same “shortcut not found” error:

  let showMatchesShortcutName = "ShowMatchesInHTML";
  let showMatchesShortcutURL = new CallbackURL("shortcuts://x-callback-url/run-shortcut?name=" + encodeURIComponent(showMatchesShortcutName) + "&input=Input&text=Text");
  //showMatchesShortcutURL.addParameter("url", URLText);
  //showMatchesShortcutURL.addParameter("regex", regExText);  
  //showMatchesShortcutURL.addParameter("html", html);
  showMatchesShortcutURL.getURL();
  await showMatchesShortcutURL.open();

I can’t think of what could be wrong. If there are any suggestions I would greatly appreciate it.

Here is some code to launch a similarly named shortcut, and based on your naming conventions.

// Initialise with base URL
let showMatchesShortcutURL = new CallbackURL("shortcuts://x-callback-url/run-shortcut");

// Add the parameters to call a shortcut and pass it a string of text
showMatchesShortcutURL.addParameter("name", "TESTShowMatchesInHTML");
showMatchesShortcutURL.addParameter("input", "text");
showMatchesShortcutURL.addParameter("text", "Some Test Text");

// Compile the URL
showMatchesShortcutURL.getURL();

// Call the URL and wait for it to return with logging
console.log("Start logging...");
console.log("Calling Shortcuts");
await showMatchesShortcutURL.open();
console.log("Returned from Shortcuts");

Here is a shortcut to test it with that displays the passed in text as a notification, with an explicit delay before completing.

https://www.icloud.com/shortcuts/5151d8fea508435199ecf4bed4b0978f

This works for me., and hopefully you can see from the console log that it is waiting to return before the final log line…

Note that the code above does not deal with any success/failure x-callback-url parameters, and the shortcut is not set to pass anything back to Scriptable for further processing.

1 Like

Oh wow! Thanks so much. I will try it just as soon as I can. Thanks again.

So that it is known, the code works for me also. Thanks so much again.