How to resize Visual Studio Code window with Automator?

Viewed 507

I'm trying to do a screencast and for that, I want to record the VSCode window at the same size every time. For that I've tried to use an Automator script to resize the window. It works with all the applications that I've tested but not with VSCode.

This is the script:

(* This AppleScript will resize the current application window to the height specified below and center it in the screen. Compiled with code from Natanial Wools and Amit Agarwal *)

    set the_application to (path to frontmost application as Unicode text)
    set appHeight to 720
    set appWidth to 1280

    tell application "Finder"
        set screenResolution to bounds of window of desktop
    end tell

    set screenWidth to item 3 of screenResolution
    set screenHeight to item 4 of screenResolution

    tell application the_application
        activate
        reopen
        set yAxis to (screenHeight - appHeight) / 2 as integer
        set xAxis to (screenWidth - appWidth) / 2 as integer
        set the bounds of the first window to {xAxis, yAxis, appWidth + xAxis, appHeight + yAxis}
    end tell

And this is the error that happens when I try to resize the VSCode. Does anyone know what this error could be? Or does anyone know any other way to enforce the window size?

enter image description here

1 Answers

The bounds property and the window element only exist for applications that are scriptable. Most applications, including Microsoft Visual Studio Code, are not scriptable, so your current script will not work for any of these.

To manipulate the window of a non-scriptable application, you need to script the UI using System Events:

tell application "System Events"
    set [[screenW, screenH]] to process "Finder"'s scroll areas's size

    tell (the first process where it is frontmost)
        set [[null, menuH]] to the size of its menu bars
        tell the front window
            set [appW, appH] to its size
            set its position to [¬
                (screenW - appW) / 2, ¬
                (screenH - appH + menuH) / 2]
        end tell
    end tell
end tell
Related