applescript check window position left right

Check AppleScript Window Position: Left or Right

By Robert Walker
AD

Applescript, a robust programming language developed by Apple, enables automation of complex tasks on macOS. A common use of Applescript is to interact with applications' windows, such as checking and manipulating window positions. This can be particularly useful when you want to ensure your application windows are perfectly arranged on your screen, especially useful for multi-monitor setups or when working with a specific window layout.

In this guide, we will focus on checking and adjusting the horizontal position (left-right) of application windows using Applescript. Before we delve into the specific scripts, let's first understand the basic Applescript syntax for window positions.

Understanding Applescript Window Position Syntax

The position of an application window in Applescript is represented as an offset from the top-left corner of the screen, using X and Y coordinates. The X-coordinate represents the horizontal (left-right) position, while the Y-coordinate represents the vertical (up-down) position.

AD
the windows xp task failed successfully
the windows xp task failed successfully
the window mode in windows 7 is highlighted by red arrow pointing to the left side
the window mode in windows 7 is highlighted by red arrow pointing to the left side
Notepad finally gets spellcheck, autocorrect for all Windows 11 users
Notepad finally gets spellcheck, autocorrect for all Windows 11 users

When working with window positions, you'll use properties like `left`, `top`, `right`, and `bottom` to reference the window's edges. For example, to reference the left edge of a window, you would use `left`. The position of an edge is essentially the X-coordinate for the left and right edges, and the Y-coordinate for the top and bottom edges.

Fetching a Window's Current Left Position

To check the current left position of a window, you can use the `position` property of the `window` object. Here's a basic Applescript to fetch the left position of the frontmost window of the specified application:

the error screen in windows xp's task window, which appears to have been blocked
the error screen in windows xp's task window, which appears to have been blocked
Shortcut #12
Shortcut #12
an image of a computer screen with windows
an image of a computer screen with windows

tell application "YourApplicationName"

set windowLeft to left of front window
end tell

Return `windowLeft`

AD

an app showing different types of windows and how to use them in the home design
an app showing different types of windows and how to use them in the home design
27 Different Types of Windows for Your Home (Featuring Descriptive Diagrams)
27 Different Types of Windows for Your Home (Featuring Descriptive Diagrams)
an image of a window with the measurements
an image of a window with the measurements
a red alert sign that says windows warp engines are about to explode do you want to fiddle
a red alert sign that says windows warp engines are about to explode do you want to fiddle
an open window with the words anatomy of a window above it and below it, there is
an open window with the words anatomy of a window above it and below it, there is
an open window in a room with pictures on the wall and two stools next to it
an open window in a room with pictures on the wall and two stools next to it

Adjusting a Window's Left Position

To change the left position of a window, you simply set the value of the `left` property. Remember, the left position is relative to the screen, so setting it to a different value will move the window accordingly:

tell application "YourApplicationName"

AD
set left of front window to 100
end tell

Student Check-in
Student Check-in
🚀 Stop clicking through menus every day! Master these 10 Windows
🚀 Stop clicking through menus every day! Master these 10 Windows
various types of windows and their names on a white background with the words window styles written below them
various types of windows and their names on a white background with the words window styles written below them
Why I left Mac for Windows: Apple has given up
Why I left Mac for Windows: Apple has given up
the instructions for windows 7 shortcuts are shown in black and white, with an arrow
the instructions for windows 7 shortcuts are shown in black and white, with an arrow
How To Take Screenshots on Windows PC 11, 10, 8, 7
How To Take Screenshots on Windows PC 11, 10, 8, 7
The Free Way to Run Windows on an Apple Silicon Mac
The Free Way to Run Windows on an Apple Silicon Mac
Swift Shift Is the Window Management Tool Apple Should Have Built
Swift Shift Is the Window Management Tool Apple Should Have Built
Can Apple Intelligence—or any AI, really be private?
Can Apple Intelligence—or any AI, really be private?
the change case window in microsoft's office suite is highlighted by an orange arrow
the change case window in microsoft's office suite is highlighted by an orange arrow
Do you know how to check for costly leaks in your home?
Do you know how to check for costly leaks in your home?
How to Force Quit an App on an iMac or Macbook
How to Force Quit an App on an iMac or Macbook
11 conseils pour vous aider à corriger l'erreur d'écran bleu de Windows
11 conseils pour vous aider à corriger l'erreur d'écran bleu de Windows
New automation features are coming to macOS in Shortcuts—but not for every app
New automation features are coming to macOS in Shortcuts—but not for every app

Working with the Screen's Bounds

When adjusting window positions, it's crucial to consider the screen's bounds. You can fetch the screen's bounds (left, top, right, and bottom edges) using the `bounds` property of the `window` object, with a reference to the `desktop` as follows:

tell application "System Events"

AD
set desktopBounds to bounds of desktop 1
end tell

Return `desktopBounds`

You can then use the `desktopBounds` to ensure your window positions fall within the screen's boundaries.

centering a Window Horizontally

Here's an Applescript that centers the frontmost window of the specified application horizontally on the screen:

tell application "System Events"

set desktopBounds to bounds of desktop 1
end tell

tell application "YourApplicationName"

set windowLeft to (item 1 of desktopBounds) + (item 3 of desktopBounds - item 1 of desktopBounds) / 2
set left of front window to windowLeft
end tell

Aligning Windows Along the Left or Right Screen Edge

To align the left edge of a window with the left or right screen edge, you can set the window's `left` property to the corresponding edge value:

tell application "System Events"

set desktopBounds to bounds of desktop 1
end tell

tell application "YourApplicationName"

set left of front window to item 1 of desktopBounds -- Align with left edge
-- OR --
set left of front window to item 3 of desktopBounds -- Align with right edge
end tell

Remember, when working with Applescript, comments are written between -- and the next line break or end of string. These comments are not executed by the script but help you understand or annotate your code.

Applescript offers a wealth of possibilities for automating tasks on macOS, including manipulating window positions. By understanding and utilizing the window position syntax and properties, you can create powerful and time-saving scripts to keep your desktop organized and efficient.

Now that you've learned how to check and adjust window positions using Applescript, why not explore other aspects of Applescript, such as automating menubar applications or creating keyboard shortcuts for your everyday tasks?

AD