{"id":18292779,"url":"https://github.com/joel-beck/drafts-scripts","last_synced_at":"2025-04-05T10:31:23.861Z","repository":{"id":159506981,"uuid":"630814435","full_name":"joel-beck/drafts-scripts","owner":"joel-beck","description":"Action Scripts for the Drafts App","archived":false,"fork":false,"pushed_at":"2023-11-09T12:27:36.000Z","size":171,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-21T03:11:09.312Z","etag":null,"topics":["automation","drafts","draftsapp","javascript","macos","scripting","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joel-beck.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-04-21T08:02:43.000Z","updated_at":"2024-07-19T21:56:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"ab25e83f-57bd-4c53-99be-0fbcc51e42ce","html_url":"https://github.com/joel-beck/drafts-scripts","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joel-beck%2Fdrafts-scripts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joel-beck%2Fdrafts-scripts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joel-beck%2Fdrafts-scripts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joel-beck%2Fdrafts-scripts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joel-beck","download_url":"https://codeload.github.com/joel-beck/drafts-scripts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247324666,"owners_count":20920694,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","drafts","draftsapp","javascript","macos","scripting","typescript"],"created_at":"2024-11-05T14:19:18.847Z","updated_at":"2025-04-05T10:31:21.043Z","avatar_url":"https://github.com/joel-beck.png","language":"JavaScript","readme":"# Action Scripts for Drafts\n\nThis repository contains a collection of action scripts for the [Drafts](https://getdrafts.com/) app.\n\n\n## Repository Structure\n\nAll source code is written in Typescript files in the `src` directory.\nThe Typescript files are roughly organized into Drafts action groups.\n\nIt is good practice to dedicate a separate function to each Drafts action.\nThis has the advantage that only a single function call is required in the Drafts `Script` step.\n\n\n### Bundling\n\nDrafts expects a single Javascript file that contains all the code used in Drafts actions.\nTo bundle all Typescript files in the `src` directory into a single Javascript file (here `drafts-actions.js`), the project has moved from the [Rollup](https://rollupjs.org) bundler to using [Bun](https://bun.sh).\nBun not only simplifies the bundling process, but additionally replaces `npm` as a package manager and `node` as a runtime environment.\n\nThe bundling process is straightforward:\n\n- All Drafts actions (functions in the `src/*.ts` Typescript files) are re-exported in the `src/index.ts` file.\n\n- 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.\n\n    ```js\n    const ENTRYPOINT = \"src/index.ts\";\n    const OUTPATH = \"drafts-actions.js\";\n\n    const result = await Bun.build({\n    entrypoints: [ENTRYPOINT],\n    format: \"esm\",\n    sourcemap: \"none\",\n    });\n    ```\n\n- One additional cleanup step is required to make the bundled Javascript file compatible with Drafts.\nThe bundled Javascript file contains exports at the end, which is not supported by Drafts.\nTherefore, the `scripts/build.js` script removes the exports section from the bundled Javascript file prior to writing the bundled output to `drafts-actions.js`.\n\n- 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.\nTo run the bundling process in watch mode, use `bun watch`.\n\n\n## Usage inside Drafts\n\nUser scripts can be connected to the Drafts app as follows:\n\n1. Move the scripts directory inside the default Drafts Scripts directory - in my case `~/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/Library/Scripts`\n1. 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.\n\n### Example\n\nThe following example uses the `insertMarkdownLink()` function inside a Drafts action.\nThis user scripts repository is a child directory of the default Drafts Scripts directory with the name `custom-scripts`.\nThe bundled Javascript file is named `drafts-actions.js`.\n\nThen, the code inside the action `Script` step is\n\n```javascript\nrequire(\"custom-scripts/drafts-actions.js\")\n\ninsertMarkdownLink()\n```\n\n\n## Resources\n\n- The idea to modularize scripts into separate files originates from [this forum discussion](https://forums.getdrafts.com/t/developing-outside-of-drafts).\n\n- The `drafts-type-definitions.js` file is provided by Greg Pierce (the Drafts creator) and copied from [his GitHub repository](https://github.com/agiletortoise/drafts-script-reference/blob/main/docs/drafts-definitions.js).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoel-beck%2Fdrafts-scripts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoel-beck%2Fdrafts-scripts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoel-beck%2Fdrafts-scripts/lists"}