2014-11-05 3 views
0

그래서이 작은 전환의 목표는 URL이 포함 된 텍스트를 선택하고 OS X에서 goo.gl URL 단축키 서비스를 사용하여 짧은 URL로 변환하도록하는 것입니다.이 AppleScript를 작동시키는 방법은 무엇입니까?

이 목적을 위해 필자는 텍스트 입력을 받아들이고 "AppleScript 실행"작업에 해당 입력을 전달하는 Automator를 사용하여 서비스를 만들었습니다. AppleScript는 다음과 같습니다.

나는 Node.js를에게 v0.10.33을 설치하고 또한 노드 패키지라고 JSON (http://trentm.com/json/)

아래 스크립트는 한 내가 JSON 노드 응용 프로그램의 출력 파이프를하지 않는 한 잘 작동을 설치했습니다.
json 노드 앱에 대한 파이핑을 사용하는 컬 명령 이 터미널에서 완벽하게 작동합니다.

내 생각 엔 셸 환경이 무언가이지만, 어떻게 조사해야할지 모르겠다. 도움이 되었습니까?

-- 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 

답변

1

경우 애플 JSON을 처리하기위한 훌륭한 도구는 무료 앱 JSON 도우미 (앱 스토어)입니다 ... 내가 "입력"으로이 웹 페이지의 URL을 사용합니다. JSON을 Applescript 사전으로 변환합니다. 그래서 내 대답은 @ regulus6633 게시물에 대한 아주 (매우)과 유사하지만, 텍스트없이 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 

인사말, 마이클/함부르크 혼란에 대한

+0

고마워요, 제가 이것을 살펴 보겠습니다. 내 자신의 덕목으로 내가 쓴 버전이 왜 작동하지 않는지 알고 싶습니다. -하지만 더 많은 시간이있을 때 나는 해킹 할 수 있습니다 :) –

+0

이것은 완벽하게 작동합니다. 고맙습니다! –

1

왜 "curlCommand를 반환합니까?" 정말로 "return jsonResult"를 원하지 않습니까?

필자는 json에 대해 아무 것도 몰라요. 그러나 curl 명령의 결과는 문자열이고 나는 applescript에서 문자열을 구문 분석하는 방법을 알고 있습니다. 다음은 코드를 문자열로 파싱하는 방법을 설명합니다.

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

set curlCommand to "curl https://www.googleapis.com/urlshortener/v1/url -H 'Content-Type: application/json' -d '{\"longUrl\": \"" & input & "\"}' | grep '\"id\"'" 
set jsonResult to do shell script curlCommand 
set shortURL to text 9 thru -2 of jsonResult 
return shortURL 
+0

appologies. 당신 말이 맞아요. jsonResult를 돌려주고 싶습니다. 내 디버깅 프로세스의 일부로 curlCommand를 반환하고 다시 변경하는 것을 잊었습니다. –

관련 문제