Currency Conversion with TextExpander

Philip Blog, TextExpander, TextExpander Tips

Adrian Boioglu asked on Twitter:

We’ll need four things to make a currency conversion snippet:

1. Exchange Rate Data Service

OpenExchangeRates.org offers several paid levels of service and a free service limited to 1,000 queries per month. Sign up for one of those, and make note of your API key. You’ll need that later.

2. An app to process the service results

Download the free JSON Helper for AppleScript from the Mac App Store.

3. An AppleScript snippet for TextExpander

  1. Choose File -> New Snippet
  2. Set the Content: popup above the snippet editor to: AppleScript
  3. Copy and paste the AppleScript, which appears with a light gray background below, into the snippet editor
  4. Change the XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX to your API key from step #1
  5. Set your abbreviation to: convert_to_euros

set openExchnageRatesAppID to “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”

set baseCurrency to “USD”

set baseAmount to (the clipboard as string)

 

— Test the input

if (first character of baseAmount is not number) then

set baseAmount to (characters 2 thru ((length of baseAmount) – 1) of baseAmount) as string

end if

try

set baseAmount to baseAmount as number

on error

set baseAmount to 0

end try

if baseAmount is 0 then

return “Problem with clipboard content. Use a number or a number preceded by a currency symbol.”

end if

 

— Get the exchange rate; change |EUR| to another currency for different exchange rates

tell application “JSON Helper”

set theURL to “http://openexchangerates.org/api/latest.json?app_id=” & openExchnageRatesAppID & “&base=” & baseCurrency

set jsonResult to fetch JSON from theURL

set exchangeRates to rates of jsonResult

set convertedAmount to (|EUR| of exchangeRates) * baseAmount

return roundToDecimal(convertedAmount, 2) of me

end tell

 

on roundToDecimal(numberToRound, precision)

set multiplier to 10 ^ precision

numberToRound * multiplier

round result

return result / multiplier

end roundToDecimal

 

4. A plain text snippet to format the result

  1. Choose File -> New Snippet
  2. Copy and paste the following into the snippet editor:
    (€%snippet:convert_to_euros%)
  3. Set your abbreviation to “ineuros”

Test it out

Copy this dollar amount to the clipboard, then type a space followed by: ineuros

$34.95

Your result will depend on the current exchange rate but should be approximately:

$34.95 (€26.07)

Notes

To change the base currency, change “USD” in the second line of the AppleScript snippet from step 3 to another currency, such as “JPY” for Japanese Yen.

To change the target currency, change “|EUR|” about 2/3 down the script to another currency, such as “|MXN|” for the Mexican Peso.

AppleScript’s localized number handling is somewhat limited, so you may need to customize the script if you use a decimal separator other than ‘.’ or place your currency symbol at the end instead of the beginning.

Here is the list of currencies supported by OpenExchangeRate.org.