Skip to content

Instantly share code, notes, and snippets.

@dsanson
Created August 25, 2010 21:03

Revisions

  1. dsanson created this gist Aug 25, 2010.
    51 changes: 51 additions & 0 deletions pasteRTFasMarkdown.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    #!/bin/sh

    # Convert RTF contents of clipboard to HTML

    osascript -e 'the clipboard as «class RTF »' | \
    perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | \
    textutil -format rtf -convert html -stdin -stdout | \
    pandoc -f html -t markdown | \
    pbcopy

    # Paste contents of the clipboard into the current application

    osascript <<EOF
    (*
    First we need to get the name of the current application. Trick
    borrowed from http://vanderbrew.com/blog/2010/02/15/get-current-application-with-applescript/
    *)
    on GetCurrentApp()
    tell application "System Events"
    set _app to item 1 of (every process whose frontmost is true)
    return name of _app
    end tell
    end GetCurrentApp
    set _app to GetCurrentApp()
    (*
    Now we need to paste into the current app. I don't know any way to do this
    aside from using GUI scripting (ugh).
    *)
    tell application _app to activate
    tell application "System Events"
    tell process _app
    tell menu bar 1
    tell menu bar item "Edit"
    tell menu "Edit"
    click menu item "Paste"
    end tell
    end tell
    end tell
    end tell
    end tell
    EOF