Applescript 检查窗口是否是一个屏幕

发布于 2024-12-17 10:48:32 字数 451 浏览 0 评论 0原文

我已经编写了这个脚本函数

on GetWindowLocation()
    set front_app to (path to frontmost application as Unicode text)
    tell application front_app
        item 1 of (get bounds of front window)
    end tell
end GetWindowLocation

on GetDockStatus()
    tell application "System Events" to get the autohide of the dock preferences
end GetDockStatus

如果我在桌面上没有windows up它错误, 。 如何检查窗口是否在屏幕上,以便我可以添加 if 语句来不显示 如果屏幕上没有窗口,请运行它。

I have written this script function

on GetWindowLocation()
    set front_app to (path to frontmost application as Unicode text)
    tell application front_app
        item 1 of (get bounds of front window)
    end tell
end GetWindowLocation

on GetDockStatus()
    tell application "System Events" to get the autohide of the dock preferences
end GetDockStatus

if I am on the desktop with no windows up it errors.
How can I check if a window is on screen, so I can put an if statement to not
run it if a window is not on the screen.

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

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

发布评论

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

评论(1

撑一把青伞 2024-12-24 10:48:32

这里最简单的修复方法可能就是简单地捕获错误:

tell application front_app
    try
        return item 1 of (get bounds of front window)
    on error
        -- do something here to handle there being no front window
    end try
end tell

您还可以在尝试引用前窗口之前尝试检查窗口数,但这更容易出现问题错误(因为窗口可能会在您抓住其边界之前消失)。

Probably the easiest fix here would be to simply catch the error:

tell application front_app
    try
        return item 1 of (get bounds of front window)
    on error
        -- do something here to handle there being no front window
    end try
end tell

You could also try checking the count of windows before trying to reference the front window, but that's somewhat more prone to errors (as the window may disappear before you grab its bounds).

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