AppleScript 不工作,安装了 JSON 助手
AppleScript does not work, JSON Helper installed
我安装了 JSON Helper,但我似乎无法让这个脚本工作。
set myjson to (do shell script "curl http://api.ethplorer.io/getAddressInfo/0x3E306757A30fa6cB302109D11646428e20a91369?apiKey=freekey")
tell application "JSON Helper" to set mydata to (read JSON from myjson)
set value1 to balance of (item 1 of tokens of mydata)
set myjson to (do shell script "curl https://api.coinmarketcap.com/v1/ticker/streamr-datacoin/")
tell application "JSON Helper" to set mydata to (read JSON from myjson)
set value2 to price_usd of item 1 of mydata
set total to value1 * value2
return total
我的问题在于返回值 2 时出现错误:Can’t make "0.0246031406" into type number.
如果我记得以前使用 JSON Helper
,它有一个特定的命令来调用远程 API 端点。目前,您正在通过创建 shell 进程来使用 cURL
执行此操作。这并没有什么大错,如果我在终端机上,我肯定会这样做。
但是,除了使用 do shell script
的额外开销之外,这样做会产生一个额外的必要步骤:call to <API> -> receive <response> as text -> convert <response> to record
.
如果您改用 fetch JSON from
,则无需调出 shell,并且会消除工作流程中的一个步骤:call to <API> -> receive <response> as record
.
property url1 : "http://api.ethplorer.io/getAddressInfo/0x3E306757A30fa6cB302109D11646428e20a91369?apiKey=freekey"
property url2 : "https://api.coinmarketcap.com/v1/ticker/streamr-datacoin/"
-- First API call
tell application "JSON Helper" to fetch JSON from url1
set value1 to the contents of the result's tokens's 1st item's balance
-- Second API call
tell application "JSON Helper" to fetch JSON from url2
set value2 to contents of the result's 1st item's price_usd
set amount to (value1 as number) * (value2 as number)
return the amount
或者,您也可以取消 JSON Helper
,并使用 AppleScript-Objecive-C 桥调用 API 并解析JSON:
#!/usr/bin/env osascript
use framework "Foundation"
use scripting additions
property url1 : "http://api.ethplorer.io/getAddressInfo/0x3E306757A30fa6cB302109D11646428e20a91369?apiKey=freekey"
property url2 : "https://api.coinmarketcap.com/v1/ticker/streamr-datacoin/"
to numberFromString:(nmbrString as text)
local nmbrString
tell alloc() of NSNumberFormatter ¬
of the current application
init()
setNumberStyle_(1) -- decmial
numberFromString_(nmbrString)
return result's floatValue()
end tell
end numberFromString:
to unwrap(nsObject)
local nsObject
tell current application to tell its NSArray to ¬
tell the arrayWithObject_(nsObject) to ¬
get the 1st item in (it as list)
end unwrap
on response from api for keypath as list : ["@self"]
local api, keypath
script
property _ : a reference to missing value
property a : URLWithString_(api) of my NSURL
property b : dataWithContentsOfURL_(a) of my NSData
property c : (my NSJSONSerialization)'s ¬
JSONObjectWithData:b ¬
options:0 |error|:_
property list : arrayWithArray_(keypath) of my NSArray
property key : componentsJoinedByString_(".") of my list
property value : the valueForKeyPath_(key) of c
end script
end response
set value1 to value of the (response from url1 for ["tokens", "balance", "@firstObject"])
set value2 to value of the (response from url2 for "price_usd.@firstObject")
return {value1 as real, value2 as real}
将这两个值相乘太多,删除return
语句,然后:
set amount to (value 1 as real) * (value2 as real)
将 amount
四舍五入到小数点后两位:
set amount_2dp to (round (amount * 100)) / 100
我安装了 JSON Helper,但我似乎无法让这个脚本工作。
set myjson to (do shell script "curl http://api.ethplorer.io/getAddressInfo/0x3E306757A30fa6cB302109D11646428e20a91369?apiKey=freekey")
tell application "JSON Helper" to set mydata to (read JSON from myjson)
set value1 to balance of (item 1 of tokens of mydata)
set myjson to (do shell script "curl https://api.coinmarketcap.com/v1/ticker/streamr-datacoin/")
tell application "JSON Helper" to set mydata to (read JSON from myjson)
set value2 to price_usd of item 1 of mydata
set total to value1 * value2
return total
我的问题在于返回值 2 时出现错误:Can’t make "0.0246031406" into type number.
如果我记得以前使用 JSON Helper
,它有一个特定的命令来调用远程 API 端点。目前,您正在通过创建 shell 进程来使用 cURL
执行此操作。这并没有什么大错,如果我在终端机上,我肯定会这样做。
但是,除了使用 do shell script
的额外开销之外,这样做会产生一个额外的必要步骤:call to <API> -> receive <response> as text -> convert <response> to record
.
如果您改用 fetch JSON from
,则无需调出 shell,并且会消除工作流程中的一个步骤:call to <API> -> receive <response> as record
.
property url1 : "http://api.ethplorer.io/getAddressInfo/0x3E306757A30fa6cB302109D11646428e20a91369?apiKey=freekey"
property url2 : "https://api.coinmarketcap.com/v1/ticker/streamr-datacoin/"
-- First API call
tell application "JSON Helper" to fetch JSON from url1
set value1 to the contents of the result's tokens's 1st item's balance
-- Second API call
tell application "JSON Helper" to fetch JSON from url2
set value2 to contents of the result's 1st item's price_usd
set amount to (value1 as number) * (value2 as number)
return the amount
或者,您也可以取消 JSON Helper
,并使用 AppleScript-Objecive-C 桥调用 API 并解析JSON:
#!/usr/bin/env osascript
use framework "Foundation"
use scripting additions
property url1 : "http://api.ethplorer.io/getAddressInfo/0x3E306757A30fa6cB302109D11646428e20a91369?apiKey=freekey"
property url2 : "https://api.coinmarketcap.com/v1/ticker/streamr-datacoin/"
to numberFromString:(nmbrString as text)
local nmbrString
tell alloc() of NSNumberFormatter ¬
of the current application
init()
setNumberStyle_(1) -- decmial
numberFromString_(nmbrString)
return result's floatValue()
end tell
end numberFromString:
to unwrap(nsObject)
local nsObject
tell current application to tell its NSArray to ¬
tell the arrayWithObject_(nsObject) to ¬
get the 1st item in (it as list)
end unwrap
on response from api for keypath as list : ["@self"]
local api, keypath
script
property _ : a reference to missing value
property a : URLWithString_(api) of my NSURL
property b : dataWithContentsOfURL_(a) of my NSData
property c : (my NSJSONSerialization)'s ¬
JSONObjectWithData:b ¬
options:0 |error|:_
property list : arrayWithArray_(keypath) of my NSArray
property key : componentsJoinedByString_(".") of my list
property value : the valueForKeyPath_(key) of c
end script
end response
set value1 to value of the (response from url1 for ["tokens", "balance", "@firstObject"])
set value2 to value of the (response from url2 for "price_usd.@firstObject")
return {value1 as real, value2 as real}
将这两个值相乘太多,删除return
语句,然后:
set amount to (value 1 as real) * (value2 as real)
将 amount
四舍五入到小数点后两位:
set amount_2dp to (round (amount * 100)) / 100