请求修改AppleScript:将选文件夹算视频总时长改为右键Service
适配右键Service的AppleScript修改版
没问题!我帮你把原脚本改成适配右键Service的版本,这样你直接右键点文件夹就能调用啦~
修改核心点
- 去掉了原脚本里的
choose folder弹窗,直接接收右键选中的文件夹作为输入 - 支持同时选中多个文件夹一起计算总时长
- 保留了原有的视频时长计算逻辑,同时优化了格式兼容性
修改后的完整脚本
on run {input, parameters} -- 初始化总时长为0 set totalDuration to 0 -- 遍历所有选中的文件夹(支持多选) repeat with selectedFolder in input -- 定义要识别的视频格式,可根据需求自行添加 set supportedFormats to {"mov", "mp4", "m4v", "avi", "mkv", "flv", "wmv"} set videoFiles to every file of selectedFolder whose name extension is in supportedFormats -- 逐个计算视频文件时长 repeat with aFile in videoFiles -- 用系统自带的mdls命令获取视频时长(秒),无需额外工具 set durationCmd to "mdls -name kMDItemDurationSeconds -raw " & quoted form of POSIX path of aFile set durationSeconds to do shell script durationCmd -- 避免空值导致计算错误 if durationSeconds is not "" then set totalDuration to totalDuration + durationSeconds as number end if end repeat end repeat -- 把总秒数转换成小时:分钟:秒的友好格式 set totalHours to totalDuration div 3600 set remainingSeconds to totalDuration mod 3600 set totalMinutes to remainingSeconds div 60 set totalSeconds to remainingSeconds mod 60 -- 弹窗展示计算结果 display dialog "选中文件夹内视频总时长:" & totalHours & "小时 " & totalMinutes & "分钟 " & totalSeconds & "秒" buttons {"确定"} default button 1 return input end run
配置成右键Service的步骤
- 打开「Automator」应用,新建一个「快速操作」(旧版叫「服务」)
- 在顶部栏设置:「工作流程收到」选择「文件夹」,「位于」选择「Finder.app」
- 从左侧动作库拖入「运行AppleScript」动作,替换默认代码为上面的脚本
- 点击左上角保存,命名为比如「计算视频总时长」,之后就能在Finder右键菜单里找到它啦
内容的提问来源于stack exchange,提问作者Utkarsh Patel
相关产品推荐
相关产品推荐

