• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is there a way to prepare NSAttaributedString like "attribute runs"?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is there a way to prepare NSAttaributedString like "attribute runs"?


  • Subject: Re: Is there a way to prepare NSAttaributedString like "attribute runs"?
  • From: Takaaki Naganoya <email@hidden>
  • Date: Tue, 12 Dec 2017 09:44:44 +0900

I changed. Thanks Shane and Nigel.

(1) Change for NSMaxRange
(2) Detect RGB Color
(3) Return color string for filtering like “attribute runs”


<AppleScript>
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set fRes to choose file of type {"public.rtf"}

set aFilePath to current application's NSString's stringWithString:(POSIX path
of fRes)
set aData to current application's NSData's dataWithContentsOfFile:aFilePath
options:0 |error|:(missing value)
set theStyledText to current application's NSMutableAttributedString's
alloc()'s initWithData:aData options:(missing value)
documentAttributes:(missing value) |error|:(missing value)

set attrRes to getAttributeRunsFromAttrString(theStyledText) of me
(*
{​​​​​{​​​​​​​stringVal:"abcdefg", ​​​​​​​colorStr:"0 0 0",
​​​​​​​colorVal:{​​​​​​​​​0, ​​​​​​​​​0, ​​​​​​​​​0​​​​​​​},
​​​​​​​fontName:"Helvetica", ​​​​​​​fontSize:12.0​​​​​},
​​​​​{​​​​​​​stringVal:"12234534353534", ​​​​​​​colorStr:"255 0 0",
​​​​​​​colorVal:{​​​​​​​​​255, ​​​​​​​​​0, ​​​​​​​​​0​​​​​​​},
​​​​​​​fontName:"Helvetica", ​​​​​​​fontSize:12.0​​​​​},
​​​​​{​​​​​​​stringVal:"dewhuiwheiuweriuwherwe", ​​​​​​​colorStr:"0 0 0",
​​​​​​​colorVal:{​​​​​​​​​0, ​​​​​​​​​0, ​​​​​​​​​0​​​​​​​},
​​​​​​​fontName:"Helvetica", ​​​​​​​fontSize:12.0​​​​​},
​​​​​{​​​​​​​stringVal:"asda192837137129831", ​​​​​​​colorStr:"255 0 0",
​​​​​​​colorVal:{​​​​​​​​​255, ​​​​​​​​​0, ​​​​​​​​​0​​​​​​​},
​​​​​​​fontName:"Helvetica", ​​​​​​​fontSize:12.0​​​​​}​​​}
*)



on getAttributeRunsFromAttrString(theStyledText)
        set thePureString to theStyledText's |string|() --pure string from
theStyledText

        set styleList to {} ---for output

        set theLength to theStyledText's |length|()
        set startIndex to 0

        repeat until (startIndex = theLength)
                set {theAtts, theRange} to theStyledText's
attributesAtIndex:startIndex longestEffectiveRange:(reference)
inRange:{startIndex, theLength - startIndex}

                --String
                set aText to (thePureString's substringWithRange:theRange) as
string

                --Color
                set aColor to (theAtts's valueForKeyPath:"NSColor")
                if aColor is not equal to missing value then
                        set aSpace to aColor's colorSpace()
                        set colName to aSpace's localizedName()
                        set colModel to aSpace's colorSpaceModel()
                        set colComps to aSpace's numberOfColorComponents()

                        if {colModel, colComps} = {1, 3} then --RGB Color Space
(maybe)
                                set aRed to (aColor's redComponent()) * 255
                                set aGreen to (aColor's greenComponent()) * 255
                                set aBlue to (aColor's blueComponent()) * 255
                        else
                                error "Detected color is not RGB (" & colName &
")"
                        end if

                        set colList to {aRed as integer, aGreen as integer,
aBlue as integer} --for comparison
                        set colStrForFind to (aRed as integer as string) & " "
& (aGreen as integer as string) & " " & (aBlue as integer as string) --for
filtering
                else
                        set colList to {0, 0, 0}
                        set colStrForFind to "0 0 0"
                end if

                --Font
                set aFont to (theAtts's valueForKeyPath:"NSFont")
                if aFont is not equal to missing value then
                        set aDFontName to aFont's displayName()
                        set aDFontSize to aFont's pointSize()
                end if

                set the end of styleList to {stringVal:aText,
colorStr:colStrForFind, colorVal:colList, fontName:aDFontName as string,
fontSize:aDFontSize}
                set startIndex to current application's NSMaxRange(theRange)

        end repeat

        return styleList

end getAttributeRunsFromAttrString
</AppleScript>


> 2017/12/12 6:55、Shane Stanley <email@hidden>のメール:
> On 12 Dec 2017, at 8:03 am, Nigel Garvey <email@hidden> wrote:
>> Shouldn't startIndex be set directly to the NSMaxRange result?
>
> Yes, it should. Takaaki should change his script accordingly.


--
Takaaki Naganoya
email@hidden
http://piyocast.com/as/

 _______________________________________________
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

References: 
 >Re: Is there a way to prepare NSAttaributedString like "attribute runs"? (From: "Nigel Garvey" <email@hidden>)
 >Re: Is there a way to prepare NSAttaributedString like "attribute runs"? (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Is there a way to prepare NSAttaributedString like "attribute runs"?
  • Next by Date: Re: How to script the launching of a script at a specific time...
  • Previous by thread: Re: Is there a way to prepare NSAttaributedString like "attribute runs"?
  • Next by thread: Make new page command not working in InDesign CC2018
  • Index(es):
    • Date
    • Thread