Thursday, August 11, 2022

#1 2022-03-07 02:49:19 am

Ptujec
Member
From:: Slovenia
Registered: 2009-01-17
Posts: 12
Website

Menu Bar Items

Hi!
Is there a way to do the following with ASObjC, so that it performs better?

Applescript:

tell application "System Events" to tell (process 1 whose frontmost is true)
   tell menu bar 1
       return entire contents of menu bar items
   end tell
end tell

Offline

 

#2 2022-03-23 04:10:05 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2443

Re: Menu Bar Items

Hello,

I tried to get the main menu items as an array (using |items|() property), but it didn't work for me. So I did a little trick - I used the numberOfItems() property. The following script returns a list of lists of names for menu items fairly quickly. Of course, this only works with Cocoa applications.

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set menuBAR1 to ((current application's NSApplication)'s sharedApplication())'s mainMenu()

set menus to {}
repeat with i from 0 to (menuBAR1's numberOfItems()) - 1
   
   set m to (menuBAR1's itemAtIndex:i)
   set end of menus to (m's title()) as text
   
   set tempList to {}
   set sub_Menu to m's submenu()
   repeat with j from 0 to (sub_Menu's numberOfItems()) - 1
       set t to ((sub_Menu's itemAtIndex:j)'s title()) as text
       -- checking the "" is for submenu separator lines " ----- "
       if t is not "" then set end of tempList to t
   end repeat
   
   set end of menus to tempList
end repeat
return menus

NOTE: my script lists only 2 levels of menu items. To get entire contents, someone can make it recursive.

Last edited by KniazidisR (2022-03-23 04:27:56 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2022-04-26 03:20:14 am

Ptujec
Member
From:: Slovenia
Registered: 2009-01-17
Posts: 12
Website

Re: Menu Bar Items

Thank you!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)