You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过脚本或Automator任务将应用及Finder窗口设为预设大小与位置?

Absolutely! You can absolutely automate window positioning and resizing for apps and Finder windows on macOS—using either Automator (for simpler, point-and-click workflows) or AppleScript (for precise, customizable control, often paired with background monitors to trigger automatically when apps open). Let’s walk through exactly how to set this up for your specific examples:

Using Automator for Quick Visual Workflows

If you prefer avoiding raw code, Automator can handle basic window setup with a mix of built-in actions and a tiny bit of AppleScript:

  1. Open Automator and create a new Application document.
  2. Search for and add the Run AppleScript action to your workflow.
  3. Replace the default placeholder script with one of the example snippets below (or combine multiple scripts into one).
  4. Save the app, then run it manually, or set it to open automatically at login via System Settings > General > Login Items.

Using AppleScript (Flexible & Customizable)

AppleScript is the most powerful way to control window behavior—it lets you target specific apps, adjust positions pixel-perfectly, and handle multiple windows at once. Here are scripts tailored to your requests:

Example 1: Sublime Text (Top Half) & Chrome (Bottom Half)

Save this as a .scpt file with Script Editor, then run it manually or set it to trigger automatically:

-- Position Sublime Text to top half of screen
tell application "System Events"
    tell application process "Sublime Text"
        if exists (first window whose visible is true) then
            set frontWindow to first window whose visible is true
            set {screenWidth, screenHeight} to {width, height} of screen 1
            set position of frontWindow to {0, 22} -- 22px accounts for the menu bar
            set size of frontWindow to {screenWidth, (screenHeight / 2) - 22}
        end if
    end tell
end tell

-- Position Google Chrome to bottom half of screen
tell application "System Events"
    tell application process "Google Chrome"
        if exists (first window whose visible is true) then
            set frontWindow to first window whose visible is true
            set {screenWidth, screenHeight} to {width, height} of screen 1
            set position of frontWindow to {0, screenHeight / 2}
            set size of frontWindow to {screenWidth, screenHeight / 2}
        end if
    end tell
end tell

Example 2: Four Finder Windows (Each Quarter of Screen)

This script creates four Finder windows (or uses existing ones) and positions them in a clean grid:

tell application "Finder"
    set {screenWidth, screenHeight} to {width, height} of bounds of window of desktop
    set quarterWidth to screenWidth / 2
    set quarterHeight to (screenHeight - 22) / 2 -- Subtract menu bar height

    -- Ensure 4 Finder windows exist (create new ones if missing)
    repeat with i from 1 to 4
        if not (exists window i) then
            make new Finder window
            set target of window i to home -- Swap "home" with "Downloads" or another folder if preferred
        end if
    end repeat

    -- Position each window in a quadrant
    set bounds of window 1 to {0, 22, quarterWidth, quarterHeight + 22} -- Top-left
    set bounds of window 2 to {quarterWidth, 22, screenWidth, quarterHeight + 22} -- Top-right
    set bounds of window 3 to {0, quarterHeight + 22, quarterWidth, screenHeight} -- Bottom-left
    set bounds of window 4 to {quarterWidth, quarterHeight + 22, screenWidth, screenHeight} -- Bottom-right
end tell

Automatically Trigger Scripts When Apps Open

To make these scripts run automatically when an app launches, use a Launch Agent (a macOS background process monitor):

  1. Save your AppleScript as a .scpt file (e.g., WindowPositions.scpt) in a safe location like ~/Scripts/.
  2. Create a new plist file (e.g., com.user.windowpositions.plist) in ~/Library/LaunchAgents/ with this content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.user.windowpositions</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>/Users/YourUsername/Scripts/WindowPositions.scpt</string>
    </array>
    <key>WatchPaths</key>
    <array>
        <string>/Applications/Sublime Text.app</string>
        <string>/Applications/Google Chrome.app</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
  1. Replace /Users/YourUsername/ with your actual user path.
  2. Load the launch agent in Terminal:
launchctl load ~/Library/LaunchAgents/com.user.windowpositions.plist

Now, whenever you open Sublime Text or Chrome, the script will run automatically to position their windows.


Key Notes
  • Accessibility Permissions: You’ll need to grant accessibility access to Script Editor, Terminal, and any Automator apps you create. Go to System Settings > Privacy & Security > Accessibility and toggle them on—without this, the scripts can’t control window behavior.
  • Notched Macs: If you have a Mac with a notch, adjust the menu bar height from 22 to 28 in the scripts to account for the extra screen space.
  • Multiple Windows: To handle apps that open multiple windows by default, modify the scripts to loop through all visible windows instead of just the first one.

内容的提问来源于stack exchange,提问作者John Vaughters

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:06:46