如何获取 Finder 选择的文件的 posix 路径?
How can I get the posix path of a file that is selected by Finder?
我正在尝试获取 Finder 选择的文件的完整路径。在这一点上,我尝试了很多方法:
>> 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.
None这些命令获取posix路径,即/Users/example/Tools/my-tools/example.sh
.
最终,我知道我可以简单地去掉 url 前面的 file://
:
>> Application('Finder').selection()[0].url().replace(/^file:\/\//, '')
=> "/Users/example/Tools/my-tools/example.sh"
但这似乎很老套,似乎应该有一个命令可以直接获取 posix 路径。
如果您检查 the file's properties()
,您会看到唯一包含完整路径的 属性 是 url()
。但是,在使用 url()
属性:
时,特殊字符被转义并且 url 以 file://
协议为前缀
>> Application('Finder').selection()[0].name()
=> "asdf\"asdf.txt"
>>
>> Application('Finder').selection()[0].url()
=> "file:///Users/example/Tools/my-tools/asdf%22asdf.txt"
为了获得 POSIX 路径,您可以 unescape()
url()
并删除 url()
开头的 file://
协议:
>> unescape(Application('Finder').selection()[0].url()).replace(/^file[:]\/\//, '')
=> "/Users/example/Tools/my-tools/asdf\"asdf.txt"
我正在尝试获取 Finder 选择的文件的完整路径。在这一点上,我尝试了很多方法:
>> 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.
None这些命令获取posix路径,即/Users/example/Tools/my-tools/example.sh
.
最终,我知道我可以简单地去掉 url 前面的 file://
:
>> Application('Finder').selection()[0].url().replace(/^file:\/\//, '')
=> "/Users/example/Tools/my-tools/example.sh"
但这似乎很老套,似乎应该有一个命令可以直接获取 posix 路径。
如果您检查 the file's properties()
,您会看到唯一包含完整路径的 属性 是 url()
。但是,在使用 url()
属性:
file://
协议为前缀
>> Application('Finder').selection()[0].name()
=> "asdf\"asdf.txt"
>>
>> Application('Finder').selection()[0].url()
=> "file:///Users/example/Tools/my-tools/asdf%22asdf.txt"
为了获得 POSIX 路径,您可以 unescape()
url()
并删除 url()
开头的 file://
协议:
>> unescape(Application('Finder').selection()[0].url()).replace(/^file[:]\/\//, '')
=> "/Users/example/Tools/my-tools/asdf\"asdf.txt"