Can Someone Explain Why These Scripts Work?
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: