宝剑锋从磨砺出,梅花香自苦寒来。这篇文章主要讲述AppleScript无法获得api响应的引用形式相关的知识,希望能为你提供帮助。
我今天早些时候制作了一个AppleScript,它显示了Geektools的YouTube订阅者数量,但我希望人们可以更轻松地使用它并尝试使其不受文件名称的影响(例如,采用子计数 -PewDiePie.scpt和输出PewDiePie的子计数),我已经从文件名中输入了名称,但是当我尝试从api的响应中取出数字时,它给了我错误
工作(原件)的代码
set apiResponse to (do shell script "curl -s 'https://www.googleapis.com/youtube/v3/channels?part=statistics&
forUsername=PewDiePie&
fields=items%2Fstatistics%2FsubscriberCount&
key=AIzaSyAEQGj2ZcDrTU0ZqzteD8eDVJwB9cpmvEo'")on returnNumbersInString(inputString)
set s to quoted form of inputString
do shell script "sed s/[a-zA-Z\']//g <
<
<
" &
s
set dx to the result
set numlist to {}
repeat with i from 1 to count of words in dx
set this_item to word i of dx
try
set this_item to this_item as number
set the end of numlist to this_item
end try
end repeat
end returnNumbersInStringreturnNumbersInString(apiResponse)
【AppleScript无法获得api响应的引用形式】破坏的可定制代码
set channelName to path to me as text
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"subcount-"}
set nameFilter to text items of channelName
set channelName to item 2 of nameFilterset AppleScript's text item delimiters to {"."}
set nameFilter to the text items of channelName
set channelName to item 1 of nameFilterset curlLink to "https://www.googleapis.com/youtube/v3/channels?part=statistics&
forUsername=" &
channelName &
"&
fields=items%2Fstatistics%2FsubscriberCount&
key=AIzaSyAEQGj2ZcDrTU0ZqzteD8eDVJwB9cpmvEo"
set curlCommand to "curl -s " &
(quoted form of curlLink)set apiResponse to {do shell script curlCommand}
on returnNumbersInString(inputString)
set s to quoted form of inputString
do shell script "sed s/[a-zA-Z\']//g <
<
<
" &
s
set dx to the result
set numlist to {}
repeat with i from 1 to count of words in dx
set this_item to word i of dx
try
set this_item to this_item as number
set the end of numlist to this_item
end try
end repeat
end returnNumbersInStringreturnNumbersInString(apiResponse)
每次我做第二次,它输出错误
Can’t get quoted form of {"{
"items": [
{
"statistics": {
"subscriberCount": "76957805"
}
}
]
}"}.
它从网站上获取信息后立即失败,这没有任何意义,因为除了如何获得网站链接之外的任何代码都没有改变,任何人都可以帮我解决这个问题
答案你在这里用括号括起你的
do shell script
命令:set apiResponse to {do shell script curlCommand}
因此,
apiResponse
现在是一个包含JSON字符串的列表,而不仅仅是一个JSON字符串。删除大括号,使行显示:set apiResponse to do shell script curlCommand
推荐阅读
- 限制为Android时,YouTube API密钥会出现500错误
- YouTube Android Player API无法在xiaomi Redmi Note 3中使用
- 我们可以在React Kendo Grid Wrapper上添加虚拟滚动
- 带Gmail的Log4Net SMTP Appender
- 通过C#通过Google Apps帐户发送电子邮件
- 通过Google Apps向Django发送电子邮件时,为电子邮件帐户指定名称
- 如何接收电子邮件(节点app / ubuntu服务器[重复])
- 本文教你如何设置U盘打开
- 本文教你U盘打开uefi