Action

Open URLs and File-URLs

Posted by Chris Grieser (armenaton), Last update over 2 years ago

UPDATES

over 2 years ago

small bugfixes

show all updates...

over 2 years ago

small bugfixes

over 2 years ago

also works on file hooks now

If text is selected, opens on URLs in the selection.
If no text is selection, opens all URLs in the whole draft.

Also works on URLs to the file system like file:///Users/myname/file.txt

Steps

  • script

    
    //checks ether there is an in selection at the moment
    
    if (editor.getSelectedText() == "") {
    	var textToCheck = draft.content; 
    } else {
    	var textToCheck = editor.getSelectedText();
    }
    
    //identifies URL and opens all, URL-Regex from https://stackoverflow.com/a/11209098
    let URLmatches = textToCheck.match(/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig);
    
    
    if (URLmatches != null){
    	URLmatches.forEach(element => {
    		//checks if it is a file-hook
    		if (element.match(/file:\/\/\/[^\)]*/ig) != null){
    			draft.setTemplateTag("filepath", element);
    			if (element.match(/(.*)\/$/) != null) { draft.setTemplateTag("folder", "true") ;} ;
    		}	else {
    			app.openURL (element);
    		}
    	})
    } else {
    	app.displayErrorMessage("No URL or file URLs found");
    }
    
    //archives when no selection was made
    if ((editor.getSelectedText() == "") && (URLmatches != null)){
    	draft.isArchived = true; 
    	draft.update();
    	editor.new();
    }
    
  • runAppleScript (macOS only)

    -- AppleScript content
    -- Editing and testing in Script Editor recommended
    on execute(draft)
        tell application "Finder" 
    		open location "[[filepath]]"
    		if ("[[folder]]" = "true") then activate
    	end tell
    end execute

Options

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