• 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: Bug with "attribute run" and Safari ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bug with "attribute run" and Safari ?


  • Subject: Re: Bug with "attribute run" and Safari ?
  • From: Shane Stanley <email@hidden>
  • Date: Sun, 12 Oct 2014 12:53:48 +1100

On 12 Oct 2014, at 8:45 am, Christopher Stone <email@hidden> wrote:

On Oct 11, 2014, at 15:21, quark67 <email@hidden> wrote:
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.

Whatever you use, it’s a matter of converting the HTML. I’m not entirely sure what the OP wants, but if it’s a list of the attribute runs, this builds on your script and should give it to them:

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.

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
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: 
 >Bug with "attribute run" and Safari ? (From: quark67 <email@hidden>)
 >Re: Bug with "attribute run" and Safari ? (From: Christopher Stone <email@hidden>)

  • Prev by Date: Re: RESOLVED: Contacts Plugin AppleScript
  • Next by Date: Re: RESOLVED: Contacts Plugin AppleScript
  • Previous by thread: Re: Bug with "attribute run" and Safari ?
  • Next by thread: Terminal Window Resize or Bust
  • Index(es):
    • Date
    • Thread