使菜单项执行某项操作的AppleScript

huangapple go评论101阅读模式
英文:

Make Menu Item Do Something AppleScript

问题

我正在使用AppleScript编写一个程序,该程序在MacOS的菜单栏上创建一个菜单。这是我的代码:

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 determine 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 separator
        end if
    end repeat
    
end makeMenus

--menuItems  action is required for the menu to be enabled
on someAction:sender
    --MenuItem --do some thing
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 delegate's method (menuNeedsUpdate:(menu)) to run dynamically update.


    *)
    
    StatusItem's setMenu:newMenu
    
end makeStatusBar

my makeStatusBar()

函数

on someAction:sender
    --MenuItem --do some thing
end someAction:

是我需要更改的部分。我不知道如何根据单击的菜单项运行特定任务。当我直接将代码放在函数中时:

on someAction:sender
    --MenuItem --do some thing
    display dialog "This runs on menu item click"
end someAction:

则代码在单击任何菜单项时都会运行。我希望它针对特定的菜单项运行特定任务。菜单项包括:

Settings
Battery Stats
Quit

我尝试过这样做:

on someAction:sender
    --MenuItem --do some thing
    if sender is equal to "Settings" then
        --在这里添加代码!
    end if
end someAction:

但是,当我单击菜单中的“Settings”时,什么也没有发生。

希望这能帮助你!

英文:

I'm writing a program in AppleScript that creates a menu in the menu bar on MacOS. This is my code:

use AppleScript version &quot;2.4&quot;
use scripting additions
use framework &quot;Foundation&quot;
use framework &quot;AppKit&quot;
property StatusItem : missing value
property selectedMenu : &quot;&quot; -- each menu action will set this to a number, this will determin which IP is shown
property theDisplay : &quot;&quot;
property defaults : class &quot;NSUserDefaults&quot;
property internalMenuItem : class &quot;NSMenuItem&quot;
property externalMenuItem : class &quot;NSMenuItem&quot;
property newMenu : class &quot;NSMenu&quot;
property theList : &quot;Settings Battery Quit&quot;
-- 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&#39;s NSThread&#39;s isMainThread()) as boolean then
display alert &quot;This script must be run from the main thread.&quot; buttons {&quot;Cancel&quot;} as critical
error number -128
end if
on menuNeedsUpdate:(menu)
(* NSMenu&#39;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&#39;s removeAllItems() -- remove existing menu items
-----&lt; (* this is just to show in this example a dynamic list for the menu items
set allMenuItems to {&quot;Settings&quot;, &quot;Battery Stats&quot;, &quot;Quit&quot;}
----  &lt;
repeat with i from 1 to number of items in allMenuItems
set this_item to item i of allMenuItems
set thisMenuItem to (current application&#39;s NSMenuItem&#39;s alloc()&#39;s initWithTitle:this_item action:&quot;someAction:&quot; keyEquivalent:&quot;&quot;)
(newMenu&#39;s addItem:thisMenuItem)
(thisMenuItem&#39;s setTarget:me) -- required for enabling the menu item
if i is equal to 2 then
(newMenu&#39;s addItem:(current application&#39;s NSMenuItem&#39;s separatorItem)) -- add a seperator
end if
end repeat
end makeMenus
--menuItems  action is requied for the menu to be enabled
on someAction:sender
MenuItem --do some thing
end someAction:
-- create an NSStatusBar
on makeStatusBar()
set bar to current application&#39;s NSStatusBar&#39;s systemStatusBar
set StatusItem to bar&#39;s statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem&#39;s setTitle:&quot;IP&quot;
-- set up the initial NSMenu of the statusbar
set newMenu to current application&#39;s NSMenu&#39;s alloc()&#39;s initWithTitle:&quot;Custom&quot;
newMenu&#39;s setDelegate:me (*
Requied delegation for when the Status bar Menu is clicked  the menu will use the delegates method (menuNeedsUpdate:(menu)) to run dynamically update.
*)
StatusItem&#39;s setMenu:newMenu
end makeStatusBar
my makeStatusBar()

The function

on someAction:sender
--MenuItem --do some thing
end someAction:

is what I need to change. I don't know how to run specific tasks based on which menu item is clicked. When I put code straight in the function:

on someAction:sender
--MenuItem --do some thing
display dialog &quot;This runs on menu item click&quot;
end someAction:

then the code runs whenever ANY menu item is clicked. I want it to run specific tasks for specific menu items. The menu items are:

Settings
Battery Stats
Quit

I tried this:

on someAction:sender
--MenuItem --do some thing
if sender is equal to &quot;Settings&quot;
--Code here!
end if
end someAction:

However, nothing happened when I clicked "Settings" in the menu.

Any help would be appreciated!

答案1

得分: 0

你可以通过检查发送者的标题来运行特定菜单项的特定任务:

on someAction:sender
set theTitle to title of sender as string
if theTitle is "Settings" then
display dialog "当菜单项“设置”被点击时运行此操作"
else if theTitle is "Battery Stats" then
display dialog "当菜单项“电池统计”被点击时运行此操作"
else if theTitle is "Quit" then
quit
else
-- 什么都不做
end if
end actionHandler:
英文:

You can run specific tasks for specific menu items by checking the sender title:

on someAction:sender
set theTitle to title of sender as string
if theTitle is &quot;Settings&quot; then
display dialog &quot;This runs when menu item \&quot;Settings\&quot; clicked&quot;
else if theTitle is &quot;Battery Stats&quot; then
display dialog &quot;This runs when menu item \&quot;Battery Stats\&quot; clicked&quot;
else if theTitle is &quot;Quit&quot; then
quit
else
-- do nothing
end if
end actionHandler:

huangapple
  • 本文由 发表于 2023年1月9日 07:34:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定