如何使用Automator调整Visual Studio Code窗口大小?

3

我正在尝试录制屏幕演示,为此,我希望每次记录VSCode窗口时都以相同的大小记录。 为此,我尝试使用自动化脚本调整窗口大小。 它在我测试过的所有应用程序中都有效,但在VSCode中无效。

这是脚本:

(* 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

这就是当我尝试调整VSCode大小时发生的错误。有人知道这个错误可能是什么吗?或者有人知道其他强制窗口大小的方法吗?

enter image description here

1个回答

5

bounds 属性和 window 元素仅适用于可编写脚本的应用程序。包括 Microsoft Visual Studio Code 在内的大多数应用程序都不可编写脚本,因此您当前的脚本将无法在这些应用程序中运行。

要操作不可编写脚本的应用程序窗口,您需要使用 系统事件 编写 UI 脚本:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接