How to make Scriptable open x-success URLs passed in by Drafts?

Anybody have an action that passes some data from Drafts to Scriptable?

I’m having trouble getting a script to work and can’t tell exactly where the issue is.

EDIT:

The issue seems to be that Scriptable doesn’t open the x-success callback URL passed by Drafts at the end of the script.

How should that be done? Just loading that URL explicitly using a new Request or something like that?

A few weeks back I posted some details of working with passing dictionary data between Shortcuts and Scriptable in response to a query. One of these methods was around the use of x-callback-url. Take a look at the CarLog3 script in the following linked post, and have a read through the details of the third method.

2 Likes

Thanks! Looks like the bit I am looking for is this line:

Safari.open(urlSuccess)

I think I can make it work from there.

@kopischke once posted a helper class:

cpac, do you care to share your final script? I’m trying to work out this question too, especially now that Scriptable lets me build widgets… I’d love to see what you did.

Sure. Here’s the relevant part of the Drafts script:

// build Scriptable callback URL

const baseURL = "scriptable:///run";

var cb = CallbackURL.create();
cb.baseURL = baseURL;
cb.addParameter("scriptName", "write comlog");
// cb.addParameter("openEditor", "true");
cb.addParameter("client", JSON.stringify(client));
cb.addParameter("addition", addition);

console.log(cb.url);
var success = cb.open();
	
if (!success) {
	context.fail("Failed to write to comlog");
	console.log(cb.status);
}

And here’s the relevant part of the Scriptable script:

// collect data from URL parameters passed from Drafts
const client = JSON.parse(args.queryParameters.client);
const addition = args.queryParameters.addition;
const successURL = args.queryParameters["x-success"];
…

// return to Drafts
Safari.open(successURL);

So I’m stuck here. What I want is actually super simple. I want to just pull in text from Drafts to Scriptable. The ultimate goal is to use that text in a widget. I can’t get that data in at all. What am I doing wrong?

  • The URL scheme I’m using is:
    drafts://x-callback-url/get?uuid=9B3A549D-DA68-4EB1-8FDF-C823AC143E77&x-success=scriptable:///run?scriptName=Text
  • The Scriptable lines I’m using are:
    const successURL = args.queryParameters[“x-success”];
    Safari.open(successURL);

I did try to use cpac’s Drafts code but I don’t get what’s going on in the Drafts section of the script.

You should post your entire script as the pieces you have are fine so far as they go. But they don’t do anything by themselves.

Oh, I’d be elated if I could get it to do anything at this point. Here’s the complete script I’m working with for now. I get “Expected value of type string but got value of type undefined”

I call the Drafts URL above (drafts://x-callback-url/get?uuid=9B3A549D-DA68-4EB1-8FDF-C823AC143E77&x-success=scriptable:///run?scriptName=Text)

// In Scriptable I have:
const successURL = args.queryParameters[“x-success”];
console.log(successURL)

let alert = new Alert()
alert.title = successURL
alert.message = “This is my alert.”
alert.addAction(“OK”)
await alert.presentAlert()

Do remember to block your code with back ticks, otherwise we have to amend the code to remove all the stuff that Discourse does to it when you post.

Drafts’ call to get doesn’t return x-success. x-success is the URL for Drafts to run on a successful x-callback run. get appends the (URL encoded) draft text to the x-success URL as a parameter called text.

When the Scriptable script runs, we do want to access args.queryParameters, but the result isn’t an array. It’s actually an object with the attributes of the parameters.

Finally, you probably also want to make the draft content the message rather than the title of the alert.

Maybe try it more like this:

let alert = new Alert()
alert.message = args.queryParameters.text;
alert.title = "Draft Content"
alert.addAction("OK")
await alert.presentAlert()

Hope that helps.

1 Like

Thanks so much for taking the time with this. That was helpful. I wish I could swing this project—displaying some text from either Drafts or even just from Shortcuts in a Scriptable widget. I’m afraid my skills just aren’t up to it and I can’t find enough Scriptable documentation to work it out. After maybe 10+ hours, learning curve has me beat, I’m afraid.

Thank you very much for your help!

Check back here after iOS 14 has been out. I intend to create exactly that sort of widget. But I’m not jumping on the beta train.

2 Likes