使用AppleScript设置窗口的位置

时间:2012-10-09 15:52:48

标签: macos cocoa applescript finder appleevents

我正在尝试使用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尝试相同的代码,它可以工作。但是大多数其他应用程序都不起作用。

3 个答案:

答案 0 :(得分:9)

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

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

enter image description here

答案 1 :(得分:6)

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

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

答案 2 :(得分:1)

在撰写本文时,其他解决方案在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