How to you find which monitor has the menu bar in AppleScript

applescriptdisplaydisplayportmenu bar

Playing with AppleScript I want to manipulate the location of a few windows with position but I'm having an issue defining the monitor. Currently I have several different size and brand monitors I connect to with my MacBook during my travels. The only repeating occurrence with all four monitors I connect to is that the monitor I want to manipulate includes the menu bar.

When testing with do shell script "system_profiler SPDisplaysDataType I'm not sure if the returned text of Main Display: Yes considers the main to be where the menu bar resides from the extraction:

Graphics/Displays:

    Intel Iris Pro:

      Chipset Model: Intel Iris Pro
      Type: GPU
      Bus: Built-In
      VRAM (Dynamic, Max): 1536 MB
      Metal: Supported
      Displays:
        Color LCD:
          Display Type: Built-In Retina LCD
          Resolution: 2880 x 1800 Retina
          Mirror: Off
          Online: Yes
          Rotation: Supported
          Automatically Adjust Brightness: Yes
          Connection Type: DisplayPort
        Thunderbolt Display:
          Display Type: LCD
          Resolution: 2560 x 1440
          Main Display: Yes
          Mirror: Off
          Online: Yes
          Rotation: Supported
          Automatically Adjust Brightness: No
          Connection Type: DisplayPort

If my assumption of the Main Display is correct how should I properly grep the resolution of the identified main display so I can manipulate the windows?

When I research it would appear some commonly just step into each display:

So I don't know if this would be two questions in one. I'm somewhat confused on how the sequence is determined when connecting to multiple monitors (such as today I might be connected to a thunderbolt display but tomorrow I might be connected to two Dells monitors through my two DisplayPorts) and if there is a way to target a monitor in particular based on the menu bar.

I can already detect the app and it's size with:

tell application "System Events" to tell application process "Notes" to set theSize to get size of window 1
set theWidth to item 1 of theSize
set theHeight to item 2 of theSize

but my issue falls in determining the resolution so I can calculate the position and bounds to move the app window.

Best Answer

UPDATE

Removed my original answer. Here is a completely different approach which I prefer over my original solution. This version actually uses “Image Events” scripting addition.

This script will get the names of the displays connected to your computer. The returned values of “display "Display 1" will always be “which monitor has the menu bar in AppleScript?”

property activeDesktop : missing value
property activeDesktopResolution : missing value

set displayNames to {}

tell application "Image Events"
    set theDisplays to displays
    repeat with i from 1 to number of items in theDisplays
        set this_item to item i of theDisplays
        set theName to name of display profile of item i of theDisplays
        set end of displayNames to theName
        set activeDesktop to item 1 of displayNames
    end repeat
end tell

tell application "Finder"
    set activeDesktopResolution to bounds of window of desktop
    set activeDesktopResolution to items 3 thru 4 of activeDesktopResolution
end tell

set theResultz to display dialog activeDesktop & "  " & item 1 of activeDesktopResolution & " x " & item 2 of activeDesktopResolution ¬
    buttons ¬
    "OK" with title ¬
    "Your Current Display and Its Resolution" with icon 1 ¬
    giving up after 10

enter image description here enter image description here enter image description here enter image description here enter image description here