Action
Add 2 QuickReads
created by @FlohGro / more on my Blog
Add 2 QuickReads
Save links from Drafts straight into your Quick Reads reading queue (the read-later service by Matt Birchler).
The action finds every URL in the current draft and saves each one to Quick Reads via its API. If you selected text, only the URLs in the selection are used.
It recognises:
- Markdown links:
[Title](https://example.com/post)(also with<…>around the URL or a link title) - Autolinks:
<https://example.com/post> - Reference links:
[ref]: https://example.com/post - Plain URLs:
https://example.com/post
Duplicate URLs in the draft are only sent once. Trailing punctuation is stripped from plain URLs, while parentheses that belong to the URL (e.g. Wikipedia links) are kept.
[Configuration]
You need a Quick Reads API key (it starts with rl_). You can create one in your Quick Reads account settings.
On the first run the action asks for the key and stores it in a Drafts credential named QuickReads. You can change or remove it anytime in Drafts → Settings → Credentials.
If Quick Reads rejects the key, the action removes the stored credential automatically so you’re asked for a new one on the next run.
[Usage]
- Write or paste one or more links into a draft (or select the part with the links you want to save)
- Run the action
- A notification shows how many links were saved and how many were already in Quick Reads
Links you saved before are reported as “already in Quick Reads” and don’t count as errors. If a link can’t be saved, an alert lists it together with the error and the action fails. That way “After Success” options (e.g. archiving the draft) only run when all links were saved.
If you find this useful and want to support me you can donate or buy me a coffee
Steps
-
script
// Send to QuickReads // Drafts action script: finds every URL in the current draft (or in the // selection, if there is one) and saves each one to Quick Reads // (https://quickreads.app) via its REST API. // // Recognises: // - Markdown links: [Title](https://example.com/post) // [Title](<https://example.com/post> "optional title") // - Markdown autolinks: <https://example.com/post> // - Reference links: [ref]: https://example.com/post // - Plain URLs: https://example.com/post // // The API key (starts with "rl_") is stored in a Drafts credential named // "QuickReads". You're asked for it on first run. Forget/reset it under // Settings → Credentials, or it's cleared automatically if the API rejects it. const API_URL = "https://quickreads.app/api/articles"; // ---------- Credential ---------- const credential = Credential.create( "QuickReads", "Enter your Quick Reads API key (starts with rl_). Create one in the Quick Reads web app under API settings." ); credential.addPasswordField("apiKey", "API Key"); function main() { if (!credential.authorize()) { app.displayErrorMessage("QuickReads: no API key provided"); context.cancel(); return; } const apiKey = (credential.getValue("apiKey") || "").trim(); if (!apiKey) { credential.forget(); app.displayErrorMessage("QuickReads: API key is empty"); context.fail(); return; } // ---------- Collect URLs ---------- const selection = editor.getSelectedText(); const text = selection && selection.trim().length > 0 ? selection : draft.content; const urls = extractUrls(text); if (urls.length === 0) { app.displayWarningMessage("QuickReads: no URLs found"); context.cancel(); return; } // ---------- Send ---------- const saved = []; const duplicates = []; const failed = []; for (const url of urls) { const http = HTTP.create(); const response = http.request({ url: API_URL, method: "POST", encoding: "json", data: { url: url, source: "api" }, headers: { "Authorization": "Bearer " + apiKey, "Accept": "application/json" } }); const status = response.statusCode; if (status === 200 || status === 201) { saved.push(url); } else if (status === 409) { duplicates.push(url); } else if (status === 401 || status === 403) { // Bad key: forget it so the next run prompts again, and stop. credential.forget(); alert( "Quick Reads rejected the API key (HTTP " + status + ").\n\n" + "The stored key has been removed. Run the action again to enter a new one." ); context.fail(); return; } else { failed.push(url + " → HTTP " + status + errorDetail(response)); } } // ---------- Report ---------- const parts = []; if (saved.length) parts.push(saved.length + " saved"); if (duplicates.length) parts.push(duplicates.length + " already in Quick Reads"); if (failed.length) parts.push(failed.length + " failed"); const summary = "QuickReads: " + parts.join(", "); if (failed.length) { console.log("Failed URLs:\n" + failed.join("\n")); alert(summary + "\n\nFailed:\n" + failed.join("\n")); context.fail(summary); } else { app.displaySuccessMessage(summary); } } // Returns a de-duplicated list of http(s) URLs in the order they appear. function extractUrls(text) { const found = []; let rest = text; // URL body that allows one level of balanced parentheses, // e.g. https://en.wikipedia.org/wiki/Foo_(bar) const urlBody = "https?:\\/\\/(?:[^\\s()<>]|\\([^\\s()<>]*\\))+"; // 1) Inline markdown links: [text](url) / [text](<url>) / [text](url "title") const mdLink = new RegExp( "\\[[^\\]]*\\]\\(\\s*<?(" + urlBody + ")>?(?:\\s+(?:\"[^\"]*\"|'[^']*'))?\\s*\\)", "g" ); rest = rest.replace(mdLink, (match, url) => { found.push(url); return " "; }); // 2) Everything else: plain URLs, <autolinks>, reference definitions const plain = /https?:\/\/[^\s<>"'`\[\]{}|\\^]+/g; let m; while ((m = plain.exec(rest)) !== null) { found.push(trimTrailing(m[0])); } // De-duplicate, keeping order const seen = {}; return found.filter((u) => { if (seen[u]) return false; seen[u] = true; return true; }); } // Strip sentence punctuation and unbalanced closing parens from plain URLs. function trimTrailing(url) { let u = url.replace(/[.,;:!?*_~]+$/, ""); while (u.endsWith(")")) { const opens = (u.match(/\(/g) || []).length; const closes = (u.match(/\)/g) || []).length; if (closes > opens) { u = u.slice(0, -1).replace(/[.,;:!?*_~]+$/, ""); } else { break; } } return u; } function errorDetail(response) { try { const body = JSON.parse(response.responseText); if (body && body.error) return " (" + body.error + ")"; } catch (e) {} return ""; } main();
Options
-
After Success Default Notification None Log Level Info
