Set Exact Height and Width of VS Code Window in Pixels on Mac Using AppleScript

Published on 2025-06-07

Executing the following AppleScript will position the VS Code window exactly 100 pixels from the left and 90 pixels from the top of the screen and resize it to precisely 1600×900 pixels:

tell application "System Events"
    tell process "Code"
        set frontmost to true
        
        if (count of windows) > 0 then
            tell window 1
                -- change following two lines based on your desired position & window_size and your screen resolution
                set position to {100, 90}
                set size to {1600, 900}
            end tell
        else
            display dialog "No VS Code window is open" buttons {"OK"} default button "OK"
        end if
    end tell
end tell

I often use VS Code screenshots for presentations, blog posts, and documentation. macOS’s built-in window screenshot feature (Cmd+Shift+4, then Spacebar, then click the window) is fantastic because it captures the window beautifully, complete with its subtle shadow and rounded corners.

However, certain times I need the image to be a specific size, like 1600x900 pixels, or a specific aspect ratio, like 16:9. The built-in screenshot feature doesn’t allow you to set the precise size of the window before taking the screenshot, which means I’d have to crop the image afterward in some image editor, which is a hassle, especially if I need to take multiple screenshots. Cropping also messes up the lovely shadows and clean edges that the built-in screenshot feature captures.

In such situations, I use this AppleScript to set the VS Code window to the exact size I need and then take a screenshot. There is one caveat though: the built-in screenshot feature adds padding around the window, which means the image size will be slightly larger than the window size. So here is my recommended workflow:

  1. If you need a specific image size in pixels, check if you have or haven’t scaled the display in System Preferences.
  2. Based on the scaling and your desired image size (or aspect ratio), adjust the window size in the AppleScript.
  3. Run the AppleScript to position and resize the VS Code window.
  4. Use the built-in screenshot feature (Cmd+Shift+4, then Spacebar, then click the window while holding the Option key) to capture the window without padding. Remember to hold the Option key while clicking the window to avoid the padding around the window in the screenshot and to get the exact size image you want.
  5. Use the screenshot as needed in your presentations, blog posts, or documentation.

To run this AppleScript:

  1. Open Script Editor (found in Applications > Utilities) or use Spotlight (Cmd+Space) and type “Script Editor”.
  2. Copy and paste the code above into the new Script Editor window.
  3. Set the desired position and size in the script.
  4. Click the Run button (▶️) or press ⌘R to execute the script.

The VS Code window should now be positioned and resized as specified.

You can also save this script as an application for easy access in the future!