HOW TO:显示复选标记,禁用菜单项,刷新菜单栏 [英] HOW TO: display a check mark, disable a menu item, refresh a menubar

查看:137
本文介绍了HOW TO:显示复选标记,禁用菜单项,刷新菜单栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to set up some simple services on a Mac using a menubar/status script with applescript. After having read the web up and down, bearing in mind I am new to scripting, it seems I have reached my limit and I need some help...

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

First, I want to display a check mark next to a menuItem on a condition. In my example the condition is the display resolution between 720p and 1080p.

我已经根据现有脚本(其中一些我不太了解)改编了菜单栏:

I have set up the menubar adapted from an existing script (some of which I do not fully understand) as follows:

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

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

The handler to check the display resolution:

    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,则还将设置显示分辨率.

Basically I want the check mark to move from 720p to 1080p depending on which of the resolution is active. The 720p and 1080p item, if clicked, will also set the display resolution.

我的代码返回了一个错误:未定义NSOnState ...,我在那里迷路了.

The code I have return an error: NSOnState is not defined... and there I am lost.

我要解决的第二个问题是找到一种方法: a)灰显"(禁用)menuItem(在这种情况下,为外部监视器:活动" b)根据条件将项目更改为外部监视器:丢失"

The second issue I have is to find a way to: a) "grey out" (disable) a menuItem (in this case, the item "External Monitor: active" b) to have the item change to "External Monitor: missing" on a condition

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

I have tried: NSMenuItem highlight:false and NSMenuItem enabled:false and both returned and error. In addition, I don't know how to refresh the menu and/or a menu item.

任何帮助或指针将不胜感激. 我预先感谢任何人花时间来思考这些问题!

Any help or pointers will be greatly appreciated. I thank anyone in advance for the time (s)he will spend on my thinking about those questions!

推荐答案

最终解决方案

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

For issue #1, displaying a check mark next to a menu item, thanks to @CJK help, I have found some working code:

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

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

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

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

I have also been able to display any image next to a menu item with NSimage (do not hesitate to ask me if need be)

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

As regards issue #2, enabling/disabling a menu item, I also have found a working code:

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

in the createMenu(aList) handler, you need to add the second line before the repeat loop:

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

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

then in the repeat loop, to enable/disable a menu item:

(aMenuItem's setEnabled:false)

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

**Finally to refresh the menu items, ** I put the code aMenu's removeAllItems() in createMenu(aList) handler and call the handler when I want to refresh. It seems by removing on run/ end run at the beginning of the script, everything works fine!

这篇关于HOW TO:显示复选标记,禁用菜单项,刷新菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