Re: Class ksty
Re: Class ksty
- Subject: Re: Class ksty
- From: Laine Lee <email@hidden>
- Date: Wed, 06 Aug 2008 22:30:44 -0500
- Thread-topic: Class ksty
Title: Re: Class ksty
On 8/5/08 2:57 PM, "Christopher Nebel" <email@hidden> wrote:
«class ktxt» of ("some text" as string as record)
This is a no-op under Leopard -- you still get Unicode text out the other end -- but it doesn't fail. The old "styled text" type (and the 'ksty' data is part of that) has been deprecated simply because it doesn't work very well these days -- there are large swaths of characters that it simply can't represent. Honestly, I have difficulty imagining what you'd do with the 'ksty' part of the record in the first place.
Thanks, Chris!
OK, please suggest how I might accomplish a Leopard fix for this script adapted from a script by julifos, which worked quite well (at least to some extent) through Tiger, for example:
property hexchars : "0123456789ABCDEF"
local styleInfo, clipContents, startDate, ktxt, HTMLText, kakafutis, startAt, iEx, finishAt, textChunk, endDate, totalTime, statistics, stylesCount
--activate
set clipContents to (the clipboard) as styled text
set startDate to (current date)
copy StyledTextToRecord(clipContents) to styleInfo
copy txt of styleInfo to ktxt
--> basic html formatting (color and bolds).
set HTMLText to {""}
tell styleInfo
set kakafutis to its stylesCount
repeat with iEx from 1 to its stylesCount
set startAt to (its stylesOffsets's item iEx) + 1
try
set finishAt to (its stylesOffsets's item (iEx + 1))
on error
set finishAt to ktxt's length
end try
if startAt = finishAt then
set textChunk to character finishAt of ktxt
else
set textChunk to text startAt thru finishAt of ktxt
end if
set textChunk to my HTMLize(textChunk)
if (its stylesColors's item iEx) is not {0, 0, 0} then set textChunk to "<font color=\"#" & my Convert_16bitRGB_to_WebHex(its stylesColors's item iEx) & "\">" & textChunk & "</font>"
if {bold} is in contents of (its stylesOn's item iEx) then ¬
set textChunk to "<b>" & textChunk & "</b>"
if {italic} is in contents of (its stylesOn's item iEx) then ¬
set textChunk to "<i>" & textChunk & "</i>"
if {underline} is in contents of (its stylesOn's item iEx) then ¬
set textChunk to "<u>" & textChunk & "</u>"
set HTMLText's end to textChunk
end repeat
end tell
set HTMLText to HTMLText as string
--> clean it!
set HTMLText to my sr("</b> <b>", space, HTMLText)
set HTMLText to my sr("</font> <b>to</b> <font color=\"#990099\">", " <b>to</b> ", HTMLText)
set endDate to (current date)
set totalTime to endDate - startDate
set statistics to "(" & totalTime & " seconds for " & stylesCount of styleInfo & " styles)"
--beep 2
--display dialog "Conversion is done!" & return & return & statistics buttons {"Cancel", "To Clipboard"} with icon note default button 2
--if button returned of result = "To Clipboard" then
set the clipboard to ("<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" bgcolor=\"#FBFBFB\"><tr><td><font face=\"Geneva, sans-serif\" size=\"1\" color=\"#000000\">" & HTMLText & "</font></td></tr></table>")
--end if
to StyledTextToRecord(clipContents)
--> split text from style
--display dialog styledText
--display dialog class of clipContents
try
set clipContents to clipContents as record
copy «class ktxt» of clipContents to ktxt
copy xToString(«class ksty» of clipContents) to ksty
on error
error "Bad data!" & return & return & "You must provide styled text before running this handler, thanks..."
end try
--> extract style info
set numStyles to my HexToDec(text 11 thru 14 of ksty)
set offsetx to {}
set heightx to {}
set ascentx to {}
set fontidx to {}
set onstylex to {}
set sizex to {}
set colorx to {}
repeat with i from 15 to (numStyles * 40) by 40
set end of offsetx to my HexToDec(text i thru (i + 7) of ksty)
--set end of heightx to my HexToDec(text (i + 8) thru (i + 11) of ksty)
--set end of ascentx to my HexToDec(text (i + 12) thru (i + 15) of ksty)
--set end of fontidx to my HexToDec(text (i + 16) thru (i + 19) of ksty)
--set end of sizex to my HexToDec(text (i + 24) thru (i + 27) of ksty)
set end of colorx to {(my HexToDec(text (i + 28) thru (i + 31) of ksty)), (my HexToDec(text (i + 32) thru (i + 35) of ksty)), (my HexToDec(text (i + 36) thru (i + 39) of ksty))}
set end of onstylex to my returnStyles(my HexToDec(text (i + 20) thru (i + 21) of ksty))
end repeat
return ¬
{txt:ktxt, stylesCount:numStyles, stylesOffsets:offsetx, stylesHeights:heightx ¬
, stylesAscents:ascentx, stylesFontIDs:fontidx, stylesOn:onstylex ¬
, stylesSizes:sizex, stylesColors:colorx}
end StyledTextToRecord
to returnStyles(infoChunk) --> credits to Malcom Fitzgerald
set onstylex to {}
if infoChunk = 0 then set end of onstylex to plain
if (infoChunk mod 2 = 1) then set end of onstylex to bold
if (infoChunk div 2 mod 2 = 1) then set end of onstylex to italic
if (infoChunk div 4 mod 2 = 1) then set end of onstylex to underline
--if (infoChunk div 8 mod 2 = 1) then set end of onstylex to outline
--if (infoChunk div 16 mod 2 = 1) then set end of onstylex to shadow
--if (infoChunk div 32 mod 2 = 1) then set end of onstylex to condensed
--if (infoChunk div 64 mod 2 = 1) then set end of onstylex to expanded
return onstylex
end returnStyles
to HexToDec(hexNumDebug)
set hexNumDebug to reverse of (hexNumDebug's text items)
set theResult to 0
repeat with iDebug from 1 to hexNumDebug's length
set theResult to theResult + (((offset of (hexNumDebug's item iDebug) in hexchars) - 1) * (16 ^ (iDebug - 1)))
end repeat
theResult
end HexToDec
on xToString(x)
try
error "" & x
on error msg
return text 12 thru -16 of msg
end try
end xToString
on Convert_16bitRGB_to_WebHex(thecolor)
set HTMLColor to {}
repeat with rgb in thecolor
set theResultx to {}
set rgb to (((rgb as real) - 1) div 256)
repeat
set mod_Digit to (rgb) mod 16
set beginning of theResultx to hexchars's item (1 + mod_Digit)
set rgb to rgb div 16
if (rgb is 0) and ((count theResultx) mod 2 = 0) then exit repeat
end repeat
set HTMLColor to HTMLColor & characters 1 thru 2 of (theResultx as text)
end repeat
HTMLColor as text
end Convert_16bitRGB_to_WebHex
property search_entities : ("&\"<> ¡¢£¥§¨©ª«¬®&hibar;°±´µ¶·¸º»¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜßàáâãäåæçèéêëìíîïñòóôõö÷øùúûüÿŒœŸƒˆ˜Ωπ–—‘’‚“”„†‡•…‰‹›⁄€™∂∏∑√∞∫≈≠≤≥◊" & tab & return) as string
property replace_entities : {"&", """, "<", ">", " ", "¡", "¢", "£", "¥", "§", "¨", "©", "ª", "«", "¬", "®", "¯", "°", "±", "´", "µ", "¶", "·", "¸", "º", "»", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ÿ", "Œ", "œ", "Ÿ", "ƒ", "ˆ", "˜", "Ω", "π", "–", "—", "‘", "’", "‚", "“", "”", "„", "†", "‡", "•", "…", "‰", "‹", "›", "⁄", "€", "™", "∂", "∏", "∑", "√", "∞", "∫", "≈", "≠", "≤", "≥", "◊", " ", "<br>"}
to HTMLize(thisTextx)
repeat with yeah from 1 to search_entities's length
set my text item delimiters to (search_entities's item yeah) as string
set thisTextx to thisTextx's text items
set my text item delimiters to (replace_entities's item yeah) as string
set thisTextx to thisTextx as text
set my text item delimiters to {""}
end repeat
return thisTextx
end HTMLize
to sr(s, r, t)
tell (a reference to AppleScript's text item delimiters)
set contents to s
set t to t's text items
set contents to r
set t to "" & t
set contents to {""}
end tell
return t
end sr
--
Laine Lee
_______________________________________________
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