Skip to content

Instantly share code, notes, and snippets.

@sgonyea
Forked from dsanson/pasteRTFasHTML.sh
Created March 16, 2016 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgonyea/6b3df8f0f823c29991b6 to your computer and use it in GitHub Desktop.
Save sgonyea/6b3df8f0f823c29991b6 to your computer and use it in GitHub Desktop.
#!/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 | \
ruby -ne '@found=true if $_ =~ /<body>/; next unless @found; exit if $_ =~ /<\/body>/; puts $_;' | \
grep -v "</body>" | \
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment