【问题标题】:Applescript - how to achieve the queue functionalityApplescript - 如何实现队列功能
【发布时间】:2019-06-09 01:14:23
【问题描述】:

我正在编写一个脚本,将视频批量转换为 h.265 格式,该格式使用 repeat with 浏览所有视频。它同时适用于 3 或 4 个文件,但是当视频数量达到 ~50 时,我的旧 Mac 会重新启动。

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脚本)

【问题讨论】:

    标签: queue applescript


    【解决方案1】:

    经过一些尝试,我自己成功地做到了,希望我的回答能帮助碰巧有类似问题的人。

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

    set next_video_index to 1
    

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

    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.

        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
    

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

        if not task_not_finished then exit repeat
        delay 1
    end repeat
    

    【讨论】:

      猜你喜欢
      • 2012-02-11
      • 1970-01-01
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 2014-05-02
      • 1970-01-01
      • 2022-07-04
      • 1970-01-01
      相关资源
      最近更新 更多