Action
open markdown url
this action was build by inspiration form this entry in the drafts forum: https://forums.getdrafts.com/t/i-can-t-seem-to-open-a-url-from-a-scrip/6980
Credits to @RoyRogers - i stole some code from your examples and actions..
This action will search the current draft for markdown formatted urls - e.g. (I inserted a „]‘(„ between the brackets to prevent the description to display only the urls):
[Google]‘(www.gogle.com) this is famous search engine
text before [Bear]‘(bear://) and text after
[text in brackets, no problem] [OneNote]‘(onenote://open)
[Google]‘(www.gogle.com)
BBC
It will search through the draft immediately open the url if only one url is inside the draft.
If there are several urls, a prompt with the URL titles as buttons will be displayed to select the url to open.
The urls are opened with safari, to enable „callback“ links (e.g. drafts://open..)
Steps
-
script
// open markdown url const content = draft.content; // regel to match markdown urls "[]()” let regex = /\[([^\[]+)\]\(([^\[]+)\)/g // prompt to select the url to open var pSelUrl = Prompt.create(); // flag to indische if any url was found. var foundUrlCounter = 0; // if only one url is in the note - open it directly - first url will be stored in this variable var firstURL = ''; var urlToOpen; let foundUrl = findAndStoreUrls(); if (foundUrl) { urlToOpen = getUrlToOpen(); } else { let message = "no markdown url '[..](..)' found in draft"; context.cancel(message); app.displayInfoMessage(message); } if (foundUrl) { if (urlToOpen) { openURL(urlToOpen); } else { let message = "no url to open selected"; context.cancel(message); app.displayInfoMessage(message); } } // functions // stores the found urls in the prompt as buttons function findAndStoreUrls() { var matches = [...content.matchAll(regex)] if (matches) { for (match of matches) { foundUrlCounter++; let description = match[1]; let url = match[2]; // add "http://" if the url is no callback url and just witten as domain. if (!url.match(/:\/\//)) { url = 'http://' + url; } // add the found url to the prompt pSelUrl.addButton(description, url); if (foundUrlCounter == 1) { firstURL = url; } } } if (foundUrlCounter == 0) { return false; } else { return true; } } function getUrlToOpen() { if (foundUrlCounter == 1) { return firstURL; } else if (foundUrlCounter > 0) { pSelUrl.title = "select URL:"; var didSelect = pSelUrl.show(); if (didSelect) { return pSelUrl.buttonPressed; } } } function openURL(url) { let success = app.openURL(url, false); if (!success) { let message = "failed to open url: " + url; app.displayErrorMessage(message); context.fail(message); } }
Options
-
After Success Nothing Notification Error Log Level Error