Search code examples
bashshelltextexpander

TextExpander, Shell Script, Paste Image


I'm creating a text expansion snippet to pull stock information from a website. I'd like it to look up the ticker currently on the clipboard, go to said website and pull down the required stock chart and paste it into the document I am working on. I can download the image as follows:

curl -s -o ticker.png http://example.com/ticker-chart.aspx?t=%clipboard

I'm not sure how to use text expander/bash to copy the saved image into my document. pbcopy/pbpaste only seem to work with text. Any help would be appreciated.


Solution

  • After messing around with this, the best I could do was create a script that copied the image to the clipboard. After typing the command, you still have to hit command+v to paste it into the document you are working on.

    First you need something to copy the image file to the clipboard. I found some code here: http://www.alecjacobson.com/weblog/?p=3816 that seemed to do the trick. I put it into a gist to make it easier to consume. Open a new terminal and run the following commands:

    curl -L -o 'impbcopy2.m' 'https://gist.githubusercontent.com/russorat/2635e2904caadaa12825/raw/aefb2239ea98e56a1cfa55c3ae4c7a84c8aa7d78/impbcopy.m'
    gcc -Wall -g -O3 -ObjC -framework Foundation -framework AppKit -o impbcopy impbcopy.m
    

    If you are missing gcc, follow the instructions here to install it first: How to use/install gcc on Mac OS X 10.8 / Xcode 4.4

    Now, copy the impbcopy file you just created to your system path to make it available for Text Expander:

    sudo mv impbcopy /usr/bin/
    

    Now, in Text Expander, create a new New Snippet, make sure the Content is set for "Shell Script", and enter the following code:

    #!/bin/bash
    filePath='/tmp/stock.png'
    stockTicker="$(pbpaste)"
    curl -L -o "$filePath" "http://chart.finance.yahoo.com/z?s=$stockTicker&t=1d&q=l&l=on&z=l&a=v&p=s&lang=en-US&region=US#.png"
    /usr/bin/impbcopy "$filePath"
    

    I'm using my own stock link here. For the one you provided, change the curl line to:

    curl -L -o "$filePath" "http://example.com/ticker-chart.aspx?t=$stockTicker"
    

    Now, in your document, enter AMZN, copy it to the clipboard, the type your abbreviation. After the "swoosh" sound, you should be able to hit command+v and have a nice image inserted into your document.