MacBook – Can Someone Explain Why These Scripts Work

applescriptdual-screenmacbook pro

I'm a brand new AppleScript initiate. I was at a Developer Summit and needed to stop messing with my windows so much. So I looked up scripting the window sizing and positioning. I was on my MacBook Pro and so I wrote the following code and placed in Automator as a Service whic I then saved to HardDrive>Users>User>Library>Services.

tell application "System Events"
    set myTargetApplication to name of the first process whose frontmost is true
end tell

tell application myTargetApplication
    --  This line is what we use when we need to grab that bounds of a window.
    -- set currentBounds to bounds of the first window
    set bounds of the first window to {61, 46, 1261, 777}
end tell

I would write the script in "Script Editor" and test it with the commented out line uncommented and the line after it commented. Then I could position the window like I want and get the settings I needed to apply. I got the settings, and pasted them in the line right before "end tell" and they worked.

I set this as a service and now I can make ANY window (from Service) size to what I needed.

I arrived home and connected to my dual monitor, a 32" Acer, and everytime I would run the script in my big monitor it would JUMP to the MacBook Window.

So I tweaked this and that, tried several things, looked things up, and during a run did this:

tell application "System Events"
    set myTargetApplication to name of the first process whose frontmost is true
end tell

tell application myTargetApplication
    --  This line is what we use when we need to grab that bounds of a window.
    -- set currentBounds to bounds of the first window
    set bounds of the first window to {1299, 38, 3182, 1062}
end tell

I did the same thing in Automator… saved as a service… and now they work.

EXACTLY LIKE I WANT.

I am not COMPLAINING… I'm just lost…

I don't see WHY they work. I'm hoping someone can point out what makes them work so I can better understand AppleScript.

Thank you in advance

Best Answer

The smallest rectangle containing both "real" monitors is your virtual desktop.

The upper left corner of the monitor containing the menubar is the (0|0) coordinate. Moving rightwards and downwards increases the numbers.

If you have one monitor (1920|1200) the upper left corner is the (0|0) and bottom right corner has the coordinate (1920|1200) and the whole desktop is described by {0, 0, 1920, 1200}

If you have two equally sized monitors (1920|1200) arranged side by side and the left one is your main monitor the coordinates of the complete desktop are {0, 0, 3840, 1200}. If the right monitor is the main one: {-1920, 0, 1920, 1200}.

If you have two differently sized monitors (1920|1200) and (2000|1400) arranged with an off-set (e.g. the bottom right corner of the small monitor (1920|1200) is right next to the upper left corner (0|0) of the other monitor and the left one is the main one) then the desktop is described by {0, 0, 3920, 2600}. If the right one is the main monitor the whole desktop is represented by {-1920, -1200, 2000, 1400}.

So your window {1299, 38, 3182, 1062} in the arrangement marked in bold would look like this:

enter image description here