Need to automate copy / paste graphic selection from Preview to Google Doc

applescriptgoogle docsgoogle-chromepreview

I need to paste sequential rectangular selections from a PNG opened in Preview into a designated Google Doc. I suspect AppleScript is the best candidate for this, but I'm open to using a more familiar language like Python or JS. Here's some rough pseudocode for what I want to do.

   Define Hot Keys for CopyPaste and Done
   File Dialog to select destination Google Doc
   Open destination doc
   Set insertion point at end of destination doc
   Until Done key pressed:
       Activate Preview window
       On CopyPaste key pressed:
           Copy current selection from Preview
           Paste to destination doc
           Insert two newlines

The goal is speed up the many copy/paste operations that I'm now doing by Cmd-C, click destination window, Cmd-V, Enter, Enter, click Preview (so I can make the next selection).

Best Answer

The following is working for me in OS X 10.9:

In ~/bin/preview2chrome.scpt,

tell application "Preview" to activate
tell application "System Events"
    tell process "Preview"
        keystroke "c" using command down
    end tell
end tell
delay 0.5

tell application "Google Chrome" to activate
tell application "System Events"
    tell process "Chrome"
        keystroke "v" using command down
        keystroke return
        keystroke return
    end tell
end tell

delay 0.5
tell application "Preview" to activate

In ~/Library/Services, a Run Shell Script service named Preview2Chrome.workflow containing:

/usr/bin/osascript /Users/mellis/bin/preview2chrome.scpt

Option-V assigned as a shortcut key for Preview2Chrome.workflow .

This solution requires manually opening the destination Google Doc and making it the active tab in an active Chrome window with all other Chrome windows minimized. That's not much of a restriction for my purposes, but it would be nice to have an solution that knows how to paste to the destination doc no matter what tabs and windows are active.