Apple Script - How do I check if the bounds of a window are equal to specific values?

I know that the following code can adjust the bounds of a specific window

set bounds of (front window of application "Notes") to {0, 0, 540, 300}

But how do I check if the bounds of a window are equal to specific values?

(macOS Catalina)


Solution 1:

The following example AppleScript code uses the value of the bounds for Note in your OP to check if the current bounds matches the given list:

tell application "Notes"
    if (bounds of front window) is not equal to {0, 0, 540, 300} then
        set bounds of front window to {0, 0, 540, 300}
    end if
end tell

It can also be written like:

tell application "Notes" to ¬
    if (bounds of front window) ¬
        is not equal to {0, 0, 540, 300} then ¬
        set bounds of front window to {0, 0, 540, 300}

Or using a variable assignment:

tell application "Notes"
    set checkBounds to bounds of front window
    if checkBounds is not equal to {0, 0, 540, 300} then
        set bounds of front window to {0, 0, 540, 300}
    end if
end tell

Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.