This repository contains a collection of action scripts for the Drafts app.
All source code is written in Typescript files in the src directory.
The Typescript files are roughly organized into Drafts action groups.
It is good practice to dedicate a separate function to each Drafts action.
This has the advantage that only a single function call is required in the Drafts Script step.
Drafts expects a single Javascript file that contains all the code used in Drafts actions.
To bundle all Typescript files in the src directory into a single Javascript file (here drafts-actions.js), the project has moved from the Rollup bundler to using Bun.
Bun not only simplifies the bundling process, but additionally replaces npm as a package manager and node as a runtime environment.
The bundling process is straightforward:
-
All Drafts actions (functions in the
src/*.tsTypescript files) are re-exported in thesrc/index.tsfile. -
The
scripts/build.jsscript usessrc/index.tsas the entry point and bundles all Typescript files into a single Javascript filedrafts-actions.js. The name of this output file is later used in theScriptstep of Drafts actions.const ENTRYPOINT = "src/index.ts"; const OUTPATH = "drafts-actions.js"; const result = await Bun.build({ entrypoints: [ENTRYPOINT], format: "esm", sourcemap: "none", });
-
One additional cleanup step is required to make the bundled Javascript file compatible with Drafts. The bundled Javascript file contains exports at the end, which is not supported by Drafts. Therefore, the
scripts/build.jsscript removes the exports section from the bundled Javascript file prior to writing the bundled output todrafts-actions.js. -
The bundling process is executed by running
bun scripts/build.jsfrom the command line, or, alternatively,bun run buildas defined in thescriptssection of thepackage.jsonfile. To run the bundling process in watch mode, usebun watch.
User scripts can be connected to the Drafts app as follows:
- Move the scripts directory inside the default Drafts Scripts directory - in my case
~/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/Library/Scripts - Choose the
Scriptstep inside a Drafts action and import the function for this action withrequire()from the bundled Javascript file. Provide the relative path to the script file with respect to the default Drafts scripts directory. Then call the function for this action.
The following example uses the insertMarkdownLink() function inside a Drafts action.
This user scripts repository is a child directory of the default Drafts Scripts directory with the name custom-scripts.
The bundled Javascript file is named drafts-actions.js.
Then, the code inside the action Script step is
require("custom-scripts/drafts-actions.js")
insertMarkdownLink()-
The idea to modularize scripts into separate files originates from this forum discussion.
-
The
drafts-type-definitions.jsfile is provided by Greg Pierce (the Drafts creator) and copied from his GitHub repository.