前言
一个程序猿么,在用不同系统的时候,显示器的屏幕状态会切换的,比如用Mac
在看文档或者撸代码时用竖屏,用Win
打游戏时用横屏。
其实手动旋转屏幕倒是其次,但是有时候在横屏状态下突然打开Mac
就有些麻木了。手动设置一两次便不说什么了,但是博主多次设置后着实觉得有点儿麻烦!所以找了一下脚本。
参考的脚本是有多个显示器的话,需要遍历去修改。
我这里提供一个单显示器的脚本来切换。
系统环境:Monterey 12.4
脚本代码
首先,打开脚本编辑器.app
这个应用。
输入脚本内容:
(*
Now works with Monterey 12.4
Rotate Display on machines, support toggling between only two modes - landscape and portrait
NOTE: UI Scripting must be enabled for this to work! Confirm that
"Enable access for assistive devices" is checked in the
Universal Access System Preference Pane
*)
(*
v1.2.2022-12-15 updated to work on Monterey 12.4. Thanks, Keen
v1.2.2013-01-26 Thanks, Rich Graham for pointing out that display panels may have different layouts. "button 1" below might need to change to "button 2" (or 3).
v1.1.2012-09-02 updated to work on 10.8 Mountain Lion. Thanks, F. Parsons
v1.1.2010-08-01 updated to work with 10.6.4's reverted(?) display panelv1.1.2010-04-08 updated to work with 10.6.3's revised display panel
v1.1.2006-03-01 updated to handle displays with the same name.
v1.1.2006-05-28-Bryan updated to handle displays with the same name.
v1.1.2009-08-02 - Bryan updated to work with Snow Leopard
*)
-- 打开系统偏好设置中的显示器
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.displays"
end tell
delay 3
my setDisplay(1)
on setDisplay(thisDisplay)
set rotatable to false
tell application "System Events"
tell process "System Preferences"
tell window thisDisplay
tell group 1
try
click pop up button 3
tell pop up button 3
repeat with i from 1 to 4
if selected of menu item i of menu 1 is true then
exit repeat
end if
end repeat
log i -- 标准水平就是1,90度是2,180度是3,270度是4
-- 我的逻辑是在标准水平和垂直之间进行切换
if i is equal to 1 then
-- is landscape now, switch to portrait mode
set rotateMenuItem to 4
else
-- is not landscape now, switch to landscape
set rotateMenuItem to 1
end if
click menu item rotateMenuItem of menu 1
end tell
-- If "Standard" is selected, no confirmation dialog is displayed.
if rotateMenuItem is not 1 then
set rotatable to true
end if
on error
log "Can't rotate display. It may be the laptop's built in display."
end try
end tell
end tell
delay 5
if rotatable then
-- 等切换完成五秒之后,确认窗口
try
tell window 1
tell sheet 1
click button "确认"
log "success"
end tell
end tell
on error errText
log errText
delay 1
end try
end if
quit
end tell
end tell
end setDisplay
保存成为App文件即可。
注意运行的时候需要打开辅助功能选项。
感谢
用Alfred实现Mac OSX快捷旋转屏幕
Alfred 快捷切换MacBook外接显示器横竖屏