Creating custom module for repeated script

I am thinking of creating different tasks that operate on TaskPaper format in Drafts and do something with them in OmniFocus, such as “Create project in Personal”. I have some other tasks where this question applies.

Since doing multiple more tasks like this without a provided module in Drafts (such as the Dropbox module), it requires a lot of repetition in the Javascript. Is there any way to add a custom module or class to tasks in Drafts? For instance, I’d like to add the receptive code for parsing TaskPaper format to a single place instead of copying it around.

You can use require and a script library file.

You could have a script step action then include actionthat action and bring in your code as a kind of library.

You could take a look at some of the parsers and API sets in the Action directory as examples of how people have built sets of functionality as what is in effect a library of functionality.

You might also want to look at the existing Omnifocus/Taskpaper group in the Action directory to ensure you are not reinventing the wheel.

1 Like

Thanks, I looked at the require in iCloud thing and it might work. I’ll also look at the action directory if there are examples of parsers, etc.

Yes, I’ve looked at the Rosemary OmniFocus group and have it installed. It has the TaskPaper to OF action which saves it to inbox, seems to rely on OmniFocus capability to parse TaskPaper. Already here if I only wanted to tweak it a little bit, maybe create another action like it but save to a specific folder, I’d need to copy it without having some way to share code. Outside of this issue, I’ve already got other TaskPaper specific actions that do some basic things with TaskPaper files with basic regex (that I copied from the action directory examples). If I do more of this kind of things, it’s better to have some common library to parse the TaskPaper format to a more generic Javascript array/object structure and then perform things with it.

Can you rely on the required module being cached by iCloud Drive on the device?

(As usual I’m concerned with the 35,000 feet problem.)

If you can open the file in “Airplane mode”, you should be fine. I’ve never had any iCloud Drive caching issues, but a sample size of one is not proof.

Almost all of my script resides in iCloud Drive (for longer than the require has been around for Drafts :wink:) as it means I can use whatever editor I like for editing it. Then simple function calls in the Action.

1 Like