Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.