Space before a menu item containing an image

Hello,

Is it possible to add a space before a menu item containing an image?


set itemsMenuBar to current application's NSMenu's alloc()'s init()

set theImage to current application's NSImage's imageNamed:"icnutilities"
itemMenu's setImage_(theImage)

itemMenu's setTitle_("Utilities")
itemMenu's setAction_("menubarUtilities:")
itemsMenuBar's addItem_(itemMenu)

Thanks a lot!

I would like to help you, but I don’t understand You’re question.

are you working in Xcode with a xib file ?
Or are you creating the menu user interface in code in Script Editor ?
You’re using an object named “itemMenu”, where is this object created ?
Is the menu item a sub menu of of an application’s main menu ?

As I understand it the menu item is suppose to have an image and a title.
is the image left or right of the title ?

Do you meen a separator bar, and not a space ?

Regards Mark

Sorry…

Yes it’s a project in Xcode.

Not a separator bar, It’s just an interval before the image (see the screenshot). Sorry for my bad English…


set itemsMenuBar to current application's NSMenu's alloc()'s init()

set itemMenu to current application's NSMenuItem's alloc()'s init()
set theImage to current application's NSImage's imageNamed:"icnapps"
itemMenu's setImage_(theImage)
itemMenu's setTitle_("Applications")
itemMenu's setAction_("menubarApplications:")
itemsMenuBar's addItem_(itemMenu)

set itemMenu to current application's NSMenuItem's alloc()'s init()
set theImage to current application's NSImage's imageNamed:"icnutilities"
itemMenu's setImage_(theImage)
itemMenu's setTitle_("Utilities")
itemMenu's setAction_("menubarUtilities:")
itemsMenuBar's addItem_(itemMenu)

Thanks!

I think you need to set the imageAlignment property.
If you view a menuItem in interface builder it will show you
The available options. Such as trailing, leading, left, right etc.

https://developer.apple.com/documentation/appkit/nscellimageposition?language=objc

Oh sorry look
At indentationLevel

https://developer.apple.com/documentation/appkit/nsmenuitem/1514809-indentationlevel?language=objc

I understand now!

I don’t have access to “Monterey”, so you may need help from those who do.

But this should work for you.


set itemsMenuBar to current application's NSMenu's alloc()'s init()
set theImage to current application's NSImage's imageNamed:"icnapps"

set itemMenu to current application's NSMenuItem's alloc()'s initWithTitle:"Utilities" action:"menubarUtilities:" keyEquivalent:"u"
itemMenu's setImage:theImage
itemMenu's setIndentationLevel:2
itemMenu's setTarget:me
itemsMenuBar's addItem:itemMenu

The “indentationLevel” parameter is not documented very well, but can be a integer value between 0 and 15, but the Apple documentation doesn’t inform how this value relates to the screen resolution or dimensions, so experimentation might be needed for different screen resolutions.

Regards Mark

Hello,

Great!!

you’re the best, it’s perfect.
Thanks Mark FX, thanks technomorph!

:cool: