如何用AppleScript隐藏未使用的网络驱动器?
用AppleScript实现隐藏未使用的网络驱动器
是的,可以通过AppleScript实现你的需求,以下是针对两种常见场景的具体方案:
场景1:隐藏Finder侧边栏中未挂载的网络驱动器入口
如果希望Finder侧边栏仅展示已挂载的网络驱动器,不显示未挂载的网络位置,可通过AppleScript直接控制Finder偏好:
tell application "Finder" -- 关闭侧边栏的"网络"入口,不再列出未挂载的网络驱动器 set sidebar shows network to false -- 确保已挂载的网络驱动器正常显示在侧边栏 set sidebar shows mounted servers to true end tell
若需更精准地隐藏特定未挂载网络驱动器,可修改系统偏好文件(修改后需重启Finder生效):
-- 隐藏侧边栏的"网络"项 do shell script "defaults write com.apple.sidebarlists systemitems -dict-add 'Network' '<dict><key>visible</key><false/></dict>'" -- 重启Finder使设置生效 do shell script "killall Finder"
场景2:自动卸载长时间未使用的已挂载网络驱动器
如果要处理已挂载但无进程使用的网络驱动器,可结合lsof命令判断使用状态,自动卸载(卸载后驱动器会从Finder中消失):
tell application "Finder" -- 获取所有已挂载的网络驱动器 set mountedNetworkVolumes to every disk whose kind is "Network Volume" repeat with volumeItem in mountedNetworkVolumes set volumePosixPath to POSIX path of volumeItem -- 检查驱动器是否有进程在占用 set usageCount to do shell script "lsof -t " & quoted form of volumePosixPath & " | wc -l" if usageCount is "0" then -- 卸载未被使用的网络驱动器 eject volumeItem end if end repeat end tell
注意事项
- 场景1的偏好设置会全局生效,重启Mac后依然保持;
- 场景2的脚本可配置为定时任务(通过LaunchAgent),定期自动清理未使用的网络驱动器;
- 修改偏好文件前建议备份
~/Library/Preferences/com.apple.sidebarlists.plist,避免配置出错。
内容的提问来源于stack exchange,提问作者McProgger
相关产品推荐
相关产品推荐

