Skip to content

Instantly share code, notes, and snippets.

@cmikeb1
Created June 9, 2017 01:12
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • 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
@guigui38github
Copy link

Hi,

Love your code, works well for me, I tried the same thing using coingecko's api… since I'm an absolute beginner in apple script I get stuck there
`-- get the value for each coin
repeat with coin in coins
set BASE_URL to "https://api.coingecko.com/api/v3/simple/price?ids="
set BASE_CURRENCY to "eur"
set coinUrl to BASE_URL & (|name| of coin) & "&vs_currencies=eur"

tell application "JSON Helper"
	set response to fetch JSON from coinUrl
end tell

if success of response is not false then
	set coinPrice to eur of (|name| of coin) of response
else
	set coinPrice to "error"
end if

set coin_price of coin to coinPrice

end repeat`

In particular at
set coinPrice to eur of (|name| of coin) of response
I think it's because the value of the variable is not accessed there, don't know why… Could you please help me with this (of course I researched about this bug on the internet), the big advantage of this api is that we can get any token we want..

There you will find the entire code
`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 2 to cntRow + 1
		if curRow is not cntRow + 1 then
			set tmpVal to value of cell curRow of column 1
			set the end of the coins to {|name|:tmpVal, coin_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.coingecko.com/api/v3/simple/price?ids="
set BASE_CURRENCY to "eur"
set coinUrl to BASE_URL & (|name| of coin) & "&vs_currencies=eur"

tell application "JSON Helper"
	set response to fetch JSON from coinUrl
end tell

if success of response is not false then
	set coinPrice to eur of (|name| of coin) of response
else
	set coinPrice to "error"
end if

set coin_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 coin_price of coin is not equal to "error" then
					set value of cell curRow of column 2 to coin_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 (EUR)" then
		error "Incorrect Table: " & curTableName
	end if
	
end tell

end validate_sheet`

Thanks a lot for your work

@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