我正在开发一个应用程序,该应用程序可以移动 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 或完全其他的东西 - 都非常受欢迎。
最佳答案
不幸的是,应用程序窗口的AppleScripting取决于应用程序的开发人员——通常没有干净的方法来做到这一点。看看我几年前为所有特殊情况编写的这个脚本:
-- Get screen bounds and origins
set f to (path to preferences from local domain as Unicode text) & "com.apple.windowserver.plist"
tell application "System Events" to set {{|Width|:w1, |Height|:h1, |OriginX|:OX1, |OriginY|:OY1}, {|Width|:w2, |Height|:h2, |OriginX|:OX2, |OriginY|:OY2}} to value of property list items of property list item 1 of property list item "DisplaySets" of property list file f
set SecondaryScreenBounds to {OX2, OY2, OX2 + w2, OY2 + h2}
set RHedge to OX1
set BOTedge to OY1
tell application "Finder"
-- Get the running apps (excluding those with special windows)
set |running| to name of processes whose visible is true and name is not "Finder" and name is not "QuickSilver" and name is not "CopyPaste" and name is not "DropCopy" and name is not "iPulse"
repeat with anApp in |running|
try -- for a scriptable App with window bounds property
tell application anApp
set allWindows to (every window)
repeat with aWindow in allWindows
set Wbounds to (get bounds of aWindow)
if item 1 of Wbounds > RHE or item 2 of Wbounds > BoE then my moveWindows(contents of anApp)
end repeat
end tell
on error -- for an App with window position & size properties
tell application "System Events"
tell application process anApp
set allWindows to (every window)
repeat with aWindow in allWindows
set {p1, p2} to aWindow's position
if p1 ≥ RHedge or p2 ≥ BOTedge then my moveWindows(contents of anApp)
end repeat
end tell
end tell
end try
end repeat
-- for the Finder
set allWindows to (every window whose visible is true)
repeat with aWindow in allWindows
set Wbounds to bounds of aWindow
if (item 1 of Wbounds) > RHedge or (item 2 of Wbounds) > BOTedge then
set bounds of aWindow to {200, 200, 1200, 800}
end if
end repeat
end tell
-- for Safari
if "Safari" is in |running| then tell application "Safari"
set Wind to name of windows
set Wbounds to bounds of windows
repeat with k from 1 to count Wind
set W to item k of Wind
set B to item k of Wbounds
if (item 1 of B) ≥ RHedge or (item 2 of B) ≥ BOTedge then
set bounds of window W to {200, 200, 1200, 800}
end if
end repeat
end tell
-- for HoudahSpot
if "HoudahSpot" is in |running| then tell application "System Events" to tell process "HoudahSpot"
set W to name of windows
set B to position of windows
repeat with k from 1 to count W
if item k of W is not missing value and (item 1 of item k of B) ≥ RHedge then set position of window (item k of W) to {100, 100}
end repeat
end tell
-- for Activity Monitor
if "Activity Monitor" is in |running| then tell application "System Events" to tell process "Activity Monitor"
set W to name of windows
set B to position of windows
repeat with k from 1 to count W
if item k of W is not missing value and (item 1 of item k of B) ≥ RHedge then set position of window (item k of W) to {100, 100}
end repeat
end tell
-- for 1Password
if "1Password" is in |running| then tell application "System Events" to tell process "1Password"
set W to name of windows
set B to position of windows
repeat with k from 1 to count W
if item k of W is not missing value and (item 1 of item k of B) ≥ RHedge then set position of window (item k of W) to {100, 100}
end repeat
end tell
-- for iCal
if "iCal" is in |running| then tell application "iCal"
set iCB to bounds of window "iCal"
if item 1 of iCB ≥ RHedge or item 2 of iCB ≥ BOTedge then
set bounds of window "iCal" to {100, 100, 1200, 1000}
end if
end tell
-- for a Help Window
tell application "System Events"
if exists process "Help Viewer" then tell process "Help Viewer"
set W to windows
repeat with w1 in W
set position of w1 to {200, 200}
end repeat
end tell
end tell
to moveWindows(anApp)
tell application "System Events"
if anApp is "ScriptLight" then
tell process "ScriptLight" to set position of window 1 to {200, 200}
else if anApp is "PowerKey" then
tell process "PowerKey" to set position of window "PowerKey" to {200, 200}
else if anApp is "Script Debugger 4" then
tell application process "Script Debugger 4"
set allWindows to (every window)
repeat with aWindow in allWindows
set {p1, p2} to aWindow's position
if p1 ≥ 1680 or p2 > 1050 then set aWindow's position to {100, 100}
end repeat
end tell
end if
end tell
end moveWindows
关于cocoa - 在 AppleScript 中调整不可编写脚本的应用程序的窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6164164/