离开窗口时(以及切换回窗口时)是否可以执行脚本 (Applescript/Shellscript)?我想要做的是:离开 VM 窗口时暂停 VM (vmrun pause path/to/vm),切换回 VM 时恢复 VM (vmrum unpause path/to/vm)。
谢谢!
答案1
您可以将类似这样的内容保存为 AppleSript 编辑器中保持打开的应用程序。
set prev to false
repeat
delay 1
tell application "System Events"
try
tell process "TextEdit"
set now to frontmost and name of window 1 is "Test.txt"
end tell
on error
set now to false
end try
if prev and not now then say "the window was unfocused"
set prev to now
end tell
end repeat
如果您不想运行后台进程,您可以将其存储prev
在文件中并每隔几秒钟使用 LaunchAgent 运行脚本。