AppleScript实现多应用连续全屏的异常问题求助
AppleScript全屏工作环境脚本异常排查求助
设备与系统信息
- 设备:15英寸2017款MacBook Pro
- 系统:macOS Catalina 10.15.7
问题概述
编写AppleScript脚本,目标是通过单条命令打开并将多个应用设置为全屏以搭建工作环境,但在应用全屏设置环节遇到执行异常。
异常脚本片段(script1.scpt)
tell application "Sublime Text" activate tell application "System Events" tell front window of (first process whose frontmost is true) set value of attribute "AXFullScreen" to true end tell key code 123 using {control down} end tell end tell
执行异常情况
- 当Sublime Text未全屏时,执行
osascript script1.scpt,应用激活并进入全屏,但key code 123 using {control down}无效果,无报错或提示音。 - 当应用已全屏时执行该命令,应用激活并保持全屏,但出现报错:
execution error: System Events got an error: Can't get window 1 of process 1 whose frontmost = true. Invalid index. (-1719)
同时key code 123 using {control down}也无效果。 - 通过Shell脚本调用script1.scpt且应用未全屏时,行为与上述第一种情况一致。
- 通过Shell脚本调用script1.scpt且应用已全屏时行为正常,但扩展为设置两个应用全屏的script2.scpt时,第二个应用无反应,且再次出现上述第二种情况的报错。
双应用全屏异常脚本(script2.scpt)
tell application "Sublime Text" activate tell application "System Events" tell front window of (first process whose frontmost is true) set value of attribute "AXFullScreen" to true end tell key code 123 using {control down} end tell end tell tell application "Skim" activate tell application "System Events" tell front window of (first process whose frontmost is true) set value of attribute "AXFullScreen" to true end tell key code 123 using {control down} end tell end tell
当前临时解决方案
目前仅能通过拆分步骤并添加延迟的方式让脚本工作,但希望找到更快速简洁的解决方案。以下是依赖延迟的可用脚本:
.bash_profile
cd ~/<path>/ ; osascript workspace_on.scpt
workspace_on.scpt
tell app "System Events" to tell process "Terminal" to set the value of attribute "AXFullScreen" of window 1 to true delay 0.6 tell app "Sublime Text" to activate delay 0.6 tell app "System Events" to tell process "Sublime Text" to set the value of attribute "AXFullScreen" of window 1 to true delay 0.6 do shell script "open -a skim ~/<path>/main.pdf" delay 1 tell app "Skim" to set interaction mode of document 1 to full screen mode delay 0.6 do shell script "open ~/<path>" delay 1 tell app "System Events" to tell process "Finder" to set the value of attribute "AXFullScreen" of window "latex-template" to true
内容的提问来源于stack exchange,提问作者FlatKos
相关产品推荐
相关产品推荐

