如何通过AppleScript打开Finder侧边栏文件夹?

4
我有一个Bash脚本,需要通过ssh在Mac上运行。该脚本需要已经挂载特定网络驱动器。在Mac上,我通过在Finder中打开该驱动器上的“JPLemme”文件夹来挂载此驱动器。这将使驱动器挂载,直到Mac夜间休眠。
显然,ssh无法使用Finder,因此我想创建一个AppleScript来模拟我通过GUI所做的操作。我尝试了:
tell application "Finder"
activate
open "JPLemme"
end tell

我收到了以下错误信息:

execution error: Finder got an error: Can't get "JPLemme". (-1728)

我认为我可能错过了一些显而易见的东西,但是谷歌没有帮助我。我也愿意考虑更好的解决方案,比如直接挂载驱动器;我避免采用这种方法,因为我不想在已经以意外的方式挂载驱动器后让Mac再次尝试挂载它而导致系统崩溃。(我真的不喜欢Mac或AppleScript...)

2个回答

3

您的网络卷应该附加了某种域名,例如“JPLemme.domain.com”。我使用以下代码块来访问一个受密码保护的网络卷:

    tell application "Finder"
       try
          set theServer to mount volume "smb://path/to/volume" as username "YourUserName" with password "YourPassword" 
--Please note here that this is a plain string without any built-in security. Use at your own risk.
       on error
          set VolumeCount to (get count of disks)
          repeat with x from 1 to VolumeCount
             set thisVolume to disk x
             if name of thisVolume is "JPLemme" then
                set theServer to thisVolume
                exit repeat
             end if
          end repeat
       end try
    end tell

你可以根据需要进行清理,但这就是核心。祝好运。

1

在其核心,实际上只需要以下内容:

Tell Application "Finder"
   Mount Volume "smb://username:password@server/sub/directory"
End Tell

但是使用什么取决于网络环境和返回的错误。


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