Mac切換橫豎屏

前言
一個程序猿么,在用不同系統(tǒng)的時候,顯示器的屏幕狀態(tài)會切換的,比如用Mac
在看文檔或者擼代碼時用豎屏,用Win
打游戲時用橫屏。
其實手動旋轉(zhuǎn)屏幕倒是其次,但是有時候在橫屏狀態(tài)下突然打開Mac
就有些麻木了。手動設(shè)置一兩次便不說什么了,但是博主多次設(shè)置后著實覺得有點兒麻煩!所以找了一下腳本。
參考的腳本是有多個顯示器的話,需要遍歷去修改。
我這里提供一個單顯示器的腳本來切換。
系統(tǒng)環(huán)境:Monterey 12.4
腳本代碼
首先,打開腳本編輯器.app
這個應(yīng)用。
輸入腳本內(nèi)容:
(*
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?panel
v1.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
*)
--?打開系統(tǒng)偏好設(shè)置中的顯示器
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實現(xiàn)Mac OSX快捷旋轉(zhuǎn)屏幕
Alfred 快捷切換MacBook外接顯示器橫豎屏