【问题标题】:Applescript: Open a Folder in FinderApplescript:在 Finder 中打开文件夹
【发布时间】:2012-06-30 23:15:39
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 使用Finder window 类而不是window 如果这些窗口之一打开(信息窗口、首选项窗口、剪辑窗口和视图选项窗口)不会出现错误。--> @ 987654328@ 。显示命令:将指定的对象带入 view,使用 open 命令,如 adayzdone 的答案。

标签: applescript directory finder


【解决方案1】:

试试:

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 未定义的错误。
【解决方案2】:

据我搜索,似乎没有办法打开文件夹,而只是在 AppleScript 中突出显示文件夹。所以我用过:

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

它对我来说很好,但如果我错了,请更新我。

【讨论】:

  • 您的do shell script 命令和Finder 中的open(不是reveal)命令没有区别,它适用于所有人。但是,也许这是规则的一个例外。
  • 为了完整起见,我想补充一点,如果您的路径是参数,您可以使用do shell script "open " & quote & pathParameter & quote,它会处理路径中的空格等内容。
【解决方案3】:

实际上比看起来简单:

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)。谢谢!
  • 我不得不使用这样的东西(莫哈韦):Macintosh HD:Users:MyUser:Downloads。将“MyUser”替换为用户文件夹的实际名称。
猜你喜欢
  • 2010-09-23
  • 2011-10-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
相关资源
最近更新 更多