【问题标题】:Set position of window with AppleScript使用 AppleScript 设置窗口位置
【发布时间】:2012-09-30 00:13:48
【问题描述】:

我正在尝试使用 AppleScript 设置 Messages.app 聊天窗口的位置。

tell application "System Events"
    set position of window 1 of application "Messages" to {100, 100}
end tell

使用此代码我得到一个错误:

error "Messages got an error: Can’t set «class posn» of window 1 to {100, 100}." 
number -10006 from «class posn» of window 1

这是什么意思?

如果我使用 Finder 尝试相同的代码,它会起作用。但是大多数其他应用程序都不起作用。

【问题讨论】:

  • 脚本不起作用,因为应用程序“消息”的 AppleScript 字典中没有 position 属性。只需将of application "Messages" 替换为of process "Messages"

标签: macos cocoa applescript finder appleevents


【解决方案1】:
tell application "System Events"
    set position of first window of application process "Messages" to {100, 100}
end tell  

启用Access for assistive devices 以运行此脚本。

【讨论】:

  • 感谢截图。对于任何想知道的人,可访问性设置不再像那样。相反,您必须在 Security & Privacy > Accessibility > Privacy 中指定允许访问哪些应用程序。
【解决方案2】:

我找到了解决方案。设置窗口的边界有效。我不知道为什么使用该职位不起作用。

tell application "System Events"
    set friendBounds to {4289, 400, 4479, 1600}
    set chatBounds to {3583, 400, 4289, 1599}
    set bounds of window "Buddies" of application "Messages" to friendBounds
    set bounds of window "Messages" of application "Messages" to chatBounds
end tell

【讨论】:

  • 您不需要“系统事件”应用程序,因为bounds 是 AppleScript 应用程序字典中的窗口属性“消息”。 -- “系统事件”没有bounds 属性。您可以删除tell application "System Events" 块,脚本也可以在没有它的情况下运行。
【解决方案3】:

在撰写本文时,其他解决方案在 macOS 10.14 Mojave 上对我不起作用。我设法找到了这个解决方案:

tell application "System Events" to tell process "Safari"
    set position of window 1 to {0, 50}
    set size of window 1 to {600, 650}
end tell

【讨论】:

    猜你喜欢
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多