关于可可:如何:显示复选标记,禁用菜单项,刷新菜单栏

HOW TO: display a check mark, disable a menu item, refresh a menubar

我正在尝试使用带有applescript的菜单栏/状态脚本在Mac上设置一些简单的服务。
上下阅读网络后,请记住我是脚本新手,看来我已经达到极限了,我需要一些帮助...

首先,我想根据条件在menuItem旁边显示一个复选标记。在我的示例中,条件是显示分辨率在720p和1080p之间。

我已经根据现有脚本(其中一些我不完全理解)设置了菜单栏,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use AppleScript version"2.7"
use scripting additions
use framework"Foundation"
use framework"AppKit"

property aStatusItem : missing value

on init()
        set aBar to {"Reset Display","1080p","720p","Open Monitor Preferences...","","External Monitor: active","Quit"}
        set aStatusItem to current application's NSStatusBar's systemStatusBar()'s statusItemWithLength:(current application's NSVariableStatusItemLength)
        aStatusItem's setTitle:"FTV"
        aStatusItem's setHighlightMode:true
        aStatusItem's setMenu:(createMenu(aBar) of me)

    end init

    on createMenu(aList)
        set myDisplay to ChkDisplay()
        set aMenu to current application's NSMenu's alloc()'s init()
        set aCount to 1
        repeat with i in aList
            set j to contents of i
            if j is not equal to"" then
                set aMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:j action:"actionHandler:" keyEquivalent:"")
            else

                set aMenuItem to (current application's NSMenuItem's separatorItem())
            end if
            if j = myDisplay then (aMenuItem's setState:NSOnState)
            (aMenuItem's setTarget:me)
            (aMenuItem's setTag:aCount)
            (aMenu's addItem:aMenuItem)
            if j is not equal to"" then set aCount to aCount + 1
        end repeat
        return aMenu
    end createMenu

检查显示分辨率的处理程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
    on ChkDisplay()
    tell application"System Preferences"
        reveal anchor"displaysDisplayTab" of pane id"com.apple.preference.displays"
    end tell
    tell application"System Events"
        set myDisplay to"720p"
        tell table 1 of scroll area 1 of tab group 1 of window"Philips FTV" of process"System Preferences"
            if selected of row 1 then set myDisplay to"1080p"
        end tell
    end tell
    tell application"System Preferences" to quit
    return myDisplay
end ChkDisplay

我基本上希望复选标记从720p变为1080p,具体取决于激活的分辨率。如果单击720p和1080p项目,则还将设置显示分辨率。

我已返回的代码错误:NSOnState未定义...而我迷路了。

我要解决的第二个问题是找到一种方法:
a)"灰显"(禁用)菜单项(在这种情况下,该项"外部监视器:活动"
b)在条件

下将项目更改为"外部监视器:丢失"

我尝试过:NSMenuItem高亮显示:false和NSMenuItem enabled:false并返回和错误。
另外,我不知道如何刷新菜单和/或菜单项。

任何帮助或指点将不胜感激。
我预先感谢任何人花在我对这些问题的思考上的时间!


最终溶液

对于问题1,在@CJK帮助下,菜单项旁边显示一个复选标记,我找到了一些工作代码:

替换if j = myDisplay then (aMenuItem's setState:NSOnState)

1
if j = myDisplay then (aMenuItem's setState:1)

1
if j = myDisplay then (aMenuItem's setState:NSOnState)

我还能够使用NSimage在菜单项旁边显示任何图像(不要犹豫问我是否需要)

关于问题2,启用/禁用菜单项,我还找到了一个工作代码:

在createMenu(aList)处理程序中,您需要在重复循环之前添加第二行:

1
2
set aMenu to current application's NSMenu's alloc()'s init()
aMenu's setAutoenablesItems:false

然后在重复循环中,启用/禁用菜单项:

1
(aMenuItem's setEnabled:false)

**最后刷新菜单项,**我将代码aMenu's removeAllItems()放在createMenu(aList)处理程序中,并在需要刷新时调用该处理程序。似乎通过删除脚本开头的run / end run,一切正常!