Resizing a window to a fixed size using Raycast on Mac

Resizing a window to a fixed size using Raycast on Mac

Here is a Raycast script to set size of any window to a pre-defined size

Here is my modified version of this script to set my active window to 1280 x 720 px on the main display. I need this for my screen recording software.

#!/usr/bin/swift

// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Regular Window Size
// @raycast.mode silent

// Optional parameters:
// @raycast.icon 🔲
// @raycast.packageName Window Management

// Documentation:
// @raycast.description Resize the frontmost window to 1280x720 pixels and center it on the screen.
// @raycast.author Santosh Srinivas
// @raycast.authorURL https://www.n0c0de.com

import AppKit

// Define the target window size
let targetWidth: Double = 1280
let targetHeight: Double = 720

if let screen = NSScreen.main {
    let visibleFrame = screen.visibleFrame

    let visibleFrameWidth = Double(visibleFrame.width)
    let visibleFrameHeight = Double(visibleFrame.height)

    // Calculate the height of the menu bar and other top elements
    let menuBarHeight = Double(screen.frame.height) - Double(screen.visibleFrame.height)
        - (Double(screen.visibleFrame.origin.y) - Double(screen.frame.origin.y))

    // Calculate the position to center the window
    let left = (visibleFrameWidth - targetWidth) / 2.0
    let top = menuBarHeight + (visibleFrameHeight - targetHeight) / 2.0

    // AppleScript to set the window size and position
    let appleScript = """
        tell application "System Events" to tell application processes whose frontmost is true
            tell window 1
                set {size, position} to { {\(targetWidth), \(targetHeight)}, { \(left), \(top) } }
            end tell
        end tell
    """

    var error: NSDictionary?
    if let scriptObject = NSAppleScript(source: appleScript) {
        scriptObject.executeAndReturnError(&error)

        // Optional: Handle any errors
        if let error = error {
            print("AppleScript Error: \(error)")
        }
    } else {
        print("Failed to create AppleScript object.")
    }
} else {
    print("No main screen detected.")
}

Let's Connect

Built with 1pg.notion.lol