Re: Is there a way to prepare NSAttaributedString like "attribute runs"?
Re: Is there a way to prepare NSAttaributedString like "attribute runs"?
- Subject: Re: Is there a way to prepare NSAttaributedString like "attribute runs"?
- From: Shane Stanley <email@hidden>
- Date: Mon, 11 Dec 2017 22:33:19 +1100
On 11 Dec 2017, at 7:34 pm, Takaaki Naganoya <email@hidden> wrote:
>
> We can prepare styled text with TextEditor.app with “attribute runs”.
> So, we can filter text whose color is red. This is very useful and ordinary
> processing in AppleScript.
>
> I am looking for similar one with NSAttributedString in vain.
>
> Is there a way to prepare NSAttributedString like “attribute runs”?
Sure. If you have an attributed string in a variable called attString and run
this code:
set {theAtts, theRange} to attString's attributesAtIndex:0
longestEffectiveRange:(reference) inRange:{0, attString's |length|()}
The variable theAtts will contain a dictionary of the attributes, where you can
check the color, and theRange will contain the equivalent of the attribute run.
You can put it in a repeat loop, each time changing the index to just after the
previous range. So something like:
set theLength to attString's |length|()
set startIndex to 0
repeat
set {theAtts, theRange} to attString's attributesAtIndex:startIndex
longestEffectiveRange:(reference) inRange:{0, attString's |length|()}
-- do stuff with theAtts here
set rangeEnd to current application's NSMaxRange(theRange)
if rangeEnd ≥ theLength - 1 then exit repeat
set startIndex to rangeEnd + 1
end repeat
Alternatively, you can use -attribute:atIndex:longestEffectiveRange:inRange:
instead, and get back the value for a particular attribute, like text color.
--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden