我正在尝试使用带有applescript的菜单栏/状态脚本在Mac上设置一些简单的服务。 在上下阅读网页后,请记住我是脚本新手,似乎我已达到极限,我需要一些帮助......
首先,我想在条件上的menuItem旁边显示一个复选标记。在我的例子中,条件是720p和1080p之间的显示分辨率。
我已经设置了从现有脚本(其中一些我不完全了解)改编的菜单栏,如下所示:
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
检查显示分辨率的处理程序:
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)“灰色”(禁用)menuItem(在这种情况下,项目“外部监视器:活动” b)在条件
上将项目更改为“外部监视器:缺失”我试过:NSMenuItem突出显示:false和NSMenuItem启用:false并且返回和错误。 另外,我不知道如何刷新菜单和/或菜单项。
任何帮助或指示将不胜感激。 我提前感谢任何人在考虑这些问题时花的时间!
答案 0 :(得分:0)
最终解决方案
问题#1,在菜单项旁边显示一个复选标记,感谢@CJK帮助,我找到了一些正常工作的代码:
用
替换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的菜单项旁边显示任何图像(如果需要,请不要犹豫,问我)
关于问题#2,启用/禁用菜单项,我还找到了一个正常工作的代码:
在createMenu(aList)处理程序中,您需要在重复循环之前添加第二行:
set aMenu to current application's NSMenu's alloc()'s init()
aMenu's setAutoenablesItems:false
然后在重复循环中,启用/禁用菜单项:
(aMenuItem's setEnabled:false)
**最后要刷新菜单项,**我将代码aMenu's removeAllItems()
放在createMenu(aList)处理程序中,并在我想要刷新时调用处理程序。似乎通过在脚本开头的运行/结束运行中删除,一切正常!