Merge/append attributed strings

asobjc

(Andreas Kiel) #1

I have some code which creates an attributed string from given values

on createATS(zColor, fontStyle, aStr as string, zAlign)
	set fontName to (item fontStyle of psFontList)
	set fontVal to NSFont's fontWithName:fontName |size|:aFontSize
	set fontKey to (NSFontAttributeName)
	
	set {theRed, theGreen, theBlue} to zColor
	set theRed to theRed as real
	set theGreen to theGreen as real
	set theBlue to theBlue as real
	set zColor to (current application's NSColor's colorWithCalibratedRed:theRed green:theGreen blue:theBlue alpha:1.0)
	
	set clrVal to zColor
	set clrKey to (NSForegroundColorAttributeName)
	
	set aParagraphStyle to NSMutableParagraphStyle's alloc()'s init()
	aParagraphStyle's setMinimumLineHeight:(zLineSpacing)
	aParagraphStyle's setMaximumLineHeight:(zLineSpacing)
	aParagraphStyle's setAlignment:(zAlign) -- 0, 1, 2, 3
	set parKey to (NSParagraphStyleAttributeName)
	---------------------------------------------------------------------------
	set keyList to {fontKey, clrKey, parKey}
	set valList to {fontVal, clrVal, aParagraphStyle}
	set attrsDictionary to NSMutableDictionary's dictionaryWithObjects:valList forKeys:keyList
	
	set attrStr to NSMutableAttributedString's alloc()'s initWithString:aStr attributes:attrsDictionary
	return attrStr
end createATS

The font name is defined as PostScript Name, size and line spacing are defined as properties.
This works fine.
Now I want to join the “text snippets” into one string.
From the docs ( how I understand) I have to change them into mutable and then use the NSMutableAttributedString’s append.
But I can’t make it work.
Probably I stuck directly in the conversion or initialising the NSMutableAttributedString.


(Shane Stanley) #2

Try creating your mutable version using mutableCopy().


(Andreas Kiel) #3

Thanks Shane,

Creating an empty string finally did the trick.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions


-- classes, constants, and enums used
property NSFont : a reference to current application's NSFont
property NSFontManager : a reference to current application's NSFontManager

property NSColor : a reference to current application's NSColor

property NSUTF8StringEncoding : a reference to current application's NSUTF8StringEncoding
property NSString : a reference to current application's NSString
property NSAttributedString : a reference to current application's NSAttributedString
property NSMutableAttributedString : a reference to current application's NSMutableAttributedString

property NSDictionary : a reference to current application's NSDictionary
property NSMutableDictionary : a reference to current application's NSMutableDictionary

property NSFontAttributeName : a reference to current application's NSFontAttributeName
property NSForegroundColorAttributeName : a reference to current application's NSForegroundColorAttributeName

property NSMutableParagraphStyle : a reference to current application's NSMutableParagraphStyle
property NSParagraphStyleAttributeName : a reference to current application's NSParagraphStyleAttributeName

-- font properties
property psFontList : {"Menlo", "Menlo-Bold", "Menlo-Italic", "Menlo-BoldItalic"} -- {1, 2, 3, 4}
property aFontSize : 14
--property aLineSpacing : 16

############################################
set zAlign to 2 -- {left: 0, right: 1, center: 2, justified: 3, natural: 4}
-- some list of tx portions
set txPartList to {{{1, 0, 1}, 2, "bold magenta words", zAlign}, {{0, 1, 1}, 3, "
italic cyan words", zAlign}}

set styleTxBlock to my createATS(txPartList)

--	write an RTF test file
set fileName to choose file name
set rtfData to styleTxBlock's RTFFromRange:{0, styleTxBlock's |length|()} documentAttributes:{DocumentType:current application's NSRTFTextDocumentType}
rtfData's writeToFile:(fileName's POSIX path) atomically:true

############################################
on createATS(txBlockList)
	set theATS to current application's NSMutableAttributedString's new()
	repeat with txBlock in txBlockList
		set {zColor, fontStyle, aStr, zAlign} to txBlock
		set styledTx to my createStyledTx(zColor, fontStyle, aStr, zAlign)
		(theATS's appendAttributedString:styledTx)
	end repeat
	return theATS
end createATS

on createStyledTx(zColor, fontStyle, aStr, zAlign)
	set fontName to (item fontStyle of psFontList)
	set fontVal to NSFont's fontWithName:fontName |size|:aFontSize
	set fontKey to (NSFontAttributeName)
	---------------------------------------------------------------------------
	set {theRed, theGreen, theBlue} to zColor
	set theRed to theRed as real
	set theGreen to theGreen as real
	set theBlue to theBlue as real
	set zColor to (current application's NSColor's colorWithCalibratedRed:theRed green:theGreen blue:theBlue alpha:1.0)
	set clrVal to zColor
	set clrKey to (NSForegroundColorAttributeName)
	---------------------------------------------------------------------------
	set aParagraphStyle to NSMutableParagraphStyle's alloc()'s init()
	aParagraphStyle's setAlignment:(zAlign)
	set parKey to (NSParagraphStyleAttributeName)
	---------------------------------------------------------------------------
	set keyList to {fontKey, clrKey, parKey}
	set valList to {fontVal, clrVal, aParagraphStyle}
	set attrsDictionary to NSMutableDictionary's dictionaryWithObjects:valList forKeys:keyList
	
	set attrStr to NSMutableAttributedString's alloc()'s initWithString:aStr attributes:attrsDictionary
	return attrStr
end createStyledTx