Last active
April 13, 2018 10:47
-
-
Save crmne/8481724 to your computer and use it in GitHub Desktop.
Converts currencies using Google Finance. Created specifically for LaunchBar, just put it in `~/Library/Application Support/LaunchBar/Actions`. It accepts input in this form: `<amount> <from currency> […] <to currency>`. So for example all of those strings will result in the same conversion: "60 gbp to eur" "60 gbp in eur" "60 gbp eur". It will …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on parse_input_string(theString) | |
set AppleScript's text item delimiters to " " | |
set theFromAmount to text item 1 of theString as text | |
if length of text items of theString is greater than 1 then | |
set theFromCurrency to text item 2 of theString as text | |
set theToCurrency to text item -1 of theString as text | |
set currenciesGiven to true | |
else | |
set theFromCurrency to "USD" | |
set theToCurrency to "EUR" | |
set currenciesGiven to false | |
end if | |
return {fromAmount:theFromAmount, fromCurrency:theFromCurrency, toCurrency:theToCurrency, areCurrenciesGiven:currenciesGiven} | |
end parse_input_string | |
on currency_convert(fromAmount, fromCurrency, toCurrency, currenciesGiven) | |
set theURL to "http://www.google.com/finance/converter?a=" & fromAmount & "&from=" & fromCurrency & "&to=" & toCurrency | |
if currenciesGiven then | |
set theShellScript to "curl " & quoted form of theURL & " 2> /dev/null | xmllint --html --xpath \"//div[@id='currency_converter_result']/span/text()\" - 2> /dev/null" | |
set AppleScript's text item delimiters to " " | |
return text item 1 of (do shell script theShellScript) | |
else | |
open location theURL | |
return | |
end if | |
end currency_convert | |
on handle_string(theString) | |
set parsedData to my parse_input_string(theString) | |
set toAmount to my currency_convert(fromAmount of parsedData, fromCurrency of parsedData, toCurrency of parsedData, areCurrenciesGiven of parsedData) | |
if areCurrenciesGiven of parsedData then | |
tell application "LaunchBar" | |
set theMessage to toAmount & " " & toCurrency of parsedData | |
set theTitle to (fromAmount of parsedData & " " & fromCurrency of parsedData & " = ") as text | |
display in large type theMessage with title theTitle | |
end tell | |
end if | |
end handle_string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment