Check AppleScript Window Position: Left or Right
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.
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:
tell application "YourApplicationName"
set windowLeft to left of front window
end tell
Return `windowLeft`
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"
set left of front window to 100
end tell
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"
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?