Skip to content

Instantly share code, notes, and snippets.

@mellet
Last active July 18, 2018 20:55
Show Gist options
  • Save mellet/a073a9c3c05f3f81c9ea703a0916491e to your computer and use it in GitHub Desktop.
Save mellet/a073a9c3c05f3f81c9ea703a0916491e to your computer and use it in GitHub Desktop.
BetterTouchTools show crypto value

Setup BetterTouchTools

  1. Install BetterTouchTools and "JSON Helper for Apple Script" from the App Store.
  2. Set up a widget inside BetterTouchTools
  • Global -> Click +Widget
  • In Select toucbar wigdet pick the option Run Apple Script and Show Return Value
  • Click Advanced Configuration
  • Paste the script and click Run Script to validate that it works.
-- Settings
set coinName to "bitcoin" -- Name of coin
set currencry to "usd" -- Currency (usd or eur)"
set showChange to false -- Should show percentage change
set roundingDeliminator to 2 -- Number of numbers after the deliminator
-- End of settings

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on roundThis(n, numDecimals)
	set x to 10 ^ numDecimals
	(((n * x) + 0.5) div 1) / x
end roundThis

tell application "JSON Helper"
	set fetch_api to fetch JSON from "https://api.coinmarketcap.com/v1/ticker/" & coinName & "/?convert=EUR" with cleaning feed
end tell

set change to percent_change_24h of first item of fetch_api

if currencry = "usd" then
	set raw_usd_price to price_usd of first item of fetch_api
	set fix_deliminator to replace_chars(raw_usd_price, ".", ",") as number
	set rounded to roundThis(fix_deliminator, roundingDeliminator)
	set price to "$" & (rounded as inches as string)
end if

if currencry = "eur" then
	set raw_eur_price to price_eur of first item of fetch_api
	set fix_deliminator to replace_chars(raw_eur_price, ".", ",") as number
	set rounded to roundThis(fix_deliminator, roundingDeliminator)
	set price to "" & (rounded as inches as string)
end if

if showChange then
	return price & " [" & change & "%]"
end if

return price
  1. Tweak the settings
  • Set a name
  • Set Execute this script every x seconds to 60 seconds or something to avoid spaming the network
  • Pick the TouchBar button color
  • Set a icon by dragging a image over the Icon slot. Here are some nice icons.
  1. Rince and repeat for as many coins you want.

Settings

At the top of the script there are some values you can change to set another coin, set the currency and how many numbers after the deliminator (in case the coin is worth less than a dollar).

How to find coin name
You can find the coinName from the url from coinmarketcap https://coinmarketcap.com/currencies/{coinName}/

@AndenIDK
Copy link

Great script, I do do often get many more numbers than 2 after the deliminator with my EOS currency (but not all the time) :) Cant spot the error though

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