Apple script to change main display

applescriptdisplay

I have 3 displays, MacBook and 2x4k. My main display is one of 4k. The issue is when I lock computer often 2nd 4k Display (not main) move all full screen apps to MacBook, MacBook to this 4k. Only main display have apps on place.
To solve this issue I have to open preferences/ display/ arrangement and move white bar from main display to MacBook, than "not main 4k" and to "main" 4k. In conclusion I have to move white bar 3 times. This is really uncomfortable and I wonder how to do it wit apple script. Right now I find solution to open system preferences-> display -> arrangement, however I have no idea how to move white bar around displays. My code is here:

-- QUIT SYSTEM PREFERENCCES APPLICATION
if application "System Preferences" is running then
    tell application "System Preferences" to quit
    delay 1
end if
tell application "System Preferences"
    activate
    set the current pane to pane id "com.apple.preference.displays"
    delay 1
    tell application "System Events"
        click radio button "Rozmieszczenie" of tab group 1 of window "LG Ultra HD (1)" of application process "System Preferences"
    end tell
end tell

Can anyone help me how to finish script?

Thank you!

Best Answer

I wrote a command line tool called displayplacer that will help with this. You can specify the resolution and positioning of your various monitor setups. Setting a screen with origin:(0,0) will make it the main display with the white bar in the settings.

Execute displayplacer list to get your screen ids.

Example profile 1 with MacBook screen displayplacer "id:<4kScreen1> res:3840x2160 scaling:off origin:(0,0) degree:0" "id:<4kScreen2> res:3840x2160 scaling:off origin:(-3840,0) degree:0" "id:<MacBook> res:1440x900 scaling:on origin:(3840,0) degree:0"

Example profile 2 without MacBook Screen displayplacer "id:<4kScreen1> res:3840x2160 scaling:off origin:(0,0) degree:0" "id:<4kScreen2> res:3840x2160 scaling:off origin:(-3840,0) degree:0"

Also available via Homebrew brew tap jakehilborn/jakehilborn && brew install displayplacer