AppleScript 无法获得 api 响应的引用形式
AppleScript can't get quoted form of api response
我今天早些时候制作了一个 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 returnNumbersInString
returnNumbersInString(apiResponse)
损坏的可自定义代码
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 nameFilter
set AppleScript's text item delimiters to {"."}
set nameFilter to the text items of channelName
set channelName to item 1 of nameFilter
set 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 returnNumbersInString
returnNumbersInString(apiResponse)
每次我做第二个时它输出错误
Can’t get quoted form of {"{
\"items\": [
{
\"statistics\": {
\"subscriberCount\": \"76957805\"
}
}
]
}"}.
它从网站获取信息后立即失败,这没有任何意义,因为 none 获取网站 link 之外的代码已更改,任何人都可以帮我解决这个问题
您已将 do shell script
命令括在此处的大括号中:
set apiResponse to {do shell script curlCommand}
因此,apiResponse
现在是一个包含 JSON 字符串的列表,而不是简单的 JSON 字符串。删除大括号,使该行显示为:
set apiResponse to do shell script curlCommand
我今天早些时候制作了一个 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 returnNumbersInString
returnNumbersInString(apiResponse)
损坏的可自定义代码
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 nameFilter
set AppleScript's text item delimiters to {"."}
set nameFilter to the text items of channelName
set channelName to item 1 of nameFilter
set 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 returnNumbersInString
returnNumbersInString(apiResponse)
每次我做第二个时它输出错误
Can’t get quoted form of {"{
\"items\": [
{
\"statistics\": {
\"subscriberCount\": \"76957805\"
}
}
]
}"}.
它从网站获取信息后立即失败,这没有任何意义,因为 none 获取网站 link 之外的代码已更改,任何人都可以帮我解决这个问题
您已将 do shell script
命令括在此处的大括号中:
set apiResponse to {do shell script curlCommand}
因此,apiResponse
现在是一个包含 JSON 字符串的列表,而不是简单的 JSON 字符串。删除大括号,使该行显示为:
set apiResponse to do shell script curlCommand