2017-01-27 3 views
1

Finder에서 선택한 파일의 전체 경로를 가져 오려고합니다. 이 시점에서, 나는 많은 방법을 시도 :이 명령의Finder에서 선택한 파일의 posix 경로는 어떻게 얻을 수 있습니까?

>> Application('Finder').selection()[0] 
=> Application("Finder").startupDisk.folders.byName("Users").folders.byName("example").folders.byName("Tools").folders.byName("my-tools").documentFiles.byName("example.sh") 
>> 
>> Application('Finder').selection()[0].name() 
=> "example.sh" 
>> 
>> Application('Finder').selection()[0].toString() 
=> "[object ObjectSpecifier]" 
>> 
>> Application('Finder').selection()[0].posixPath() 
!! Error on line 1: Error: Can't get object. 
>> 
>> Application('Finder').selection()[0].path() 
!! Error on line 1: Error: Can't get object. 
>> 
>> Application('Finder').selection()[0].url() 
=> "file:///Users/example/Tools/my-tools/example.sh" 
>> 
>> Path(Application('Finder').selection()[0]) 
!! Error on line 1: Error: Can't get object. 
>> 
>> Path(Application('Finder').selection()[0]).toString() 
!! Error on line 1: Error: Can't get object. 

없음 /Users/example/Tools/my-tools/example.sh 인 POSIX 경로를 얻을 수 없습니다.

>> Application('Finder').selection()[0].url().replace(/^file:\/\//, '') 
=> "/Users/example/Tools/my-tools/example.sh" 

을하지만 그 해키 것, 그리고 직접 POSIX 경로를 얻을 수있는 명령이 있어야처럼 보인다 :

궁극적으로, 나는 단순히 URL의 전면 떨어져 file://을 제거 할 수 있습니다 알고 있습니다.

+0

또한 JXA 용 API 문서가있는 경우 누구든지 나를 가르쳐 주시면 감사하겠습니다. 나는 [JXA Cookbook] (https://github.com/dtinth/JXA-Cookbook/wiki)과 [JXA Release Notes] (https://developer.apple.com/library/content/releasenotes/InterapplicationCommunication /RN-JavaScriptForAutomation/Articles/OSX10-10.html), 그러나 모든 메소드와 각 객체에 존재하는 속성 (예 : 파일)을 보여주는 문서는 보이지 않습니다. – stiemannkj1

+1

JXA는 AppleScript처럼 자동화 가능한 응용 프로그램 및 스크립팅 확장 프로그램에서 제공하는 AppleEvent 정의를 사용합니다. 그러한 라이브러리에 대한 설명서를 보려면 스크립트 편집기를 열고 라이브러리 창을 표시하도록 설정하고 관련 응용 프로그램 또는 확장 (이 경우 Finder)을 찾은 다음 두 번 클릭하여 라이브러리 뷰어를 엽니 다. 문서를 표시 할 언어를 선택하는 드롭 다운이 있습니다. JXA를 주로 사용하는 경우 스크립트 편집기 환경 설정에서 JXA를 표준 언어로 설정할 수 있으며 문서는 적합해야합니다. – kopischke

답변

1

examin the file's properties()의 경우 전체 경로가 포함 된 유일한 속성은 url()입니다. 그러나, 특수 문자는 이스케이프와 url() 속성을 사용하는 경우 URL은 file:// 프로토콜로 시작된다

는 POSIX 경로를 얻기 위하여
>> Application('Finder').selection()[0].name() 
=> "asdf\"asdf.txt" 
>> 
>> Application('Finder').selection()[0].url() 
=> "file:///Users/example/Tools/my-tools/asdf%22asdf.txt" 

, 당신은 url()unescape()과의 처음부터 file:// 프로토콜을 제거 할 수 있습니다 url() :

>> unescape(Application('Finder').selection()[0].url()).replace(/^file[:]\/\//, '') 
=> "/Users/example/Tools/my-tools/asdf\"asdf.txt" 
+3

쉽게 :'Path (decodeURI (Application ('Finder'). selection() [0] .url()))'는 유효한 Path 객체를 얻습니다 (JXA는 AppleScript 별칭과 동일합니다). URI 인코딩 및 디코딩은 JavaScript _really_에서 다루었으며 Path 객체는'file : //'프로토콜 접두사를 처리 할만큼 똑똑합니다. – kopischke