Import Script - reuse “script” drafts

I’ve built up some helper functions that I’ve wanted to use across multiple actions. I know we can use Include Action to essentially import another script, allowing for reuse. However, I wanted to be able to do the same while evaluating a script that I’m working on in the main editor (using the Eval Draft action), which doesn’t have access to the Include Action steps. So I created an Import Script action that defines an import_script(name) function that other scripts can use.

I’ve uploaded it here if anyone else might find this helpful: https://actions.getdrafts.com/a/1Fn

Here’s how I’m currently using it. To start, I have a draft that looks like this:

// OmniFocus Helper Functions

function OF_GetRefLinks(draft_uuid){
  // loads a draft given a UUID and processes into an object
}

function OF_SelectLink(ref_links){
  // Displays a list of links and prompts the user to select one
}

Then I have another script/action where I want to use these functions. I can do so like this:

// OmniFocus - Create Tasks Link

import_script("OmniFocus Helper Functions");
var proj_links_uuid = "E67B1DC9-A3EE-42D8-9BB3-2FD634AD0C27";
var proj_links = OF_GetRefLinks(proj_links_uuid);
var project = OF_SelectLink(proj_links); 
// do something with “project”
3 Likes

Updated the import_script function in this action so now it will accept either a name or a UUID. Passing in a UUID is slightly faster, and eliminates ambiguity, at the expense of needing to look up and work with UUIDs.

Also reworked the name lookup portion so it stops once it finds a match, rather than processing all drafts that match the query.

1 Like