使用AppleScript随机打开文件时遭遇QuickTime兼容性错误求助
解决AppleScript打开随机文件时的QuickTime兼容性错误
我用以下AppleScript脚本随机打开个人文件夹下Movies目录里的文件时,频繁收到错误提示:The file isn’t compatible with QuickTime Player.
脚本代码:
tell application "Finder" set random_file to some file of entire contents of folder "Movies" of home open result end tell
这些报错的文件手动在Finder里双击能正常打开,而且手动打开过一次后,脚本再打开它就没问题了。Movies文件夹里有几千个文件,没法逐个手动处理,而且之前脚本一直正常运行。
解决方案:
强制调用默认关联应用打开
Finder的open命令有时会优先用QuickTime尝试打开Movies文件夹里的文件,哪怕它不是QuickTime支持的格式。改成让系统用文件的默认关联应用打开,替换脚本里的open result为:open random_file using (get default application of random_file)完整脚本:
tell application "Finder" set random_file to some file of entire contents of folder "Movies" of home open random_file using (get default application of random_file) end tell筛选QuickTime支持的文件类型
如果只想打开QuickTime能处理的文件,可以先过滤格式,比如只保留mov、mp4、m4v等常见媒体格式:tell application "Finder" set allowed_extensions to {"mov", "mp4", "m4v", "mp3", "m4a"} set movie_files to every file of entire contents of folder "Movies" of home whose name extension is in allowed_extensions if movie_files is not {} then set random_file to some item of movie_files open random_file else display dialog "No supported files found in Movies folder." end if end tell重置LaunchServices修复文件关联
如果是系统文件关联出了临时故障(这也是之前正常现在报错的常见原因),可以在终端运行以下命令重置LaunchServices,修复文件打开关联:/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user运行后重启Mac,再测试脚本。
内容的提问来源于stack exchange,提问作者J. Blame
相关产品推荐
相关产品推荐

