如何使用AppleScript查找残疾菜单项?

发布于 2025-02-11 15:28:22 字数 1806 浏览 1 评论 0原文

我有一个苹果脚本,可以从所有菜单栏菜单中找到所有菜单项。我已经过滤了每个分离器,所以我只有命令,现在我想找出如何确定哪些菜单项已被禁用。有人知道我该怎么做吗?

更新:

这是我必须获取每个菜单命令的名称的代码。它工作正常,我试图获取在getAppMenuitems中启用的菜单项,但是它也返回了所有菜单项,也丢失了值。

set appName to "Script Editor"
set allMenus to {}
set everything to {}
set onlyEnabled to {}


tell application "System Events" to tell process appName
    set allMenus to name of every menu of menu bar 1
end tell



repeat with menuName in allMenus
    set the end of everything to strings of getAppMenuItems(appName, menuName, onlyEnabled)
end repeat



on getAppMenuItems(appProcess, appMenus, enabledItems)
    tell application "System Events" to tell process appProcess
        
        (*
        # Get all menu items
        set theItems to every menu item of menu appMenus of menu bar 1
        
        
        repeat with i from 1 to number of items in theItems
            set thisItem to item i of theItems
            
            if thisItem is not enabled then
                log ("Item:          " & name of thisItem & " is enabled")
                set the end of enabledItems to the name of thisItem
            end if
        end repeat
    *)
        
        return name of every menu item of menu appMenus of menu bar 1
    end tell
end getAppMenuItems


# From the commented part of the 'getAppMenuItems' function above. 
# When that part of the function is uncommented, I just get everything, 
#   missing values and all
onlyEnabled



# Edit Menu
item 4 of everything

(* Outputs: Half of these items are not enabled

    {"Undo", "Redo", "Cut", "Copy", "Paste", "Paste and Match Style", "Paste Reference", "Delete", "Select All", "Complete", "Find", "Spelling and Grammar", "Substitutions", "Transformations", "Speech", "Start Dictation…", "Emoji & Symbols"} *)

I have an AppleScript script that will find all of the menu items from all of the menu bar menus. I've filtered out each separator, so I have just the commands, and now I'd like to find out how to find out which menu items are disabled. Does anybody know how I could do that?

Update:

Here's the code I have to get the names of each menu command. It's working fine, and I tried to get the menu items that were enabled in the getAppMenuItems, but it just returned all the menu items, missing values too.

set appName to "Script Editor"
set allMenus to {}
set everything to {}
set onlyEnabled to {}


tell application "System Events" to tell process appName
    set allMenus to name of every menu of menu bar 1
end tell



repeat with menuName in allMenus
    set the end of everything to strings of getAppMenuItems(appName, menuName, onlyEnabled)
end repeat



on getAppMenuItems(appProcess, appMenus, enabledItems)
    tell application "System Events" to tell process appProcess
        
        (*
        # Get all menu items
        set theItems to every menu item of menu appMenus of menu bar 1
        
        
        repeat with i from 1 to number of items in theItems
            set thisItem to item i of theItems
            
            if thisItem is not enabled then
                log ("Item:          " & name of thisItem & " is enabled")
                set the end of enabledItems to the name of thisItem
            end if
        end repeat
    *)
        
        return name of every menu item of menu appMenus of menu bar 1
    end tell
end getAppMenuItems


# From the commented part of the 'getAppMenuItems' function above. 
# When that part of the function is uncommented, I just get everything, 
#   missing values and all
onlyEnabled



# Edit Menu
item 4 of everything

(* Outputs: Half of these items are not enabled

    {"Undo", "Redo", "Cut", "Copy", "Paste", "Paste and Match Style", "Paste Reference", "Delete", "Select All", "Complete", "Find", "Spelling and Grammar", "Substitutions", "Transformations", "Speech", "Start Dictation…", "Emoji & Symbols"} *)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很OK 2025-02-18 15:28:22

为了获取菜单项的属性,您首先需要获取对该菜单项的参考。然后,您需要浏览各种菜单项,并构建符合您条件的列表或记录,以便可以根据需要使用参考。

从评论中,使用菜单层次结构列表单击或获取菜单参考的通用处理程序将达到您的目的,因此这些菜单将是:

on run -- examples
   #clickMenu at {"Finder", "View", 10, "Name"} -- note that there are 2 "Clean Up" items
   set menuRef to getMenuReference for {"Script Editor", "File", "New"}
   log result
   clickMenu at menuRef -- clickMenu at {"Script Editor", "File", "New"} can also be used
end run


