Action

Import URL

Posted by agiletortoise, Last update over 1 year ago

UPDATES

over 1 year ago

Add option to convert HTML to Markdown on import.

Prompt for a URL and download the raw HTML content of that URL and place it in a new draft. Optionally, convert that HTML to Markdown for the new draft. If a URL is in the clipboard when run, auto-fill the URL value with that URL.

Steps

  • script

    // import content of a URL
    
    // prompt for URL
    let p = Prompt.create()
    p.title = "Import URL"
    p.message = "Enter URL to import"
    
    let initialText = "";
    // use clipboard if it's a URL
    let clipboard = app.getClipboard();
    if (clipboard.startsWith("http")) {
    	initialText = clipboard;
    }
    p.addTextField("url", "URL", initialText, {"autocorrect": false, "autocapitalization": false});
    p.addSegmentedControl("type", "Import As...", ["HTML", "Markdown"], "Markdown")
    p.addButton("Import");
    
    if (p.show()) { // display prompt
    	// get url and load with HTTP GET request
    	let url = p.fieldValues["url"];
    	let http = HTTP.create();
    	let response = http.request({
    		"url": url,
    		"method": "GET"
    	})
    	if (response && response.success) {
    		// if response came back, create new draft
    		let text = response.responseText
    
    		if (p.fieldValues["type"] == "Markdown") {
    			let h = new HTMLToMarkdown()
    			text = h.process(text)
    		}
    
    		let d = Draft.create()
    		d.content = text
    		d.update()
    		editor.load(d);
    	}
    	else {
    		context.fail()
    	}
    }
    else {
    	context.cancel()
    }

Options

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