0

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.

JP.
  • 5,536
  • 7
  • 58
  • 100
  • I don't know of command line tools that can interact with the system clipboard for images. If one existed then I would think that would just work normally. Barring that you are going to need to save the image locally and then use something that can tell the application to import an image file (applescript/etc.). I'm not enough of an OS X person to be able to give specific suggestions though. – Etan Reisner Mar 20 '15 at 22:19
  • Can you clarify the question? I did not completely understood you. Can you specify in which kind of document do you want to paste your binary clipboard and in which form? If you need only a way to access to the binary data you can take more than inspiration from [the source of pngpaste](http://github.com/jcsalterego/pngpaste), or if you save the picture on disk you can always `cat` it appending or inserting where it is needed. – Hastur Mar 23 '15 at 10:15
  • @Hastur. I want to paste the binary into a word/pages/etc. document – JP. Mar 25 '15 at 17:00

1 Answers1

3

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.

Community
  • 1
  • 1
Russ Savage
  • 598
  • 4
  • 20
  • So close! I'm going to leave the bounty open, in case anyone comes up with a way to paste the image without command+v. If there are no takers I'll award you the bounty. Thanks! – JP. Mar 24 '15 at 14:35