Do you know how to use "attribute run" with Safari?
______________________________________________________________________
Hey There,
That looks like it goes back to Tiger.
Indeed. And I’m not sure what it did there — attribute run is a class, so it seems to me you’d ask for “attribute runs of…” or “attribute run n of …” But maybe things were different then.
As far as I know you cannot extract styled-text directly from Safari these days
I believe that’s the case.
although perhaps Shane can come up with a Mavericks/Yosemite Library to do it.
use framework "Foundation"
use framework "AppKit"
on fetchAttributeRuns()
tell application "Safari"
tell front document to set {_name, _src} to {name, do _javascript_ "window.document.documentElement.outerHTML"}
end tell
-- put html into an NSString
set anNSString to current application's NSString's stringWithString:_src
-- convert it to data
set theNSData to anNSString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
-- make an attributed string from the data
set theAttString to current application's NSAttributedString's alloc()'s initWithHTML:theNSData documentAttributes:(missing value)
set theAttRuns to {} -- we will add strings to this list
set theStart to 0 -- start from this character
set theLength to theAttString's |length|() -- length of the attributed string
repeat
-- theRange will contain the range of the attribute run
set {theAttributes, theRange} to theAttString's attributesAtIndex:theStart longestEffectiveRange:(reference) inRange:(current application's NSMakeRange(0, theLength))
-- extract the text in that range and add it to the list
set end of theAttRuns to (theAttString's |string|()'s substringWithRange:theRange) as text
-- adjust theStart for next loop
set theStart to current application's NSMaxRange(theRange)
if theStart is greater than or equal to theLength then exit repeat -- at the end, so exit
end repeat
return theAttRuns
end fetchAttributeRuns
Requires Mavericks (via script library) or Yosemite.