首页 > 解决方案 > 无法使用 Applescript 调整窗口大小

问题描述

我使用脚本编辑器编辑了以下 Applescript,试图调整我的 RStudio 窗口的大小,但它没有用。RStudio 已加载,但未正确调整大小。代码和错误消息附在下面。有任何想法吗?我正在使用 macOS Big Sur 11.0.1 和 RStudio 1.3.1073。谢谢!

tell application "RStudio"
    activate
    set the bounds of the first window to {140, 0, 1160, 775}
end tell

在此处输入图像描述

标签: applescriptrstudiowindow-resize

解决方案


为了在RStudio中设置窗口位置大小,您需要使用系统事件以及窗口的和的propertiespositionsize

示例 AppleScript 代码

tell application "System Events" to ¬
    get properties of window 1 of application process "RStudio"

退货,例如:

{minimum value:missing value, orientation:missing value, 
position:{140, 25}, class:window, accessibility description:missing value, 
role description:"standard window", focused:true, title:"RStudio", 
size:{1020, 750}, help:missing value, entire contents:{}, 
enabled:missing value, maximum value:missing value, role:"AXWindow", 
value:missing value, subrole:"AXStandardWindow", selected:missing value, 
name:"RStudio", description:"standard window"}

如您所见,没有bounds property,因此将使用 and ,例如positionsize

tell application "System Events"
    tell application process "RStudio"
        tell window 1
            set position to {140, 25}
            set size to {1020, 750}
        end tell
    end tell
end tell

笔记:

  • 属性list 项目,例如,不等于,而前两项确实等于,实际上需要是,因为在macOS Big Sur中, 菜单的默认值为 24 像素,因此与顶部的距离或屏幕到窗口的顶部。bounds {140, 0, 1160, 775}{position, size}list position140, 0140, 25height25

使用OP中属性的调整,以下是数字表示的内容:bounds {140, 25, 1160, 775}

  • 列表项 1:{ 140 , 25, 1160, 775} - 140是从屏幕左侧到窗口左侧的距离(以像素为单位)。
  • 列表项 2:{140, 25 , 1160, 775} - 25是从屏幕顶部到窗口顶部的距离(以像素为单位)。
  • 列表项 3: {140, 25, 1160 , 775} - 1160是从屏幕左侧到窗口右侧的距离(以像素为单位)。
  • 列表项 4:{140, 25, 1160, 775 } -- 775是从屏幕顶部到窗口底部的距离(以像素为单位)。

所以虽然 140, 25代表的是position1160, 775不是的sizesize调整后的基础bounds{1020, 750}并且是通过从 的中减去 的item 1并从 的值减去item 3值得出的。item 2item 4


推荐阅读