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

你可能感兴趣的:(Alfred 快捷切换MacBook外接显示器横竖屏)