Sunday, June 13, 2021

#1 2021-05-13 05:32:41 pm

Budgie
Member
From:: New Zealand
Registered: 2005-02-22
Posts: 714

NSSegmentedControl how to set text and background color?

Hi
I'm trying to set the text color & the background color of a NSSegmentedControl, and getting no where fast
I have tried this, but it fails, am I supposed to target the cell instead?, any help with this appreciated

Applescript:

segMentedSelection's setTitleTextAttributes:(NSColor's redColor)

cheers
Budgie


Cheers
Budgie

“Everything’s impossible until somebody does it.” – Batman

Offline

 

#2 2021-05-13 06:21:02 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6651

Re: NSSegmentedControl how to set text and background color?

I wouldn't be surprised if you can't.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2021-05-13 08:09:35 pm

Budgie
Member
From:: New Zealand
Registered: 2005-02-22
Posts: 714

Re: NSSegmentedControl how to set text and background color?

ah.. one of those one's aye,  cheers


Cheers
Budgie

“Everything’s impossible until somebody does it.” – Batman

Offline

 

#4 2021-05-14 01:29:12 pm

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 130

Re: NSSegmentedControl how to set text and background color?

You're on the right track, to set the text of the segments in an "NSSegmentedControl" you have to work with the backing "NSSegmentedCell", which is a subclass of NSCell.
And you would use the setLabel:forSegment: function of either NSSegmentedControl or NSSegmentedCell, as the functions in NSSegmentedControl call the same functions in the NSSegmentedCell anyway, so you might as well directly access the NSCell.

So here's a simple example that sets the label to the index of the segment.
And the "segmentedControlCell" property is an IB Outlet to the NSSegmentedCell in a xib file.

Applescript:


property segmentedControlCell : missing value

on applicationDidBecomeActive:notification
   if my segmentedControlCell's segmentCount() > 0 then
       repeat with index from 0 to ((my segmentedControlCell's segmentCount()) - 1)
           my (segmentedControlCell's setLabel:(index as text) forSegment:(index))
       end repeat
   else
       my (segmentedControlCell's setLabel:(0 as text) forSegment:(0))
   end if
end applicationDidBecomeActive:

The segments of the NSSegmentedCell are objects in a zero based array, and you can only set or get certain properties or attributes, but cannot access the segment object directly.
You access the segments by their index number, where the first leftmost one would have an index of 0.

The label of the segments is an NSString, and as Shane has concluded, it cannot be formatted or be an attributed string.
You can have an image as well or instead of a label string.
Like this.

Applescript:


property app : reference to current application

set segmentedControlImage to my app's NSImage's imageNamed:(my app's NSImageNameBonjour)
my segmentedControlCell's setImage:(segmentedControlImage) forSegment:(0)

I've used a standard system image for this example, but it could be any image you want to create.

The NSSegmentedControl is a fairly limited control when it comes to customisation.
The Back and Forward buttons in "Safari" are a NSSegmentedControl, with it's segmentStyle: NSSegmentStyle property set to NSSegmentStyleSeparated.

So in conclusion, if you want to jazz up a "NSSegmentedControl" you will have to create some snazzy images for the segments.

Hope this helps a bit more.

Regards Mark

Last edited by Mark FX (2021-05-14 02:02:09 pm)

Offline

 

#5 2021-05-16 03:21:43 pm

Budgie
Member
From:: New Zealand
Registered: 2005-02-22
Posts: 714

Re: NSSegmentedControl how to set text and background color?

Thank you Mark
Great explanation, much better understanding of how this works now, it's appreciated

Cheers


Cheers
Budgie

“Everything’s impossible until somebody does it.” – Batman

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)