【发布时间】:2018-03-23 01:29:43
【问题描述】:
我正在尝试使用带有 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)“灰色”(禁用)菜单项(在这种情况下,项目“外部监视器:活动” b) 在某个条件下将项目更改为“外部监视器:缺失”
我尝试过:NSMenuItem highlight:false 和 NSMenuItem enabled:false 都返回和错误。 另外,我不知道如何刷新菜单和/或菜单项。
任何帮助或指示将不胜感激。 我提前感谢任何人花时间在我思考这些问题上!
【问题讨论】:
-
不要在多个 SE 网站上交叉发布相同的问题 apple.stackexchange.com/questions/320172/…!
-
我可以为你解决你的第一个问题:
NSOnState应该是(current application's NSOnState)。此外,请阅读 Obj-C 中与NSMenuItem's enabled属性相关的开发人员文档中的 this page。具体来说:这个属性是没有作用的,除非该项目将被添加或者已经是其中一部分的菜单已经发送setAutoenablesItems:NO。开发者文档真的很好,我觉得。 -
你能告诉我这里到底发生了什么吗?这个问题非常令人困惑,并且阅读了几次之后,我仍然不清楚您是在创建自己的应用程序,还是试图控制另一个应用程序,或者脚本应该做什么(好吧,我大致知道,已经阅读了它,但是这里有下一个要知道的上下文,所以我不知道你在做什么)。此外,
ChkDisplay处理程序对于任何人来说都很难在他们的系统上进行测试,因为他们可能有不同的显示选项。 720p/1800p 甚至没有在我的列表中...... -
@CJK,非常感谢您的帮助。再次仔细阅读
if j = myDisplay then (aMenuItem's setState:(current application's NSOnState))后,确实在所需菜单项旁边取消了复选标记!为了回复您的第二条评论,我正在制作自己的应用程序来切换连接到 Mac mini 的外接显示器:因此,当您说 ChkDisplay 处理程序非常具体时,您是对的。我想禁用菜单的一项,因为它仅用于显示信息,但不可点击并最终在显示发生更改(分辨率或其他)时刷新我的菜单栏 -
我做到了,它比
(current application's NSOnState)更流畅,所以那段代码就是if j = myDisplay then (aMenuItem's setState:1)。我现在正在研究启用/禁用位。再次感谢!
标签: cocoa menu applescript nsmenuitem applescript-objc