Skip to content

Instantly share code, notes, and snippets.

@CherryDT
Last active August 6, 2020 13:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CherryDT/c3e33fec3fbb23fa2389fc835b96b23c to your computer and use it in GitHub Desktop.
Save CherryDT/c3e33fec3fbb23fa2389fc835b96b23c to your computer and use it in GitHub Desktop.
Evernote clipboard code wrapper tool
; AutoHotkey script to wrap plain text clipboard contents for Evernote in such a way that they can be pasted as code
; block and won't get double empty lines, removed indents or anything like that.
; By David Trapp
; License: MIT
; WinClip is required, from here: https://autohotkey.com/board/topic/74670-class-winclip-direct-clipboard-manipulations/
#include %A_ScriptDir%\WinClipAPI.ahk
#include %A_ScriptDir%\WinClip.ahk
; Ctrl+Alt+P = wrap plain text in clipboard to EN code block
^!p::EvernoteCodeWrap()
; Ctrl+Alt+V = wrap plain text in clipboard to EN code block and paste
^!v::EvernoteCodeWrapAndPaste()
EvernoteCodeWrapAndPaste() {
EvernoteCodeWrap()
Send ^v
}
EvernoteCodeWrap() {
plainText := WinClip.GetText()
; Clear other formats, most importantly ENML
WinClip.Clear()
WinClip.SetText(plainText)
; Convert
plainText := RegExReplace(plainText, "\t", " ")
Transform, innerHtml, HTML, % plainText
innerHtml := RegExReplace(innerHtml, "[\r\n]", "")
innerHtml := RegExReplace(innerHtml, "<br>", "</div><div>")
innerHtml := RegExReplace(innerHtml, "<div></div>", "<div><br/></div>")
innerHtml := RegExReplace(innerHtml, " ", "&nbsp;")
; Note: normally EN uses <div> for the -en-clipboard part, but with <span> we can save one newline
; The <br/> is unfortunately required for this whole thing to work
html := "<span style=""-en-clipboard:true;""><br/></span><div style=""-en-codeblock:true;""><div>" . innerHtml . "</div></div>"
WinClip.SetHTML(html)
}
@RichardBronosky
Copy link

For macos, this works:

#!/bin/bash

pbcopy <<HTML
    <span style="-en-clipboard:true;"><br/></span><div style="-en-codeblock:true;"><div>
    foo<br/>
    bar<br/>
    <div><br/></div>
    baz<br/>
    </div></div>
HTML
    
osascript -e "set the clipboard to «data HTML$(pbpaste | hexdump -ve '1/1 "%.2x"')»"

Now paste into Evernote and you get:

foo
bar

baz

Therefore, you can convert any code in your clipboard to Evernote compatible HTML with:

convert_text(){
    python3 -c 'import html, sys; [print(html.escape(l), end="") for l in sys.stdin]' | \
    sed -E $'
        1i\\\n<span style="-en-clipboard:true;"><br/></span><div style="-en-codeblock:true;"><div>\n
        s, ,\&nbsp;,g
        s,$,<br/>,
        s,^<br/>$,<div><br/></div>,
        $a\\\n</div></div>\n
    '
}

I have a complete script at https://gist.github.com/RichardBronosky/c2d609b721df3ecc771c4980e9f6b63f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment