You can shoot an exchange rate directly into a Numbers cell, where a spreadsheet formula could then access it elsewhere in the sheet.
The following is an AppleScript to do that, and which uses Ruby to request and return the exchange rate from Yahoo. The assumption of the AppleScript is that you tell it what cell you want the exchange rate dumped into, and that you already have the Numbers spreadsheet open.
The Ruby code was borrowed from Github with all attribution intended.
The request to Yahoo returns a CSV array as [[S, l1, d1, t1, b, a]] where just the last trade price is captured (e.g. [1]). You can reference these and other symbols here:
- s = symbol
- l1 = last trade price *
- d1 = last trade date
- t1 = last trade time
- b = bid price
- a = ask price
- Launch your Script Editor (Launchpad : Other : Script Editor)
- Copy and paste the following code into your Script Editor
- Click the hammer icon to compile it
- Click the adjust arrow button to run it
- Save a copy of the script as (e.g. yahoo_exchg.applescript
Script Editor : File menu : Save…
- Save As: yahoo_exchg
- Location: Documents
- File Format: Text (this will add .applescript to the Save As filename
- Hide Extension: Unchecked
- Save
- Save another copy of rthe script as an application (e.g. yahooX)
Option key + Script Editor : File menu : Save As…
- Save As: yahooX
- Location: Desktop
- File Format: Application (adds .app to yahooX)
- Hide Extension: Checked
- Save
- Double-click the yahoox application on your Desktop to use.
AppleScript code
--
-- you can add to the list, and if you do not choose and click "OK", then you will
-- be prompted to enter the exchange string
property exchg_list : {"AUDUSD", "USDAUD", "EURUSD", "USDEUR", "GBPUSD", "USDGBP"}
set exchg_select to (choose from listexchg_listwith prompt ¬
"Select your Exchange Conversion" default items "" with empty selection allowed without multiple selections allowed)
if exchg_select is false then error number -128
if exchg_select is {} then
display dialog "Enter exchange symbols as fromto (e.g. AUDUSD)" default answer "" -- fallback if not in list
set new_exchg to text returned of result
if new_exchg is not {} and length of new_exchg = 6 then
set exchg_select to new_exchg
else
display alert "Need an exchange pair ... quitting." giving up after 10
return
end if
end if
set exchg_value to yahoo_exchange(exchg_select) as number
tell application "Numbers"
tell active sheet of front document
tell table 1
set myRange to range "A2:A2"
set value of cells of myRange to exchg_value
end tell
end tell
end tell
return
on yahoo_exchange(astring)
return do shell script "ruby <<'EOF' - " & astring & "
#!/usr/bin/ruby
# coding: utf-8
# Influenced by https://gist.github.com/captainpete/1639522
require 'csv'
exchg = ARGV.join ' '
# get Yahoo Exchange rate and return last trade price
class YahooExchange
# set the default
def self.fetch(code = 'AUDUSA')
acsv = %x(curl -q -s \"http://download.finance.yahoo.com/d/quotes.csv?s=#{code}=X&f=sl1d1t1ba&e=.csv\")
CSV.parse(acsv).flatten[1]
end
end
print YahooExchange.fetch(exchg)
EOF"
end yahoo_exchange