Applescript - 如何实现队列功能
Applescript - how to achieve the queue functionality
我正在编写一个脚本来将视频批量转换为 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 来实现 "Queue" 功能:在终端 windows 中转换有限数量的视频(比方说 10)并监控是否有任何 windows 完成执行, 如果活动 windows 少于 10,则激活一些剩余任务。
我做了一些搜索,发现系统事件可以判断应用程序是否 运行,但我不确定如何监控多个 windows,尤其是新的 windows 会被激活当一些任务完成时。
如有任何建议,我们将不胜感激。
(shell方便的话也欢迎脚本)
经过一些尝试,我自己成功地做到了,希望我的回答能帮助到恰好有类似问题的人。
由于无法修改 AppleScript 中的列表(如果我错了请纠正我),我必须使用索引来循环我的视频而不是获取第一个项目然后将其从列表中删除:
set next_video_index to 1
下面的无限repeat
循环是为了监控活动终端的数量windows,由System Events
统计。这不是最好的解决方案,因为 System Events
计算所有 windows,包括用户手动打开的那些。
repeat while true
tell application "System Events"
tell application "Terminal"
set window_count to (count of windows)
end tell
end tell
if 语句有助于在终端数量 windows 未达到最大值(在我的代码中设置为 5)且并非所有视频都已转换时启动新的转换任务。
需要注意的是 ; exit
在终端脚本的末尾,它确保完成的任务 window 不会搞乱 window 计数,但你需要改变终端首先是偏好,请参阅此 link:
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
我正在编写一个脚本来将视频批量转换为 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 来实现 "Queue" 功能:在终端 windows 中转换有限数量的视频(比方说 10)并监控是否有任何 windows 完成执行, 如果活动 windows 少于 10,则激活一些剩余任务。
我做了一些搜索,发现系统事件可以判断应用程序是否 运行,但我不确定如何监控多个 windows,尤其是新的 windows 会被激活当一些任务完成时。
如有任何建议,我们将不胜感激。 (shell方便的话也欢迎脚本)
经过一些尝试,我自己成功地做到了,希望我的回答能帮助到恰好有类似问题的人。
由于无法修改 AppleScript 中的列表(如果我错了请纠正我),我必须使用索引来循环我的视频而不是获取第一个项目然后将其从列表中删除:
set next_video_index to 1
下面的无限repeat
循环是为了监控活动终端的数量windows,由System Events
统计。这不是最好的解决方案,因为 System Events
计算所有 windows,包括用户手动打开的那些。
repeat while true
tell application "System Events"
tell application "Terminal"
set window_count to (count of windows)
end tell
end tell
if 语句有助于在终端数量 windows 未达到最大值(在我的代码中设置为 5)且并非所有视频都已转换时启动新的转换任务。
需要注意的是 ; exit
在终端脚本的末尾,它确保完成的任务 window 不会搞乱 window 计数,但你需要改变终端首先是偏好,请参阅此 link:
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