如何在AppleScript中使用Finder移动文件时避免提示音?
解决AppleScript移动文件时Finder提示音的问题
Great question—dealing with persistent Finder sounds during long scripts is super annoying, especially when you can't mute the whole system. Here are a few reliable ways to fix this without disrupting other apps:
1. 用System Events替代Finder执行移动操作
Finder的提示音是和它的GUI交互绑定的,而System Events是后台级别的自动化工具,执行文件操作不会触发这些声音提示。把你的代码改成这样:
-- 假设yourFile是你要移动的文件对象,workdir是目标文件夹对象 tell application "System Events" move yourFile to workdir end tell
这个方法不需要修改系统设置,也不会影响Finder的正常使用,很适合大多数场景。
2. 临时禁用Finder提示音,处理后恢复
如果一定要用Finder的命令(比如依赖Finder的某些特性),可以临时关掉它的声音设置,处理完再恢复。注意这个方法会重启Finder,所以如果当时正在使用Finder的话会有短暂中断:
-- 保存当前Finder的声音设置 set originalSoundSetting to do shell script "defaults read com.apple.finder FinderSounds" -- 关闭Finder提示音并重启Finder生效 do shell script "defaults write com.apple.finder FinderSounds -bool false; killall Finder" -- 执行你的文件移动逻辑 tell application "Finder" move yourFile to workdir end tell -- 恢复原来的声音设置并重启Finder do shell script "defaults write com.apple.finder FinderSounds -bool " & originalSoundSetting & "; killall Finder"
3. 用Shell命令mv直接移动文件
完全绕开GUI工具,用Unix底层的mv命令来移动文件,速度更快还绝对不会有提示音。记得用quoted form处理路径里的空格或特殊字符:
-- 将AppleScript文件/文件夹对象转换为POSIX路径 set filePOSIXPath to POSIX path of yourFile set workdirPOSIXPath to POSIX path of workdir -- 执行mv命令 do shell script "mv " & quoted form of filePOSIXPath & " " & quoted form of workdirPOSIXPath
我自己处理大量文件的时候经常用这个方法,不仅没声音,批量处理的效率也比Finder高不少。
内容的提问来源于stack exchange,提问作者matjes
相关产品推荐
相关产品推荐

