Alfred 快捷切换MacBook外接显示器横竖屏

参考用Alfred实现Mac OSX快捷旋转屏幕

硬件:MacBook Pro early 2015 (显卡ntel Iris Graphics 6100 1536 MB)
外接:Dell P2415Q (难得的23.8英寸的小尺寸4K屏,终于不觉得显示器发虚了)
系统:macOS sierra 10.12.4
软件:Alfred 3

Dell的这个显示器支架是支持旋转的,竖屏浏览文档、刷网页还是挺爽。但是切换横竖屏不想Windows有快捷键,不是很方便。本着偷懒的态度,搜到了简书上别人写的,发现不能用,自己找了下原始的applescript 稍微改了改,终于可以用了,分享出来吧,亲测可用。

只把几个关键部分的内容改成了中文就能用了,脚本作者已经在注释里面提醒了。如果系统语言是英文的,应该用上面文章里面的workflow应该就成。

下面是源码,懒得删注释了。


创建个workflow
贴代码
-- Now works with Mountain Lion 10.8

-- Rotate Display on machines
-- This code is offered without any liability implied or explicit.
-- Use it at your own risk.
-- Copyright 2005, 2006 Conrad Albrecht-Buehler
-- Modifications for portrait/landscape only May 2006 Bryan Wu to 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.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

-- Set these to match your monitor's portrait and landscape modes
-- For example, on my monitor, Landscape mode is 'Standard' (item 1 in the rotate menu)
-- On my monitor, Portrait mode is '90°' (item 2 in the rotate menu)
property rotationDirectionLandscape : 1 -- rotate menu item 1 (Standard)
property rotationDirectionPortrait : 2 -- rotate menu item 2 (90 degrees)

-- the "main" part of the script
-- activate System Preferences
tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.displays"
    --reveal anchor "displaysDisplayTab" of pane "Displays"
end tell

-- get all the display preference pane windows
-- and rotate each corresponding display
set allDisplays to my getDisplays()
repeat with i from 1 to length of allDisplays
    my setDisplay(i)
end repeat

-- this function gets a list of the display preferences windows.
-- needed if you have more than one display that you want to 
-- rotate.  Note: PowerBooks will not rotate their built-in
-- LCDs with this script.
on getDisplays()
    tell application "System Events"
        get properties
        tell process "System Preferences"
            set allDisplays to every window
        end tell
    end tell
    return allDisplays
end getDisplays

-- This function simply clicks the pop-up button that
-- controls rotation, and selects the next in order
-- (either clockwise or counter-clockwise)
on setDisplay(thisDisplay)
    set rotatable to false
    tell application "System Events"
        get properties
        tell process "System Preferences"
            tell window thisDisplay
                tell tab group 1
                    click radio button "显示器" -- You may need to change this to your local language, i.e. "Monitor"
                    try
                        click pop up button 1 -- If this doesn't work, try button 2
                        tell pop up button 1 -- If this doesn't work, try button 2
                            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
                            if i is equal to rotationDirectionLandscape then
                                -- is landscape now, switch to portrait mode
                                set rotateMenuItem to rotationDirectionPortrait
                            else
                                -- is not landscape now, switch to landscape
                                set rotateMenuItem to rotationDirectionLandscape
                            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
            if rotatable then
                --delay 5
                -- After rotation, for some reason the confirmation dialog is always in window 1.
                set success to 0
                repeat until success is equal to 1
                    try
                        tell window 1
                            tell sheet 1
                                click button "确认"
                                set success to 1
                            end tell
                        end tell
                    on error errText
                        log errText
                        delay 1
                    end try
                end repeat
            end if
        end tell
    end tell
end setDisplay


-- quit system preferences
tell application "System Preferences"
    quit
end tell
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容