Objective C - 如何使用给定的窗口 ID 以编程方式调整窗口大小?

发布于 2024-08-14 09:25:34 字数 443 浏览 3 评论 0原文

如何使用 Objective-C / Cocoa 以编程方式调整任何应用程序的窗口大小?到目前为止,我已经获得了应用程序名称和窗口号,但不知道如何访问该窗口。

我可以用 AppleScript 做到这一点,但想用 Objective C 来学习它。

AppleScript 示例:

tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
end tell


tell application frontApp
    set bounds of window 1 to {(screenWidth / 2), 0, screenWidth, screenHeight}
end tell

感谢您的建议。

How to resize a window of any application programmatically with objective-c / cocoa? So far I've got the app name and the window number but don't know how to access the window.

I could do it with AppleScript but want to learn it with objective c.

AppleScript example:

tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
end tell


tell application frontApp
    set bounds of window 1 to {(screenWidth / 2), 0, screenWidth, screenHeight}
end tell

Thanks for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小姐丶请自重 2024-08-21 09:25:34

在这种情况下,AppleScript 确实是完成这项工作的正确工具。告诉另一个应用程序调整其窗口大小是一个自动化问题。 “在 Cocoa 中”编写该代码只会意味着从较低级别构建/发送 AppleEvents 的工作量更大。

我会尝试使用 NSAppleScript< /a> 加载并执行 Cocoa 程序中的脚本。如果您学会了如何做到这一点,您就会知道如何将 AppleScript 嵌入您的 Objective-C 程序中,这是您的实用工具中的一个方便的技巧。

祝你好运!

In this case, AppleScript really is the right tool for the job. Telling another application to resize it's window in is an automation problem. Writing that code "in Cocoa" would just mean more work building/sending AppleEvents from a lower level.

I would try using NSAppleScript to load and execute that script you have from within a Cocoa program. If you learn how to do that, you'll know how to embed AppleScript in your Objective-C programs, and that's a handy trick to have in your utility belt.

Good luck!

意犹 2024-08-21 09:25:34

您将需要以某种方式访问​​辅助系统。我不知道您需要什么函数或对象或任何东西来查看如何将消息发送到窗口的可访问性。

哦,但我可以给你一个我知道可以做同样事情的软件的链接,
https://github.com/fikovnik/ShiftIt

在那里寻找答案。

我敢打赌,如果您在代码中查找 accessibilitySetValue 函数,您可能会发现金子。

You're going to need to access the accessibility system somehow. I have no idea what function or object or whatever you need to look at how to send messages to the accessibility of windows.

Oh, but I can give you a link to software I know that does the same thing,
https://github.com/fikovnik/ShiftIt

Look in there for your answer.

I bet if you look around in that code for the accessibilitySetValue function you might strike gold.
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSAccessibility_Protocol/Reference/Reference.html#//apple_ref/occ/instm/NSObject/accessibilitySetValue:forAttribute:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文