GithubHelp home page GithubHelp logo

joel-beck / drafts-scripts Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 171 KB

Action Scripts for the Drafts App

License: MIT License

JavaScript 78.95% TypeScript 21.05%
automation drafts draftsapp javascript macos scripting typescript

drafts-scripts's Introduction

Action Scripts for Drafts

This repository contains a collection of action scripts for the Drafts app.

Repository Structure

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.

Bundling

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/*.ts Typescript files) are re-exported in the src/index.ts file.

  • The scripts/build.js script uses src/index.ts as the entry point and bundles all Typescript files into a single Javascript file drafts-actions.js. The name of this output file is later used in the Script step 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.js script removes the exports section from the bundled Javascript file prior to writing the bundled output to drafts-actions.js.

  • The bundling process is executed by running bun scripts/build.js from the command line, or, alternatively, bun run build as defined in the scripts section of the package.json file. To run the bundling process in watch mode, use bun watch.

Usage inside Drafts

User scripts can be connected to the Drafts app as follows:

  1. Move the scripts directory inside the default Drafts Scripts directory - in my case ~/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/Library/Scripts
  2. Choose the Script step inside a Drafts action and import the function for this action with require() 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.

Example

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()

Resources

  • The idea to modularize scripts into separate files originates from this forum discussion.

  • The drafts-type-definitions.js file is provided by Greg Pierce (the Drafts creator) and copied from his GitHub repository.

drafts-scripts's People

Contributors

joel-beck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.