Re: Add format to a string
Re: Add format to a string
- Subject: Re: Add format to a string
- From: jj <email@hidden>
- Date: Tue, 26 Oct 2004 22:30:42 +0200
Hi, and sorry for the delay!
Here is a sample based on your request. You can adaptate it to search for
multiple ocurrences, apply font colors, etc.
Please, NOTE that "StyledText <-> Record" does only support the standard
"scrap styles" supported by AppleScript: plain, bold, italic, underline,
outline, shadow, condensed and expanded. Other "styles" (eg, hilite color or
strikethrough) are styles pertaining to certain applications, but are not
*standard*. Read its docs for more info.
jj
##################################################################
property sttr : (load script alias "path:to:StyledText <-> Record.scpt")
set stringToFormat to "This is a simple sample" as Unicode text
--> "Unicode text" is in fact styled text, but you could copy some styled
--> text to the clipboard and stat "set stringToFormat to (the clipboard)"
set firstSubstringBoldedText to formatSubString(stringToFormat, "simple")
set the clipboard to result --> now, paste in your favorite text editor
--> applies bold to first ocurrence of subString in wholeString
on formatSubString(wholeString, subString)
--> create a base record which will be modified
--> and passed later to the RecordToStyledText handler
set rec to sttr's StyledTextToRecord(wholeString)
--> check for substring
set startOffset to (offset of subString in wholeString) - 1
if result is -1 then error "substring not found!!!"
set endOffset to startOffset + (count subString)
--> add substring's offsets to record
(*
You can add other style info in this if-end-if statement
(eg, stylesFonIDs = font face)
How can you apply a different font? Follow these steps:
- Pick TextEdit or your favorite text editor and write some text.
- Apply it your desired font (eg, "Courier"). Select and copy.
- Run styledTextToRecord. Eg:
sttr's StyledTextToRecord(the clipboard)
- Memorize the number in stylesFontIDs (Courier is 22 in my system)
- Apply such number in this if-end-if statement. Eg:
set rec's stylesFontIDs's beginning to 22
--> if "newOffset is 0", apply at beginning of style info
*)
if startOffset is 0 then
--> found substring at beginning of wholestring
set rec's stylesCount to 2
set rec's stylesOffsets's end to endOffset
set rec's stylesOn's beginning to {bold}
else if endOffset is ((count wholeString) - 1) then
--> found substring at end of wholestring
set rec's stylesCount to 2
set rec's stylesOffsets's end to startOffset
set rec's stylesOn's end to {bold}
else
set rec's stylesCount to 3
set rec's stylesOffsets's end to startOffset
set rec's stylesOffsets's end to endOffset
set rec's stylesOn's end to {bold}
set rec's stylesOn's end to rec's stylesOn's item 1 --> copy style
end if
--> now, fill subsequent fields with same info
repeat (rec's stylesCount) - 1 times
set rec's stylesHeights's end to rec's stylesHeights's item 1
set rec's stylesAscents's end to rec's stylesAscents's item 1
set rec's stylesFontIDs's end to rec's stylesFontIDs's item 1
set rec's stylesSizes's end to rec's stylesSizes's item 1
set rec's stylesColors's end to rec's stylesColors's item 1
end repeat
--> rec is now well-formed
return sttr's RecordToStyledText(rec)
end formatSubString
##################################################################
--
http://www.macscripter.net/
http://www.osaxen.com/
> I'd like a handler that would apply the bold style a specified sub-string in
> the first occurrence (or everywhere it is found if that's easier) in a string.
> I have read through StyledText <-> Record.scpt by julifos and still am not
> sure how to make a simplified handler for what I'm wanting. I'm looking for
> something like this:
>
> on formatSubString(wholeString, subString)
> -- returns styled text where subString becomes bold.
> -- options for italic, color, highlight would be nice, as well
> end formatSubString
>
> formatSubString("This is a bold statement.", "bold")
> -- Result:
> This is a bold statement.
>
> I am undecided if the wholeString should retain any style info it has at the
> beginning. Also, a third parameter allowing one to specify italics or color or
> highlighting or even font size or name instead of bold would be just swell.
>
> Thanks,
> Joe Weaks
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden