macos - 用于运行检测显示器的 Applescript

标签 macos applescript

当我将外部显示器插入 Macbook 并唤醒它时,显示器的分辨率通常是错误的。在使用 Mountain Lion 之前,我能够运行以下 applescript 来检测显示器:

tell application "System Preferences" to activate
tell application "System Events"
    tell process "System Preferences"
        click menu item "Displays" of menu "View" of menu bar 1
        tell button "Detect Displays" of window 1 to click
    end tell
end tell
tell application "System Preferences" to quit

但是,在 10.8 中,“检测显示”按钮要求您按 Option 键才能显示它,因此脚本会给出以下错误:

error "System Events got an error: Can’t get button \"Detect Displays\" of window 1 of process \"System Preferences\"." number -1728 from button "Detect Displays" of window 1 of process "System Preferences"

我的 applescript 技能还不够初级,而且我的 google-fu 还没有让我偶然找到答案。

如何修改脚本以单击现在隐藏的检测显示按钮?

最佳答案

试试这个...

tell application "System Preferences"
    activate
    reveal pane "com.apple.preference.displays"
end tell

delay 0.5

tell application "System Events"
    tell process "System Preferences"
        try --don't even consider not using a try block!
            key down option
            delay 0.2
            click button "Detect Displays" of window 1
            delay 0.2
            key up option
        on error --logging out is the only other way to clear these
            key up option
        end try
    end tell
end tell

关于macos - 用于运行检测显示器的 Applescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12640643/

相关文章:

macos - 如何在 Mac OS X 上打开 Emacs 以便它加载我的 $PATH?

macos - 在 Terminal.app 中,选项+箭头键不再按单词移动

macos - Mac OS X : Keyboard shortcut for moving active window to another screen

macos - 对于macOS Cocoa,如何指定屏幕上的某个窗口或矩形进行截图?

macos - 更改文档参数后,NSCocoaErrorDomain 256 打开*新*文档

python - 从 VBA 单词运行 python 并返回一个值

使用applescript zip 文件夹

objective-c - NSScrollView 在 Cocoa 中底部有空间并且滚动条始终位于底部

perl - shell/mac终端中的二维码生成

ruby - 将 Ruby 脚本从 Linux 盒子发送到远程 mac mini 以打开和关闭应用程序