Action

Bookmark to DEVONthink

Posted by @FlohGro, Last update 17 days ago
  • searches for MarkdownURLs in the current draft (Url Title
  • displays a prompt, which URL you want to bookmark in DEVONthink (if there is more than one URL in the draft, otherwise it will use the only existing URL)
  • calls the creatbookmark x-callback url of DEVONthink to store the Bookmark. the [Url Title] will be used as title.

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    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
    
    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]);
    		}
        var didSelect = pSelUrl.show();
    
        if (didSelect) {
          return pSelUrl.buttonPressed;
        }
      }
    }
    
    function storeURL(descUrl) {
      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);
     	
     	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.