首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Applescript -如何实现队列功能

Applescript -如何实现队列功能
EN

Stack Overflow用户
提问于 2019-01-15 10:14:29
回答 1查看 109关注 0票数 0

我正在写一个脚本批量转换视频到h.265格式,这使用repeat with来通过所有的视频。它可以同时处理3或4个文件,但我的旧mac会在视频数量达到~50个时重新启动。

代码语言:javascript
运行
复制
repeat with videofile in video_list
    set filePath to POSIX path of videofile
    set filePath to esc_space(filePath)
    set [folderPath, filename] to split_path_name(filePath)

    tell application "Terminal"
        activate
        do script with command "ffmpeg -hide_banner -i " & filePath & " -vcodec libx265 -tag:v hvc1 " & folderPath & filename & "_hevc.mp4; mv " & filePath & " ~/.Trash"
    end tell
end repeat

因此,我想使用applescript来实现“队列”功能:在终端窗口中转换有限数量的视频(比如说10个),并监控是否有任何窗口完成执行,如果活动窗口的数量少于10个,则激活一些剩余的任务。

我做了一些搜索,发现系统事件可以判断应用程序是否正在运行,但我不确定如何监控多个窗口,特别是当一些任务完成时,新窗口将被激活。

如有任何建议,欢迎光临。(如果方便的话,也欢迎使用shell脚本)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-16 20:38:14

经过几次尝试,我自己成功地完成了这项工作,希望我的答案能对碰巧有类似问题的人有所帮助。

由于AppleScript中的列表不能修改(如果我错了,请纠正我),我必须使用索引来遍历我的视频,而不是获取第一项,然后将其从列表中删除:

代码语言:javascript
运行
复制
set next_video_index to 1

下面的无限repeat循环用于监控由System Events计数的活动终端窗口的数量。这不是最好的解决方案,因为System Events对所有窗口进行计数,包括用户手动打开的窗口。

代码语言:javascript
运行
复制
repeat while true
    tell application "System Events"
        tell application "Terminal"
            set window_count to (count of windows)
        end tell
    end tell

当终端窗口的数量没有达到最大值(在我的代码中设置为5)并且不是所有视频都被转换时,if语句帮助启动新的转换任务。

应该注意的是,在终端脚本末尾的; exit,它确保了完成的任务窗口不会打乱窗口计数,但您需要首先更改终端首选项,请参阅此链接:OSX - How to auto Close Terminal window after the "exit" command executed.

代码语言:javascript
运行
复制
    set task_not_finished to (next_video_index ≤ length of video_list)

    if (window_count < 5) and task_not_finished then
        set filePath to POSIX path of item next_video_index in video_list
        set filePath to esc_space(filePath)
        set [folderPath, filename] to split_path_name(filePath)
        set next_video_index to next_video_index + 1

        tell application "Terminal"
            activate
            do script with command "ffmpeg -hide_banner -i " & filePath & " -vcodec libx265 -tag:v hvc1 " & folderPath & filename & "_hevc.mp4; mv " & filePath & " ~/.Trash; exit"
        end tell
    end if

当最后一个视频正在转换时,是时候结束重复循环了。

代码语言:javascript
运行
复制
    if not task_not_finished then exit repeat
    delay 1
end repeat
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54191854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档