无法使用 Applescript 调整窗口大小

ous*_*ong 1 applescript window-resize rstudio

我使用脚本编辑器编辑了以下 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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

use*_*894 6

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

\n

AppleScript 代码示例

\n
tell application "System Events" to \xc2\xac\n    get properties of window 1 of application process "RStudio"\n
Run Code Online (Sandbox Code Playgroud)\n

返回,例如:

\n
{minimum value:missing value, orientation:missing value, \nposition:{140, 25}, class:window, accessibility description:missing value, \nrole description:"standard window", focused:true, title:"RStudio", \nsize:{1020, 750}, help:missing value, entire contents:{}, \nenabled:missing value, maximum value:missing value, role:"AXWindow", \nvalue:missing value, subrole:"AXStandardWindow", selected:missing value, \nname:"RStudio", description:"standard window"}\n
Run Code Online (Sandbox Code Playgroud)\n

正如您所看到的,没有bounds property,因此将使用position和,例如:size

\n
tell application "System Events"\n    tell application process "RStudio"\n        tell window 1\n            set position to {140, 25}\n            set size to {1020, 750}\n        end tell\n    end tell\nend tell\n
Run Code Online (Sandbox Code Playgroud)\n
\n

笔记:

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

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

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

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

\n