• 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: How Do I Create class HTML to put on Clipboard?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How Do I Create class HTML to put on Clipboard?


  • Subject: Re: How Do I Create class HTML to put on Clipboard?
  • From: Shane Stanley <email@hidden>
  • Date: Fri, 17 Jun 2016 09:41:56 +1000

On 17 Jun 2016, at 2:15 AM, Christopher Stone <email@hidden> wrote:

That works nicely for «class html».  :)

But getting similar text for Safari's «class weba» doesn't:

Safari uses the webarchive format, which is a bit more complex -- it's what gets saved in a webarchive file, obviously.

Apps can put whatever they like on the clipboard, but there are three main formats: text, raw data, or a property list. Webarchive uses a property list, so:

use framework "Foundation"
use framework "AppKit" -- for pasteboard

set pb to current application's NSPasteboard's generalPasteboard()
set theStuff to (pb's propertyListForType:"com.apple.webarchive") as record

returns:

{
WebMainResource:{
WebResourceURL:"http://latenightsw.com/", 
WebResourceFrameName:"", 
WebResourceMIMEType:"text/html", 
WebResourceData:(NSData) <3c21444f 43545950 45206874 6d6c3e3c 68322073 74796c65 3d22626f 72646572 3a203070 783b2066 6f6e742d 66616d69 6c793a20 4c61746f 2c207361 6e732d73 65726966 3b20666f 6e742d73 697a653a 20323470 783b2066 6f6e742d 7374796c 653a206e 6f726d61 6c3b2066 6f6e742d 77656967 68743a20 3730303b 206d6172><<truncated at 128 bytes out of 1281>>, 
WebResourceTextEncodingName:"UTF-8"
}
}

You probably want the data, returned as text, so:

use framework "Foundation"
use framework "AppKit" -- for pasteboard

set pb to current application's NSPasteboard's generalPasteboard()
set theStuff to (pb's propertyListForType:"com.apple.webarchive") --as record
set theCode to (current application's NSString's alloc()'s initWithData:(theStuff's valueForKeyPath:"WebMainResource.WebResourceData") encoding:(current application's NSUTF8StringEncoding)) as text

returns:

"<!DOCTYPE html><h2 style=\"border: 0px; font-family: Lato, sans-serif; font-size: 24px; font-style: normal; font-weight: 700; margin: 36px 0px 12px; outline: 0px; padding: 0px; vertical-align: baseline; clear: both; line-height: 1; color: rgb(43, 43, 43); font-variant-caps: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);\">New in Script Debugger 6</h2><p style=\"border: 0px; font-family: Lato, sans-serif; font-size: 16px; font-style: normal; font-weight: normal; margin: 0px 0px 24px; outline: 0px; padding: 0px; vertical-align: baseline; color: rgb(43, 43, 43); font-variant-caps: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);\">Script Debugger 6 delivers 25 new features to make you even more productive, and a raft of other tweaks and improvements. Script Debugger 6 lets you tackle even larger projects, and to take advantage of new features such as progress reporting, script libraries and AppleScriptObjC.</p>"

In fact you should probably extract the WebResourceTextEncodingName value and use to decode the data. An exercise for the reader, as they say.

-- 
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: 
 >How Do I Create class HTML to put on Clipboard? (From: Jim Underwood <email@hidden>)
 >Re: How Do I Create class HTML to put on Clipboard? (From: Christopher Stone <email@hidden>)
 >Re: How Do I Create class HTML to put on Clipboard? (From: Shane Stanley <email@hidden>)
 >Re: How Do I Create class HTML to put on Clipboard? (From: Shane Stanley <email@hidden>)
 >Re: How Do I Create class HTML to put on Clipboard? (From: Christopher Stone <email@hidden>)

  • Prev by Date: Script Debugger 6 Officially Released
  • Next by Date: Re: Script Debugger 6 Officially Released
  • Previous by thread: Re: How Do I Create class HTML to put on Clipboard?
  • Next by thread: Re: How Do I Create class HTML to put on Clipboard?
  • Index(es):
    • Date
    • Thread