Applescript:在Finder中打开文件夹

19

我正尝试使用 AppleScript 在 Finder 中打开一个文件夹。以下是我的代码。我想要文件夹 WorkSpace 在 Finder 中打开,但它打开了父文件夹 /Volumes/MyMacDrive/Mani 并突出显示 WorkSpace 文件夹。我想要的是 WorkSpace 文件夹的内容,但我得到的却是其父文件夹的内容。我在这里漏掉了什么吗..?

property the_path : "/Volumes/MyMacDrive/Mani/WorkSpace/"
set the_folder to (POSIX file the_path) as alias
tell application "Finder"
    activate
    if window 1 exists then
        set target of window 1 to the_folder
    else
        reveal the_folder
    end if
end tell

2
使用“Finder窗口”类代替“window”,以避免出现错误,如果其中一个窗口已经打开(信息窗口、首选项窗口、剪辑窗口和视图选项窗口)则执行以下操作:if Finder window 1 exists then。揭示命令:将指定的对象带入视图,如adayzdone的答案中所述,使用“open”命令。 - jackjr300
4个回答

28

就我所搜寻的,似乎没有办法在AppleScript中打开文件夹,而只能突出显示文件夹。因此我使用了以下代码:

do shell script "open /Volumes/MyMacDrive/Mani/WorkSpace/"

这对我来说运作良好,但如果我错了,请更新我。


1
你的“do shell script”命令和Finder中的“open”(不是reveal)命令没有区别,它适用于所有人。但也许这是个例外。 - jackjr300
2
为了完整起见,我想补充一下,如果您的路径是一个参数,您可以使用 do shell script "open " & quote & pathParameter & quote,它会处理路径中的空格等问题。 - Tyler Forsythe

21

其实这比看起来的要简单:

tell application "Finder" to open ("/Volumes/MyMacDrive/Mani/WorkSpace/" as POSIX file)

或者使用冒号来给出一个AppleScript路径:

tell application "Finder" to open "MyMacDrive:Mani:WorkSpace"

通过这样,你拥有了一个打开的窗口。


好的,我使用了第二个方法并且在我的Mac OS X 10.10.3和AppleScript 2.4上运行良好。谢谢! - RoberRM
1
我必须使用类似这样的内容(Mojave):Macintosh HD:Users:MyUser:Downloads。将 "MyUser" 替换为您实际用户文件夹的名称。 - StevieD

5

尝试:

if front Finder window exists then
    set target of front Finder window to the_folder
else
    open the_folder
end if

编辑以包含jackjr300的更正。Finder窗口是要使用的正确类。


虽然这是正确的,但我进行了编辑,因为如果没有 end if,它将无法工作,并且您将收到“the_folder 未定义”的错误。 - DᴀʀᴛʜVᴀᴅᴇʀ

0

这对我很有效,将Finder带到了最前面。

tell application "Finder"
    activate
    open ("/Users/MYNAME/Desktop/Figma/" as POSIX file)
end tell

我刚把它放进了快捷方式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接