You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AppleScript中实现MacOS菜单栏项的专属点击操作

为MacOS菜单栏菜单的不同菜单项设置专属操作

你遇到的问题是因为sender是NSMenuItem对象而非字符串,直接将其与"Settings"这类字符串比较会失效。正确的做法是获取该对象的title属性来判断点击的目标菜单项。

以下是修改后的完整代码,关键调整已标注:

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property StatusItem : missing value
property selectedMenu : "" -- each menu action will set this to a number, this will determin which IP is shown

property theDisplay : ""
property defaults : class "NSUserDefaults"
property internalMenuItem : class "NSMenuItem"
property externalMenuItem : class "NSMenuItem"
property newMenu : class "NSMenu"

property theList : "Settings Battery Quit"
-- example list for the menu items that can be used. Ideally you will have your list created dynamically
(*MENU ITEMS
- Settings
- Battery Stats
----------
- Quit
*)


-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
    display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
    error number -128
end if

on menuNeedsUpdate:(menu)
    (* NSMenu's delegates method, when the menu is clicked this is called.

    We use it here to call the method makeMenus(). Which removes the old menuItems and builds new ones.

    This means the menu items can be changed dynamically.

    *)
    
    my makeMenus()
end menuNeedsUpdate:

on makeMenus()
    newMenu's removeAllItems() -- remove existing menu items
    -----< (* this is just to show in this example a dynamic list for the menu items
    set allMenuItems to {"Settings", "Battery Stats", "Quit"}
    ----  <
    
    repeat with i from 1 to number of items in allMenuItems
        set this_item to item i of allMenuItems
        set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:"someAction:" keyEquivalent:"")
        
        (newMenu's addItem:thisMenuItem)
        
        (thisMenuItem's setTarget:me) -- required for enabling the menu item
        if i is equal to 2 then
            (newMenu's addItem:(current application's NSMenuItem's separatorItem)) -- add a seperator
        end if
    end repeat
    
end makeMenus



--menuItems action is required for the menu to be enabled
on someAction:sender
    -- 获取点击菜单项的标题文本
    set menuTitle to sender's title() as string
    
    -- 根据标题执行对应任务
    if menuTitle is "Settings" then
        -- 替换为Settings的专属逻辑
        display alert "打开设置面板"
    else if menuTitle is "Battery Stats" then
        -- 替换为电池统计的专属逻辑
        set batteryInfo to do shell script "pmset -g batt"
        display alert "电池状态" message batteryInfo
    else if menuTitle is "Quit" then
        -- 退出应用程序
        current application's NSApp's terminate_(me)
    end if
end someAction:

-- create an NSStatusBar
on makeStatusBar()
    set bar to current application's NSStatusBar's systemStatusBar
    
    set StatusItem to bar's statusItemWithLength:-1.0
    
    -- set up the initial NSStatusBars title
    StatusItem's setTitle:"IP"
    -- set up the initial NSMenu of the statusbar
    set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"
    
    newMenu's setDelegate:me (*
    Required delegation for when the Status bar Menu is clicked the menu will use the delegates method (menuNeedsUpdate:(menu)) to run dynamically update.


    *)
    
    StatusItem's setMenu:newMenu
    
end makeStatusBar


my makeStatusBar()

关键修改说明

  1. 在someAction:sender方法中,通过sender's title() as string提取点击菜单项的标题字符串。
  2. 用条件分支分别处理不同菜单项的逻辑:
    • Settings:示例为弹出提示框,可替换为打开设置窗口等自定义操作。
    • Battery Stats:示例通过pmset命令获取系统电池状态,可按需调整功能。
    • Quit:调用系统API终止应用程序,完成退出操作。

额外提示

如果后续需要更精细化的控制,也可以为每个菜单项指定独立的action方法(如settingsAction:、batteryAction:),但在动态生成菜单的场景下,通过标题判断是更灵活的实现方案。

内容的提问来源于stack exchange,提问作者Rumi-SE Must Follow the CC-SA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:05:14