在 AppleScript 中调整不可编写脚本的应用程序的窗口大小
我正在开发一个应用程序,可以移动 OSX 上其他应用程序的窗口并调整其大小。 主要应用程序是用 Cocoa 编写的,但调整大小部分是用 AppleScript 完成的,因为 Cocoa 似乎没有这种功能。
以下是常规调用 ActionScript 的结果(作为字符串传递给 NSAppleScript):
tell application "TextEdit"
set currentWindow to the window id 5184
set bounds of the currentWindow to {2855, 218, 3790, 578}
end tell
窗口 ID 是使用 OSX 10.6 中引入的 CGWindowListCopyWindowInfo API 获取的。
此方法适用于大多数窗口,但不适用于不可编写脚本的应用程序。 最突出的例子是 OSX 自己的预览版。
我尝试过使用此代码的变体“告诉”系统事件而不是预览
tell application "System Events"
set bounds of window 5184 to {1920, -502, 2855, 578}
end tell
但是,OSX 给了我一条错误消息:
"System Events got an error: Can’t set bounds of window 5184 to {1920, -502, 2855, 578}."
当试图获取对窗口的引用时也会发生同样的情况:
tell application "System Events"
get window 5184
end tell
我已经仔细检查了窗口是否存在以及窗口 ID是正确的。
在 OSX 上以编程方式调整不可编写脚本的窗口大小的正确方法是什么? 我可以从 moom 等应用程序中看到这是可能的。
任何建议 - 无论是基于 Cocoa 或 AppleScript 或完全其他的东西 - 都非常受欢迎。
I am working on an application that moves and resizes windows of other applications on OSX.
The main application is written in Cocoa but the resizing part is done in AppleScript as Cocoa does not seem to have this kind of feature.
Here is the result of a regular call to ActionScript (passed as a string to NSAppleScript):
tell application "TextEdit"
set currentWindow to the window id 5184
set bounds of the currentWindow to {2855, 218, 3790, 578}
end tell
The Window ID is obtained using the CGWindowListCopyWindowInfo API introduced in OSX 10.6.
This approach works fine for most windows but fails for applications that are unscriptable.
The most prominent example is OSX's own Preview.
I have tried "telling" System Events instead of Preview using variations of this code
tell application "System Events"
set bounds of window 5184 to {1920, -502, 2855, 578}
end tell
However, OSX gives me an error message:
"System Events got an error: Can’t set bounds of window 5184 to {1920, -502, 2855, 578}."
The same happens when just trying to get a reference to the window:
tell application "System Events"
get window 5184
end tell
I have doublechecked that the window exists and the window ID is correct.
What is the proper way to programmatically resize unscriptable windows on OSX?
I can see it is possible from applications such as moom.
Any advice - be it Cocoa- or AppleScript-Based or something else entirely - is more than welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,应用程序窗口的AppleScripting取决于应用程序的开发人员——一般来说没有干净的方法来做到这一点。看看我几年前为所有特殊情况编写的脚本:
Unfortunately, the AppleScripting the windows of an app is up to the developer of the app -- there's no clean way to do it in general. Look at this script I wrote some years ago for all the special cases:
您提到了预览,因此在预览中打开一个窗口并运行此脚本。
查看返回的属性。没有“id”属性,因此您无法以这种方式访问窗口。没有“bounds”属性,因此您无法设置边界。另请注意,您必须从系统事件调用中的“进程”获取窗口。因此,如果您想使用系统事件,您的代码还很遥远。
最好的选择是找到您想要定位的窗口的名称。然后在 applescript 中,您将获得像我上面那样的 Windows,然后循环检查它们的名称。当您找到正确的名称时,您就找到了合适的窗口。然后您可以设置该窗口的“大小”和“位置”属性。
祝你好运,因为你的任务很艰巨!
You mentioned Preview, so open a window in Preview and run this script.
Look at the properties that are returned. There is no "id" property so you can't access windows that way. There is no "bounds" property so you can't set the bounds. Also notice that you have to get the windows from a "process" in the system events call. So if you want to use system events your code is pretty far off.
Your best bet would be to find the name of the window you want to target. Then in applescript you get theWindows as I have above, then loop through them checking their name. When you find the proper name you have found the appropriate window. Then you could set the "size" and "position" properties of that window.
Good luck because your task is a large one!
我在尝试使用与您所使用的逻辑类似的逻辑来调整应用程序的大小时遇到了同样的问题。
就我而言,我需要调整应用程序的大小以匹配屏幕大小,我发现如果您授予脚本编辑器辅助访问。如果您将脚本保存为应用程序 您将需要授予该应用程序访问权限。
以下脚本只是获取屏幕大小,重新打开应用程序以确保其已打开,然后使用“系统事件”设置前窗口的大小。
注意:我的目标是下面一个名为“cefclient”的应用程序
I was having the same issue trying to resize an application using similar logic to what you were using.
In my case I needed to resize an application to match the screen size and I found that you could use "System Events" if you grant script editor assistive access. If you save the script as an app you will need to grant that app access instead.
The following script simply grabs the screen size, reopens the app to make sure it is open, then uses "System Events" to set the size of the front window.
NOTE: I'm targeting an application called "cefclient" below