Action on a Draft via URl Scheme, but in the background?

Given an UUID, I am able to tell Drafts via URL-Scheme to open a Draft and run an action, with something like this (where I have an delete-action that just deletes an action).
drafts://open?uuid={query}&action=delete

However, what I am interested in is to run an action completely in the background. In the case above, Drafts comes into the foreground. Here are some possibilities I thought, but I haven’t really figure it out with any of them.

  • I could use an x-success parameter as detailed in the x-callback-specification, but that only works when the source app has an URL scheme as well. Also when using sth like Alfred, the app I would return to varies.
  • with the runaction-URL-Scheme, I can run an action in the background, but haven’t figured out how to pass a specific UUID as a target for the action.
  • I could somehow tell drafts app to hide after execution. But in that case I still get a “flickering” where Drafts briefly appears, deletes the Draft, and then goes back into the background. Its something, but not really nice.

Is there any way to do this properly?

1 Like

Perhaps not ideal, but you could use runAction and have the action you run take care of addressing the proper draft. Not sure what your action does in this case, but it could be written to query for the draft you want to use and act directly on it in script, or if you have an exist action you do not want to modify, you could write another stub action that queues the action on a draft and call that action, like:

let a = Action.find("MY-ACTION");
let d = Draft.find("MY-UUID");

app.queueAction(a, d);
1 Like

Well, if that does the trick without interruption like flickering, that’s fine by me. I’ll give it a try later, thanks!

Okay, I tried to implement this, but I came across another problem specifically for using your proposed solution for my use case.

In my use case, I have the UUID of a draft as a result of an Alfred action, and want to tell Drafts to delete a certain draft via Alfred. So according to your suggestion, I would run the run-Action URL-Scheme like this:

drafts://runAction?action=delete&text={query}

where Alfred replaces the query-placeholder with the UUID of the draft that is to be deleted.

The specific problem of my use case is now, that I do not know how to delete a draft via javascript to implement the script in the way you suggested it. The only way to delete a draft via an action seems to be the “after success of the action”-Trigger, e.g. in the basic delete action. This method, however, only works on the currently open Draft, and as stated in the beginning, I do not want to open a draft because this produces a short flickering of Drafts.

I looked through the Draft Script reference and found no method for deleting a draft (or changing the folder for that matter); the only reference I found seems to be Draft.isTrashed which only checks the status of the draft but does not change it.

This should work…isTrashed is a get/set property.

draft.isTrashed = true;
draft.update();
1 Like

ah, you can set the property, I see. Got it, thanks! :slight_smile:

This action:

Will delete the draft with the UUID passed in as the text in a URL scheme.

e.g.

drafts://x-callback-url/runAction?text=A2D24B20-6205-4111-ACE4-47114A2CC1AA&action=Delete%20By%20UUID

Just swap the desired UUID in for this example one: A2D24B20-6205-4111-ACE4-47114A2CC1AA.

Use it with caution I guess. Deleting drafts from external triggers means you might miss the impact of accidental deletions. Doing it within the app has its advantages :wink:

But using a URL scheme will switch into the app, so I don’t see a way around the “flickering” effect that you reference. I think we would require deeper AppleScript support to allow for that.

thanks for the implementation, I was just finished doing it myself though :stuck_out_tongue:

let uuid2delete = draft.content;
let draft2delete = Draft.find(uuid2delete);
draft2delete.isTrashed = true;
draft2delete.update();

Mine just combined those first two lines as the UUID is only used once.

Yeah I noticed. But I think adding that extra line increases the clarity of the code. Using draft.content is quite unintuitive, considering you are not using the content of a (proper) draft to find the draft you want to delete.

I am pretty certain I’d be itching my head when looking at that code in a year.

I would hope it would be obvious from the parameter to the find function being a UUID and the description you would put on the action (or name of the action for me) :wink:

yeah it comes down to personal preference I guess. I always liked self-explaining variable names, even if others dislike them because of the increased length