Thanks to both of you for your help!
This morning I decided to tweak the script to display the information neatly, the way I had envisioned it. I've copied the result below. The script now displays a message on the screen with the source and destination currencies in full, but cmd-c copies only the converted amount, limited to 2 decimals too. The code may not be very elegant. Feel free to tweak. It works well for the currencies I convert between frequently (EUR, USD, GBP, THB, SGD) but I have not done any extensive testing.
I'll also be putting it up on GitHub at some point, with credit given to you both. Let me know if you object to this, as I would include some of the code you wrote.
This morning I decided to tweak the script to display the information neatly, the way I had envisioned it. I've copied the result below. The script now displays a message on the screen with the source and destination currencies in full, but cmd-c copies only the converted amount, limited to 2 decimals too. The code may not be very elegant. Feel free to tweak. It works well for the currencies I convert between frequently (EUR, USD, GBP, THB, SGD) but I have not done any extensive testing.
- # =============================================================================
# Currency conversion - v20130312
# -----------------------------------------------------------------------------
# This script is meant to be launched from LaunchBar, taking an input query in
# the format ##src dst and asking Google to convert ## number of src currency
# units into dst currency units. The script will limit the output currency
# units to 2 decimals. Use ISO 4217 codes for source and destination
# indication.
#
# Thanks to iRounak and CapnAverage on the obdev.at forum for their help in
# getting this off the ground.
# -----------------------------------------------------------------------------
# Further development:
# - allow input with space between amount and source currency
# - elegant error handling and reporting
# -----------------------------------------------------------------------------
on handle_string(theInput)
try
# stuff iRounak wrote
set AppleScript's text item delimiters to " "
set theFirstCurrency to text item 1 of theInput as text
set theSecondCurrency to text item -1 of theInput
set theURL to quoted form of ("http://www.google.com/ig/calculator?hl=en&q=" & theFirstCurrency & "=?" & theSecondCurrency)
set theSource to (do shell script "curl " & theURL) as string
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "†"
set theSource to text items of theSource
set AppleScript's text item delimiters to ""
set theSource to theSource as text
# grab the 2 important bits from the google-returned string
set AppleScript's text item delimiters to ","
set gsFrom to text item 1 of theSource
set gsTo to text item 2 of theSource
# crop those important bits
set AppleScript's text item delimiters to "\""
set gsFrom to text item 2 of gsFrom
set gsTo to text item 2 of gsTo
# split the important bits into their 2 components, the amount and the currency name
set AppleScript's text item delimiters to " "
set gsFromAmt to text item 1 of gsFrom
set gsFromCurr to text items 2 through (count text items of gsFrom) of gsFrom
set gsToAmt to text item 1 of gsTo
set gsToCurr to text items 2 through (count text items of gsTo) of gsTo
# limit the result amount to 2 decimals
set the_offset to offset of "." in gsToAmt
set the_offset to the_offset + 2
set gsToAmt to text 1 through the_offset of gsToAmt
# display the message
tell application "LaunchBar"
set theMessage to gsToAmt
set theTitle to gsFromAmt & " " & gsFromCurr & " in " & gsToCurr
set selection as text to theMessage
display in large type theMessage with title theTitle
end tell
end try
end handle_string
I'll also be putting it up on GitHub at some point, with credit given to you both. Let me know if you object to this, as I would include some of the code you wrote.