Search code examples
visual-studio-codeapplescript

Bounds of Visual Studio Code and AppleScript


How can I get to position and size of a window of the code editor Visual Studio Pro with AppleScript?

This is what I have tried:

tell application "Visual Studio Code" to get the bounds of window 1
--> error "Visual Studio Code got an error: Can’t get bounds of window 1." number -1728 from bounds of window 1

tell application "System Events"
    tell process "Visual Studio Code"
        position of window 1
        size of window 1
    end tell
end tell
--> error "System Events got an error: Can’t get process \"Visual Studio Code\"." number -1728 from process "Visual Studio Code"

Solution

  • Visual Studio Code does not have an AppleScript dictionary file, e.g.:

    ${AppName}.app/Contents/Resources/${AppName}.sdef

    Therefore, scripting it with AppleScript will be limited.

    Visual Studio Code's executable is:

    Visual Studio Code.app/Contents/MacOS/Electron

    There are other apps that use Electron and one may have more then one app running that uses Electron, which can further complicate trying to use AppleScript with it.

    That said, the following example AppleScript code works for me:

    tell application "System Events" to ¬
        get {position, size} of window 1 of process "Electron"