to getMenuReference for menuList -- Get a reference to a menu item in an application's main menu.
   (*
      The menu hierarchy is described in the menuList parameter.
      The last menu item doesn't need to be exact, just close enough for a match.
      Menu items can also be integer indexes (separators are counted).
      When using an index, note that menus can contain dynamic or hidden items.     
         parameters: menuList [list] -
                        item 1 [text]: the application name
                        item 2 [text]: the menu bar item name
                        item(s) 3+ [text or integer]: the (sub)menu item(s)
         returns the reference or missing value
      *)
   try
      set itemCount to (count menuList) -- needs to be at least {application, menu, item}
      if itemCount < 3 then error "menuReference handler:  the menu item list contains too few items"
      set {appName, menuName} to items 1 thru 2 of menuList
      set {menuItem, found} to {last item of menuList, false}
      tell application appName to activate
      tell application "System Events" -- set up the menu path
         set appName to (name of application processes whose frontmost is true) as text -- handle different process names
         set menuPath to menu menuName of menu bar item menuName of menu bar 1 of application process appName
         if itemCount > 3 then repeat with i from 3 to (itemCount - 1) -- add sub menu items
            set menuName to item i of menuList
            if class of menuName is integer then set menuName to item menuName of (get name of menu items of menuPath)
            set menuPath to menu (menuName as text) of menu item (menuName as text) of menuPath
         end repeat
         if class of menuItem is not integer then -- the target menu item
            repeat with anItem in (get name of menu items of menuPath)
               if anItem begins with (menuItem as text) then -- match a partial string (also 'contains', etc)
                  set {menuItem, found} to {anItem, true}
                  exit repeat
               end if
            end repeat
            if not found then error "menuReference handler:  menu item " & quoted form of menuItem & " not found"
         end if
         return menu item menuItem of menuPath
      end tell
   on error errorMessage number errorNumber
      log errorMessage
      # error errorMessage number errorNumber -- uncomment to pass error up the chain
   end try
   return missing value
end getMenuReference


to clickMenu at menuItem -- Click a menu item in an application's main menu.
   (*
      The menuItem can be a reference to the menu item or a list of the menu hierarchy.
         parameters: menuItem [reference or list] - see the getMenuReference handler
         returns true if successful, false otherwise
   *)
   try
      if class of menuItem is list then -- get a reference
         set menuItem to getMenuReference for menuItem
         if menuItem is missing value then error "clickMenu handler:  the menu item was not found"
      end if
      tell application "System Events"
         # log (get properties of menuItem)
         if (enabled of menuItem) then -- filter for desired property
            click menuItem
            delay 0.25
            return true -- success
         end if
      end tell
   on error errorMessage number errorNumber
      log errorMessage
      # error errorMessage number errorNumber -- uncomment to pass error up the chain
   end try
   return false -- failure
end clickMenu

In order to get the properties of a menu item, you first need to get a reference to that menu item. You would then need to go through the various menu items and build lists or records of those that meet your criteria, so that the references can be used as needed.

From the comments, general-purpose handlers to click at or get menu references by using a list of the menu hierarchy would serve your purpose, so those would be something like:

on run -- examples
   #clickMenu at {"Finder", "View", 10, "Name"} -- note that there are 2 "Clean Up" items
   set menuRef to getMenuReference for {"Script Editor", "File", "New"}
   log result
   clickMenu at menuRef -- clickMenu at {"Script Editor", "File", "New"} can also be used
end run


to getMenuReference for menuList -- Get a reference to a menu item in an application's main menu.
   (*
      The menu hierarchy is described in the menuList parameter.
      The last menu item doesn't need to be exact, just close enough for a match.
      Menu items can also be integer indexes (separators are counted).
      When using an index, note that menus can contain dynamic or hidden items.     
         parameters: menuList [list] -
                        item 1 [text]: the application name
                        item 2 [text]: the menu bar item name
                        item(s) 3+ [text or integer]: the (sub)menu item(s)
         returns the reference or missing value
      *)
   try
      set itemCount to (count menuList) -- needs to be at least {application, menu, item}
      if itemCount < 3 then error "menuReference handler:  the menu item list contains too few items"
      set {appName, menuName} to items 1 thru 2 of menuList
      set {menuItem, found} to {last item of menuList, false}
      tell application appName to activate
      tell application "System Events" -- set up the menu path
         set appName to (name of application processes whose frontmost is true) as text -- handle different process names
         set menuPath to menu menuName of menu bar item menuName of menu bar 1 of application process appName
         if itemCount > 3 then repeat with i from 3 to (itemCount - 1) -- add sub menu items
            set menuName to item i of menuList
            if class of menuName is integer then set menuName to item menuName of (get name of menu items of menuPath)
            set menuPath to menu (menuName as text) of menu item (menuName as text) of menuPath
         end repeat
         if class of menuItem is not integer then -- the target menu item
            repeat with anItem in (get name of menu items of menuPath)
               if anItem begins with (menuItem as text) then -- match a partial string (also 'contains', etc)
                  set {menuItem, found} to {anItem, true}
                  exit repeat
               end if
            end repeat
            if not found then error "menuReference handler:  menu item " & quoted form of menuItem & " not found"
         end if
         return menu item menuItem of menuPath
      end tell
   on error errorMessage number errorNumber
      log errorMessage
      # error errorMessage number errorNumber -- uncomment to pass error up the chain
   end try
   return missing value
end getMenuReference


to clickMenu at menuItem -- Click a menu item in an application's main menu.
   (*
      The menuItem can be a reference to the menu item or a list of the menu hierarchy.
         parameters: menuItem [reference or list] - see the getMenuReference handler
         returns true if successful, false otherwise
   *)
   try
      if class of menuItem is list then -- get a reference
         set menuItem to getMenuReference for menuItem
         if menuItem is missing value then error "clickMenu handler:  the menu item was not found"
      end if
      tell application "System Events"
         # log (get properties of menuItem)
         if (enabled of menuItem) then -- filter for desired property
            click menuItem
            delay 0.25
            return true -- success
         end if
      end tell
   on error errorMessage number errorNumber
      log errorMessage
      # error errorMessage number errorNumber -- uncomment to pass error up the chain
   end try
   return false -- failure
end clickMenu
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文