Skip to content

Instantly share code, notes, and snippets.

@cmikeb1
Created June 9, 2017 01:12
Show Gist options
  • Save cmikeb1/92ba8c53caa4519af0c4a3f787fbd885 to your computer and use it in GitHub Desktop.
Save cmikeb1/92ba8c53caa4519af0c4a3f787fbd885 to your computer and use it in GitHub Desktop.
AppleScript to retrieve crypto currency prices
validate_sheet()
set coins to {}
-- retrieve the list of coins from number
tell application "Numbers"
tell table 1 of sheet 1 of document 1
set cntRow to count row
set cntCol to count column
repeat with curRow from 1 to cntRow
if curRow mod 2 is not 0 then
set tmpVal to value of cell curRow of column 1
set the end of the coins to {|name|:tmpVal, price:null}
end if
end repeat --curRow
end tell --document
end tell -- application
-- get the value for each coin
repeat with coin in coins
set BASE_URL to "https://api.cryptonator.com/api/ticker/"
set BASE_CURRENCY to "usd"
set coinUrl to BASE_URL & (|name| of coin) & "-" & BASE_CURRENCY
tell application "JSON Helper"
set response to fetch JSON from coinUrl
end tell
if success of response is not false then
set coinPrice to price of ticker of response
else
set coinPrice to "error"
end if
set price of coin to coinPrice
end repeat
-- update values in numbers
tell application "Numbers"
tell table 1 of sheet 1 of document 1
set cntRow to count row
set cntCol to count column
repeat with curRow from 1 to cntRow
set coinName to value of cell curRow of column 1
repeat with coin in coins
if |name| of coin is equal to coinName then
if price of coin is not equal to "error" then
set value of cell curRow of column 2 to price of coin
set value of cell curRow of column 3 to (get current date)
end if
end if
end repeat
end repeat --curRow
end tell --document
end tell -- application
return
on validate_sheet()
tell application "Numbers"
if not (exists document 1) then error number 1000
-- validate sheet
set curSheet to sheet 1 of document 1
set curSheetName to curSheet's name
if curSheetName is not "Overview" then
error "Incorrect Sheet: " & curSheetName
end if
-- validate table
set curTable to table 1 of curSheet
set curTableName to curTable's name
if curTableName is not "Coin Values (USD)" then
error "Incorrect Table: " & curTableName
end if
end tell
end validate_sheet
@userid6789
Copy link

Got the original script to work as directed for Numbers on Mac OS with the included cryptonator api. Thank You! Would you be able to edit this to work with https://api.coingecko.com/api/v3/simple/supported_vs_currencies for simple price fetching in usd? Trying to learn but I keep getting sheet validation error in the Apple script editor when I try and do it myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment