Node.js 如何使此applescript正常工作?

Node.js 如何使此applescript正常工作?,node.js,applescript,osx-yosemite,automator,url-shortener,Node.js,Applescript,Osx Yosemite,Automator,Url Shortener,因此,这个小转移的目标是能够选择一段包含URL的文本,并让OS X使用goo.gl URL shortener服务将其转换为短URL 为此,我使用Automator创建了一个服务,它接受文本输入并将输入传递给“RunAppleScript”操作。下面列出了AppleScript 我已经安装了node.js v0.10.33,还安装了一个名为json()的节点包 只要我不将输出传输到json节点应用程序,下面的脚本就可以正常工作。 curl命令通过管道连接到json节点应用程序,在Terminal

因此,这个小转移的目标是能够选择一段包含URL的文本,并让OS X使用goo.gl URL shortener服务将其转换为短URL

为此,我使用Automator创建了一个服务,它接受文本输入并将输入传递给“RunAppleScript”操作。下面列出了AppleScript

我已经安装了node.js v0.10.33,还安装了一个名为json()的节点包

只要我不将输出传输到json节点应用程序,下面的脚本就可以正常工作。
curl命令通过管道连接到json节点应用程序,在Terminal中非常有效

我的猜测是,shell环境的某些功能已关闭,但我不知道如何查看它。 有什么帮助吗

-- shorten URLs via goo.gl URL shortener
-- syntax derrived from Scott Lowe @ blog.scottlowe.org

on run {input, parameters}

    set curlCommand to "curl https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{\"longUrl\": \"" & input & "\"}' | /usr/local/bin/json id"

    set jsonResult to (do shell script curlCommand)

    return jsonResult
end run
为什么有“返回命令”?你真的不想“返回jsonResult”吗

诚然,我对json一无所知,但curl命令的结果是一个字符串,我知道如何在applescript中解析字符串。下面是我将如何编写代码,将其解析为字符串。注:我使用此网页的url作为“输入”


在Applescript中处理JSON的一个很好的工具是免费的AppJSON助手(AppStore)。它将JSON转换为Applescript字典。因此,我的答案与@regulus6633 post非常(非常)相似,但没有对JSON进行文本解析:

set input to "http://stackoverflow.com/questions/26757656/how-to-get-this-applescript-working"

-- Note: without piping to grep at the end!
set curlCommand to "curl https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{\"longUrl\": \"" & input & "\"}'"

-- getting the full JSON answer
set jsonResult to do shell script curlCommand

-- using JSON Helper to translate JSON to Applescript dictionary
tell application "JSON Helper"
    set shortURL to |id| of (read JSON from jsonResult)
end tell

return shortURL

您好,迈克尔/汉堡

为这场混乱道歉。您是对的,我确实想返回jsonResult。作为调试过程的一部分,我返回了curl命令,但忘了将其更改回去。谢谢,我将检查此项。对于我自己的启发,我想知道为什么我写的版本不起作用——但当我有更多的时间时,我可以尝试一下:)
set input to "http://stackoverflow.com/questions/26757656/how-to-get-this-applescript-working"

-- Note: without piping to grep at the end!
set curlCommand to "curl https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{\"longUrl\": \"" & input & "\"}'"

-- getting the full JSON answer
set jsonResult to do shell script curlCommand

-- using JSON Helper to translate JSON to Applescript dictionary
tell application "JSON Helper"
    set shortURL to |id| of (read JSON from jsonResult)
end tell

return shortURL