有没有办法通过 Applescript 挂钩 OSX 睡眠/唤醒事件?

发布于 2024-08-11 13:42:59 字数 1142 浏览 8 评论 0原文

我试图解决的问题非常简单。

当我打开 MacBook 的盖子时,我喜欢将 Dock 放在屏幕的左侧,但是当我回到家并将 MacBook 连接到影院显示器并设置双显示器时,我希望将 Dock 放在屏幕的底部影院,不在 MacBook 的左侧。

我不想每次连接/断开影院显示器时都必须进入首选项。

我对 50% 的问题有一个解决方案,即我编写了以下 Applescript 代码来根据我的屏幕分辨率切换 Dock 的位置,但我必须手动调用它。

tell application "Finder" 
-- Determine Resolution
  set screenSize to bounds of window of desktop
  set screenWidth to item 3 of screenSize
  set screenHeight to item 4 of screenSize
end tell
if screenWidth is less than 1900 then    
    --MacBook Display    
    tell application "System Events"      
       tell dock preferences            
            set properties to {magnification:true, screen edge:left}
       end tell
    end tell
else
    --Cinema Display
    tell application "System Events"
        tell dock preferences
            set properties to {magnification:true, screen edge:bottom}
        end tell
    end tell
end if

我的问题是:

  • 我可以将此代码直接在 Applescript 中挂钩到 OSX 睡眠/唤醒事件吗?是否有地方可以“注册”脚本,以便在我不知道的操作系统事件上调用?

  • 如果不直接,还有哪些其他选项可以挂钩 OSX 事件?

  • 您有什么建议吗? 改进上面的 Applescript 代码?

谢谢。

The problem I am trying to solve is quite simple.

When I open the lid of my MacBook I like to have the Dock on the left side of the screen, but when I get home and connect my MacBook to my Cinema display and set up dual monitors, I want the dock on the bottom of the Cinema, not on the left side of the MacBook.

I don’t want to have to go in to preferences every time I connect/disconnect my Cinema display.

I have a solution for 50% of the problem, namely, I have written the following Applescript code to switch the location of the Dock based upon my screen resolution, but I have to manually invoke it.

tell application "Finder" 
-- Determine Resolution
  set screenSize to bounds of window of desktop
  set screenWidth to item 3 of screenSize
  set screenHeight to item 4 of screenSize
end tell
if screenWidth is less than 1900 then    
    --MacBook Display    
    tell application "System Events"      
       tell dock preferences            
            set properties to {magnification:true, screen edge:left}
       end tell
    end tell
else
    --Cinema Display
    tell application "System Events"
        tell dock preferences
            set properties to {magnification:true, screen edge:bottom}
        end tell
    end tell
end if

My questions are:

  • Can I hook this code in to OSX sleep/wake events directly within Applescript? Is there some place where you can "register" scripts to be invoked on OS events that I am not aware of?

  • If not directly, what are some other options for hooking in to OSX events?

  • Do you have any recommendations for
    improving the above Applescript code?

Thanks.

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

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

发布评论

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

评论(1

恋你朝朝暮暮 2024-08-18 13:42:59

尝试通过 Sleepwatcher 执行脚本: http://www.bernhard-baehr.de/

Try executing your script via Sleepwatcher: http://www.bernhard-baehr.de/

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