[Pending] Automatically run action on a Workspace on a scheduled basis

If it is possible, how do I automatically execute a specific action against all drafts in a specific Workspace on a scheduled basis?

For example, I have:

  • a workspace “Questions”
  • drafts which each represent a different question
  • an action “Add to Evernote” which adds the draft (i.e. question) as a list entry to a specific Evernote Note/Journal

If I were able to automate a script to run every day which automatically executes an action against all drafts in a given Workspace, it would save me from doing this manual purging myself. Thanks!

Workspaces are set up through tags.

So you can use a script with the construct.

[
### Example: Querying drafts
](https://scripting.getdrafts.com/classes/draft#example-querying-drafts)

```
// query a list of drafts in the inbox with the tag "blue"
let drafts = Draft.query("", "inbox", ["blue"])
```

Replace blue with your tag name.

If you tell me more about your question workspace (how it is set up) I can create a small action and share it with you.

On what platform do you want to run the action?

For iOS/iPadOS, only Shortcuts gives you a schedule option. On the Mac, you have many more.

Shortcuts you can call an action direct using a Drafts step. On the Mac, you probably want to utilise a Drafts URL scheme call.

Unfortunately, that is not a true statement.

Workspaces are built from the following:

  1. Text search queries.
  2. Tag filters (including matching any, matching all, options for omissions).
  3. Start dates.
  4. End dates.
  5. The folders for a workspace each have their own settings, and so flagged draft inclusion can also play a part. I think the Draft.query() will utilise the default (null workspace) settings for this.

Instead, the Workspace object has its own equivalent query that does take into account the above.

Here is one way I might build something to do this. I’ve expanded it a little in the function to make it easier to comment and follow.

Let’s start with a simple action to display the title of a draft. I’ve specifically made it an example action that is not purely script-based so that we are forced to call the separate action rather than just bundling it into one set of code. More efficient to code as one I suspect, but this is the more generic case.

Now, let’s create a little function that we can call. We’ll give it the name of the Workspace, we want to use, the name of the folder within that workspace that we want to process, and the name of the Action that we want to use.

function processWorkspaceDrafts(p_strWorkspaceName, p_strFolder, p_strActionName)
{
	//Get the action 'object' to be run against each draft
	let acToRun = Action.find(p_strActionName);

	//Get all of the drafts in the specified folder in the specified workspace
	//and then process each of them in turn
	let adWorking = Workspace.find(p_strWorkspaceName).query(p_strFolder);
	adWorking.forEach(function (draftCurrent)
	{
		//Queue the action to run against the draft
		app.queueAction(acToRun, draftCurrent);
	});

	//At the end as a final useful bit of information, we'll return the number of drafts processed.
	return adWorking.length;
}

To utilise this, create an action with a *Script* step. Add the function above, and then we just need to add an additional line to call it.

For example, if I wanted to run this against my “novel” workspace, but just against the drafts I have in my inbox in that folder, my calling line for the function above might look like this.

processWorkspaceDrafts("novel", "inbox", "Display Title");

If I wanted to get a brief overview on its processing, I could do this.

app.displayInfoMessage("Drafts sent for processing: " + processWorkspaceDrafts("novel", "inbox", "Display Title"));

But do note that this message is displayed after the initial action is run, which is before each of the queued actions is run.

A small rabbit hole on this point

If you wanted to get the information displayed right at the end of all processing, you would need to queue another action to display the information. You would then either need to write it to file for retrieval, store it on the clipboard, or queue that action on the current draft and use a custom template tag to store the value. However, you also need to be aware that in the interim one of the previous queued actions could update the draft and potentially cause issues; but the risks are low.

I hope that’s all easy enough to follow and explains why you shouldn’t just use Draft.query(), and a way that I think would work generically. At least it did in my testing so far :wink:

2 Likes

Yep.
Sorry @baldr I got that wrong.

I missed the Workspace object and it’s Workspace.query function.

As @sylumer says that the way to go.

1 Like

Probably Mac because I can probably figure it out myself with Shortcuts, but with Mac I’m not sure what I’d do, something involving AppleScript I’m guessing.

Thank you for the Drafts action you created! Now the question becomes… how do I have that action be automated to run on a scheduled basis, with no manual intervention?

The de facto way to run scheduled actions on the Mac is to use launchd.

It is hardly the friendliest of tools, but it is what Apple replaced cron with. But, if you search in the Mac App Store for “launchd”, you’ll find that helper applications are available.

Personally, I find it ridiculously easy within Keyboard Maestro to manage scheduled macros, so if you happen to be a Keyboard Maestro user, I’d look at that. For example, here’s one that clears out my expired temporary TextExpander snippets each morning.

2020-10-11-18.23.46

But there are various scheduler apps out there. It all depends on what you have, and if you have nothing third party, then you always have launchd as Apple apparently intended.

That’s the scheduling taken care of. The next step if therefore to create something to run to trigger the action.

For that we just utilise Drafts’ URL scheme and a line of script; though if you are scheduling through Keyboard Maestro you have many options of how to open a URL.

This script will trigger a Drafts action called “Process Workspace”.

#!/bin/zsh
open "drafts://x-callback-url/runAction?text=&action=Process%20Workspace"

There’s no need to resort to AppleScript, and I would argue in the case of triggering an action via a URL, it is overkill. AppleScript support in Drafts itself extends only to creating a new draft and nothing more. Hopefully, it’ll be back on top of Greg’s to-do list before too long, but until then, URL schemes are the most direct way I can think of.

Create your script file (not forgetting to make it executable), point your scheduler at it with a directive to run it at the time you require, and you’re done.

2 Likes

I do not want to disagree but why not create a Javascript and use it on Mac and iOS.

Shortcuts tend to be slow and hard to maintain.

Only a thought…

Can you give an example of what you mean and how it would be triggered? I don’t follow as to where it would be applied.

Are you sure? Shortcuts itself is a native part of iOS, and the step to trigger an action is a single action should not be at all hard to maintain.

ok. if it is only one action it is not hard to maintain.
I never stay at shortcuts that are small and I personally always bump on their borders. That is definitively caused by me being a developer.

But to move blocks around in Shortcuts is a longer automation is definitely harder that working with a script.

I was misreading this:

But I would state that one automation should be enough (iOS or macOS) hence all data will be synced to the other system or am I wrong?

Indeed. That’s why I asked at the start which platform would be used.

1 Like

Thank you for spelling it out for me, kind sir. I will attempt to implement and keep this thread updated should it be needed.

I am reviving this thread because the Action I want to automate on a scheduled basis - Append to an Evernote Note - is not supported from Mac. I get the “This action has steps which are not supported on this platform.” error when trying.

Therefore, I need to actually automate and execute the Action on iOS. I’m assuming Shortcuts is my only option here… does anyone by chance have an example Shortcut flow which retrieves Drafts from a Workspace, and iterates through each draft, applying an action to each one? Thanks.

EDIT: Actually, I think I got it, it’s pretty simple. But I still welcome any other example Shortcuts to take a look at!

It should just be a get drafts from workspace action, followed by a repeat with each item on each of the drafts to get the draft ‘objects’. Within the repeat, use an append to Evernote action utilising the content of the draft being processed by the loop (use the repeat item variable, set as a draft, and specifically to the content). In the same loop, I would also consider moving the draft to say the archive so that it would not get processed multiple times in succession should you not manually intervene.

Something like this perhaps.

Your device would need to be unlocked to do this. Therefore, I would consider just having a reminder pop-up with a link to run an action within Drafts to do the processing and sending to Evernote. It should run more quickly that way as it would not be switching between apps, and so be less disruptive to you.

1 Like

Thanks again @sylumer .

If I’d like to do this for each Workspace - and I have over a dozen Workspaces - it seems I’d have to manually create a Shortcut for each Workspace since there is no option (Shortcuts - Drafts User Guide) to retrieve a list of Workspaces and have Shortcuts iterate over each one in a Repeat Each loop… is this right?

You should be able to create a Drafts action to enumerate the Workspaces and return the list to Shortcuts to iterate over.

For example, there’s a scripting function in my Drafts library that gets all the workspace names and you could put the result somewhere that could be read by a shortcut - clipboard, file, a specific draft. Or call via an x-callback-url with a return and process that.

But would you really want to process all workspaces? That seems overly broad, and in that case, why differentiate at all? If it was to place in different Evernote notes, then you would need the note to match each workspace and so there would seem no point to the dynamic workspace name population given the manual setup requirement.

I think some of the context to your workflow may be missing from, and perhaps benefit, this discussion.

1 Like

Thanks, that looks interesting.

Correct, I would have a unique Evernote note for each Workspace. I’d probably have to have the note title be the same as the Workspace name, if creating a long case statement in Shortcuts via the “If” conditional isn’t practical.