在Finder中获取文件的文件路径

Get filepath of file in Finder

我想在Finder中获取前面文件的文件路径,例如/Users/user/Downloads/file.png,我对macOS和AppleScript处理文件路径的方式很困惑,POSIX带有斜线和原生的冒号。我尝试了这两个:

tell application "Finder"
    set temp to selection
    repeat with i from 1 to length of temp
        set the_item to item i of temp
        
        set item_posix to the_item as POSIX file
        set the_result to POSIX path of item_posix -- returns only the file's name and extension
        
        return get the path of the_item -- returns an error
    end repeat
end tell

我曾经成功过,但它与 as fileas alias 太复杂了,我不记得它是如何工作的。

如何获取Finder中最前面文件的文件路径?

更新:为了语法,我对单个路径感兴趣,我可以用循环处理多个路径。

随着 inuxmint-20-cinnamon-64bit.iso filesDownloads 文件夹Finder 中,这里有一些例子:

这个命令:

tell application "Finder" to get selection

结果:

--> {document file "linuxmint-20-cinnamon-64bit.iso" of folder "Downloads" of folder "me" of folder "Users" of startup disk}

这个命令:

tell application "Finder" to get selection as alias

结果:

-> {alias "Macintosh HD:Users:me:Downloads:linuxmint-20-cinnamon-64bit.iso"}

这个命令:

tell application "Finder" to set aliasItemName to selection as alias

结果:

--> {alias "Macintosh HD:Users:me:Downloads:linuxmint-20-cinnamon-64bit.iso"}

这些命令:

set posixPathName to POSIX path of aliasItemName
return posixPathName

结果:

"/Users/me/Downloads/linuxmint-20-cinnamon-64bit.iso"

请注意,set posixPathName to POSIX path of aliasItemName 是在 Finder 的上下文之外完成的,因为它不理解 POSIX path,因为它是 的一部分标准添加,而不是Finder

在 Finder 中 selection 属性 returns 总是 Finder 文件说明符列表或空列表。

使用您的语法的最简单方法是强制选择 alias list 并从别名

获取 POSIX 路径
tell application "Finder"
    set temp to selection as alias list
    repeat with i from 1 to length of temp
        set the_item to item i of temp           
        set the_result to POSIX path of the_item
        
        return the_result
    end repeat
end tell

POSIX file 仅在相反情况下需要,从 POSIX 路径获取 HFS 路径或别名。

如果您只想要 selection 的第一项,则不需要循环,但您必须检查 空列表

tell application "Finder"
    set temp to selection as alias list
    if temp is not {} then            
       return POSIX path of item 1           
    end if
end tell

我发现以下替代 AppleScript 解决方案是我会使用的选择。

虽然 Finder.app 位于最前面,但使用键盘快捷键...选项 + 命令 + c 会将所选 Finder 项目的 Posix 路径复制到剪贴板。

tell application "System Events" to keystroke "c" using {option down, command down}

这是我写的一个小实用程序 AppleScript,它包含检索所选 Finder 项目的 Posix 路径的操作……如果 Finder 位于最前面并且有所选项目。

最终,如果 Finder 不是最前面的,如果您正在查看的当前文档中的选定文本是 Posix 路径或 HFS 路径,它将在 Finder 中为您显示该项目。

-- Add A Delay For Testing Purposes. 
-— Allowing You Time To Bring Application "Finder" Or Other Document To The Front
-— While Running This Code In Your Script Editing Application

delay 10

tell application "Finder" to set finderIsFrontmost to frontmost

if finderIsFrontmost then
    -- Copy Selected Files In Finder, As Path Names
    tell application "System Events" to keystroke "c" using {option down, command down}
else
    -- Copy Selected File Path Text From Current Doc In Frontmost App
    tell application "System Events" to keystroke "c" using {command down}
    delay 0.1
    -- The "-R" Reveals Copied File Path In Finder, Rather Than Opening If It's A Path To A File
    try
        do shell script "open -R " & quoted form of (the clipboard)
        delay 0.1
        tell application "Finder" to activate
        repeat until application "Finder" is frontmost
            delay 0.2
        end repeat
        delay 0.2
        tell application "System Events" to key code 124 using {command down, option down, control down}
    on error errMsg number errNum
        try
            do shell script "open -R " & quoted form of POSIX path of (the clipboard)
            delay 0.1
            tell application "Finder" to activate
            repeat until application "Finder" is frontmost
                delay 0.2
            end repeat
            delay 0.2
            tell application "System Events" to key code 124 using {command down, option down, control down}
        on error errMsg number errNum
            activate
            display alert errMsg message ¬
                "Either The File or Folder No Longer Exists" & linefeed & linefeed & "OR" & linefeed & linefeed & ¬
                "The Selected Text Contains Starting or Trailing White Spaces" & linefeed & linefeed & ¬
                "Please Make Sure Your Selected Text Has No Empty Spaces At The Beginning or End Of Your Selection" as critical buttons {"OK"} giving up after 20
        end try
    end try
end if