如何使用 Apple JXA 装载卷和打开文件夹

How to mount a volume and open a folder with Apple JXA

我想安装一个卷然后立即打开 Finder window 到特定路径,但我在从 Apple Script 转换为 JXA 时遇到问题。

我想要类似这样的 JXA:

tell application "Finder"
    if not (disk "Airport Time Capsule" exists) then
        mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
    end if
    open "/Volumes/Airport Time Capsule"
end tell

我尝试了一些方法,但似乎没有任何效果。 JXA 的完整文档不存在。

var finder = Application('Finder');
finder.mount('afp://.....'); // doesn't work
finder.mount.volume('afp://.....'); // doesn't work
finder.mountVolume('afp://.....'); // doesn't work

以下代码应大致模拟您的 AppleScript。它检查是否已安装具有指定名称 ("AirPort Time Capsule") 的磁盘,如果有则打开这样的磁盘,否则使用 openLocation 函数连接到 Time Capsule(这需要 standardAdditions):

var finder = Application('Finder')
finder.includeStandardAdditions = true
var disks = finder.disks.where({name: 'AirPort Time Capsule'})
if (disks.length > 0) {
    finder.open(disk[0])
} else {
    finder.openLocation('afp://[your-time-capsule].local')
}

实际上我不得不在我的笔记本电脑上将 "AirPort Time Capsule" 更改为 "MobileBackups",所以如果上面的代码不起作用,您可以试试。