Action

Bookmark to DEVONthink

Posted by FlohGro, Last update almost 3 years ago

UPDATES

almost 3 years ago

added bookmark all url feature and updated description

show all updates...

almost 3 years ago

added bookmark all url feature and updated description

over 3 years ago

removed unnecessary comment

over 3 years ago

fixed issue for usage on macOS (base URL is now changed depending on the platform this action runs)

over 3 years ago

Description updated (fixed formatted link)

created by @FlohGro / more on my Blog

Bookmark to DEVONthink

This action lets you add bookmarks to DEVONthink on the Mac / DEVONthink To Go on i(Pad)OS.

It will search the current draft for markdown formatted urls e.g.:

- [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](www.bbc.co.uk)

Depending on how many URLs are available in the draft the action will either

  • bookmark the only existing markdown URL to DEVONthink
  • display a prompt if multiple URLs are present in the draft - you can select by the given title which URL should be bookmarked.
    • the prompt will allow you to bookmark all available URLs in the draft to DEVONthink, too

The action automatically detects if you use this action on the Mac or an iOS device to ensure it calls the correct URL scheme for DEVONthink / DTTG

If you just want to open URLs check out the open markdown URL action instead.

[Configuration]

no configuration needed

[Usage]

run this action whenever you want to bookmark a (markdown) URL from a draft to DEVONthink.


If you find this useful and want to support me you can

Buy Me A Coffee

Steps

  • script

    // created by @FlohGro
    
    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 urlsWithDescription = []
    
    var links = new Map();
    
    var urlToStore;
    
    // DEVONthink URL scheme
    
    if (device.model == "Mac") {
        var baseUrl = "x-devonthink://createbookmark"
    } else {
        var baseUrl = "x-devonthink://x-callback-url/createbookmark"
    }
    
    let foundUrl = findAndStoreUrls();
    
    if (foundUrl) {
        urlToStore = getUrlToStore();
    } else {
        let message = "no markdown url '[..](..)' found in draft";
        context.cancel(message);
        app.displayInfoMessage(message);
    }
    
    if (foundUrl) {
        if (urlToStore) {
            storeURL(urlToStore);
        } 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;
                }
    
                links.set(description, url);
    
                if (foundUrlCounter == 1) {
                    firstURL = [description, url];
                }
            }
        }
        if (foundUrlCounter == 0) {
            return false;
        } else {
            return true;
        }
    }
    
    function getUrlToStore() {
        if (foundUrlCounter == 1) {
            return firstURL;
        } else if (foundUrlCounter > 0) {
            pSelUrl.title = "select URL:";
    
            for (currentDescription of links.keys()) {
                let currentUrl = links.get(currentDescription);
                pSelUrl.addButton(currentDescription, [currentDescription, currentUrl]);
            }
            pSelUrl.addButton("Add all URLs")
            var didSelect = pSelUrl.show();
    
            if (didSelect) {
                return pSelUrl.buttonPressed;
            }
        }
    }
    
    function storeURL(descUrl) {
    
        if (descUrl == "Add all URLs") {
    
            for (currentDescription of links.keys()) {
                let currentUrl = links.get(currentDescription);
                let cb = CallbackURL.create();
                cb.baseURL = baseUrl;
                cb.addParameter("title", currentDescription);
                cb.addParameter("location", currentUrl);
    
                if (device.model == "Mac") {
                    cb.waitForResponse = false;
                    cb.open();
                } else {
                    var success = cb.open();
    
                    if (success) {
                        app.displaySuccessMessage("bookmarked \"" + currentDescription + "\" in DEVONthink");
                    } else {
                        app.displayErrorMessage("bookmarking \"" + currentDescription + "\" in DEVONthink failed!");
                        context.fail()
                    }
                }
            }
    
    
        } else {
    
            let description = descUrl[0];
            let url = descUrl[1];
            //alert(description + ": " + url);
    
            var cb = CallbackURL.create();
            cb.baseURL = baseUrl;
            cb.addParameter("title", description);
            cb.addParameter("location", url);
    
            if (device.model == "Mac") {
                cb.waitForResponse = false;
                cb.open();
            } else {
                var success = cb.open();
    
                if (success) {
                    app.displaySuccessMessage("bookmarked \"" + description + "\" in DEVONthink");
                } else {
                    app.displayErrorMessage("bookmarking \"" + description + "\" in DEVONthink failed!");
                    context.fail()
                }
            }
        }
    
    
    }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.