Setting the font and style of text in a Text Edit document

Hi all,

First time post, apologies in advance in the case that this is in the wrong place.

I’m writing a script which takes values from an excel spreadsheet and places them into a text file. Ultimately these will be ordered in a particular way, and formatted left right and center.

What I specifically want to do at the moment is to format a single phrase that exists in the text. The code I have so far is below:

	tell application "TextEdit"
			activate
			
			set text of front document to finalText
			
			tell front document
				set z to offset of "Closed" in text
				display dialog z
				set font of paragraph 1 to "Desdemona"
				set the style of characters z thru (z + (count of "Closed")) of text to "Bold"
			end tell
			

The “set z” line doesn’t appear to work as the following line, “display dialog” gives 0. The phrase is in the third paragraph.

The corresponding “set the style” returns the error “Can’t get characters 0 thru 6 of text”.

Any ideas?

Hi DemonLlama. Welcome to MacScripter.

The ‘offset’ command only works with text in or obtained by the script, whereas your line directly addresses the ‘text’ of the TextEdit document.

You could get the text from the document first and apply ‘offset’ to the result:

tell front document
	set z to offset of "Closed" in (get its text)
	set the style of characters z thru (z + (count "Closed") - 1) of text to "Bold"
end

Or possibly do this, which is wholly using what’s available in TextEdit’s scripting implementation:

tell front document
	set the style of (first word of its text where it is "Closed") to "Bold"
end

I’m running Snow Leopard, whose version of TextEdit doesn’t allow a ‘style’ to be set, but a similar command for ‘font’ works, so I imagine the above would work for you.

I better start having a signature with: “Thank you for all your contributions.” :smiley:

Nigel, that worked brilliantly thanks for that.

Hi Nigel,

I’ve tried this and made some modifications as it didn’t quite suit my needs. However, I’m still having problems.

To give it a bit of context. I want to create a piece of text with a “Heading 7” property, after which I’ll add some other text, followed by another heading.


When I set the values explicitly there's no problem

set body text to "Dependencies"
		
		tell last paragraph
			set properties to {alignment:left, first line indent:0.0, font name:"Calibri"}
		end tell

set body text to body text & return & "Features"
		
		tell last paragraph
			set properties to {alignment:left, first line indent:0.0, font name:"Calibri"}
		end tell
		

However, when I attempt to do a selection of last paragraph, formatting of perviously set 'paragraph style’s dissappear. I suspect this is to do with setting body text to body text &… as above.



		set body text to body text & return & "Features"
		set mySel to select last paragraph
		
		set paragraph style of last paragraph to "Heading 7" 
		
		set body text to body text & return & "Project Estimates and Costs"
		set mySel to select last paragraph
		
		set paragraph style of last paragraph to "Heading 7" 
		

What can I do?

Hi DemonLlama.

Are you still talking about TextEdit? Your latest efforts are full of Pages scripting terms.

Sorry,

You’re absolutely right. Posted in the wrong thread. I was previously working text documents and switched to Pages.

I’ll switch.

Thanks .