Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
like image 553
Nrc Avatar asked Oct 20 '25 01:10

Nrc


2 Answers

You can use the process named "Code":

tell application "System Events" to tell process "Code"
    tell window 1
        set size to {1024, 768}
        set position to {100, 100}
    end tell
end tell
like image 87
davegaeddert Avatar answered Oct 22 '25 04:10

davegaeddert


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"
like image 38
user3439894 Avatar answered Oct 22 '25 03:10

user3439894



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!