Skip to main content
 首页 » 编程设计

macos中自动为每个登录 Mac 的用户映射网络驱动器

2025年02月15日109虾米姐

编辑

感谢 Simon White,我现在可以正常工作了。结果发现它需要保存为 scpt 文件而不是应用程序。我修改了下面的代码以显示这一点:

我是 AppleScript 和 launchctl 的新手,所以请耐心等待。

我希望每个登录 Mac 的用户都运行一些东西来安装 5 个网络卷并更改 Finder 首选项以在桌面上显示连接的服务器和硬盘。到目前为止,我已经创建了以下 AppleScript 来执行此操作,如果将其拖到用户的登录项中,它就可以工作。我已将其作为“netvols.app”保存到/Applications 文件夹中。

-- Mount network drives 
tell application "Finder" 
    try 
        mount volume "smb://path/to/share" 
        mount volume "smb://path/to/share" 
        mount volume "smb://path/to/share" 
        mount volume "smb://path/to/share" 
        mount volume "smb://path/to/share" 
    end try 
 
    -- tell Finder preferences to show hard disks and connected servers on the desktop 
    tell Finder preferences 
        set desktop shows hard disks to true 
        set desktop shows connected servers to true 
    end tell 
end tell 

它在用户的登录项中以及手动运行时有效,但我无法让它为登录到计算机的每个用户工作。我尝试将其添加到/Library/StartupItems 但不起作用。

最近我尝试将 plist 文件添加到/Library/LaunchAgents 但它不起作用 - 在控制台中我可以看到错误:

launchctl: launchctl: no plist was returned for: /Library/LaunchAgents/login.plist 

下面是我的 plist 文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>Label</key> 
    <string>IglooLogin</key> 
    <key>ProgramArguments</key> 
    <array> 
        <string>/usr/bin/osascript</string> 
        <string>/Applications/Mount Network Volumes.scpt</string> 
    </array> 
</dict> 
</plist> 

我已使用以下内容更改了文件的所有者和权限:

sudo chown root:wheel /Library/LaunchAgents/login.plist 
sudo chmod 644 /Library/LaunchAgents/login.plist 

如果我尝试在终端中使用 launchctl 手动运行 plist:

sudo launchctl load /Library/LaunchAgents/login.plist 

我收到以下错误:

launchctl: no plist was returned for: /Library/LaunchAgents/login.plist 
launchctl: no plist was returned for: /Library/LaunchAgents/login.plist 
nothing found to load 

我确信我错过了一些非常简单的东西,但正如我所提到的,这对我来说是全新的,所以请保持温柔:)

编辑

是的,我在我的 plist 文件中发现了一个相当明显的语法错误 - D'OH!

无论如何,现在我遇到了一个全新的错误 -

com.apple.launchd: (IglooLogin[1072]) Job failed to exec(3) for weird reason: 13 

据我所知,13 是一个权限错误,但我仍然无法弄清楚原因!

提前致谢

请您参考如下方法:

需要考虑的是,如果需要在登录时挂载网盘,可以将网盘添加到每个用户的登录项中。然后手动设置 Finder 首选项一次。这可能是适合您的解决方案。

当您从另一个用户帐户运行应用程序时,该应用程序如何失败?您看到任何错误消息吗?应用程序是否正在运行然后退出并且磁盘未安装?

我不认为你对 plist 所做的事情就是答案。请记住,当您将 AppleScript 保存为应用程序时,它不再是 AppleScript,而是 Mac 应用程序。它的包内有自己的 plist。用户必须将其作为应用程序启动。您的故障排除步骤可能类似于您安装任何 Mac 应用程序而其他用户无法成功运行它时所执行的操作。例如,您可以尝试更改“系统偏好设置”的“安全和隐私” Pane 中的设置,以便允许来自身份不明的开发人员的应用程序。 (允许从以下位置下载应用程序。)

您应该在设置首选项设置的值之前对其进行测试,否则您将一次又一次地将它们设置为 true,即使它们已经是 true。

更改这些行:

tell Finder preferences 
        set desktop shows hard disks to true 
        set desktop shows connected servers to true 
end tell 

…对此:

tell Finder preferences 
    if desktop shows hard disks is equal to false then 
        set desktop shows hard disks to true 
    end if 
    if desktop shows connected servers is equal to false then 
        set desktop shows connected servers to true 
    end if 
end tell 

并且您没有在“安装卷”行中指定用户名。只要这是您想要做的,那就没问题。

您可以将您的应用程序命名为“挂载网络磁盘”,以便其他人以后更容易识别和使用。 Mac 应用程序不要求有这样的短名称。要求是显而易见且易于使用。即使用户只是查看他们的登录项,并且看到“安装网络磁盘”而不是“netvols”,这对该用户也是有帮助的。