TextEdit - how to insert a special symbol-character line divider

I cannot seem to find a sufficient example to stimulate my brain on this one.

I use TextEdit a lot and I want to create an AppleScript which will do the following:

INSERT at current cursor: a line of text 40 characters long,
where each character is a special symbol-character with hex value 2501
with each character the color specified by 16-bit RGB value-triplet {37522, 37265, 65278}

I figure this shold boil down to 2 steps
(1) create the special character string (repeat 40 times the character hex-2501 each colored as specified)
(2) insert the string at the current TextEdit document cursor position

Can anyone assist me with this?
Many thanks in advance,
ANW

Model: iMac Core2Duo (Sep 2006)
AppleScript: for Mac OSX 10.4.8
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi,

unfortunately TextEdit’s scripting capabilities are quite poor,
the only way to insert text at the cursor position is pasting from the clipboard

set a to «data utxt2501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501» as Unicode text

set the clipboard to a
activate application "TextEdit"
tell application "System Events"
	tell process "TextEdit" to keystroke "v" using command down
end tell

Changing the color is much more difficult. You must read the RTF raw data, insert the color at the end of the color table,
get it’s index number, add the color shortcut to the paragraph and write the file back to disk

Edit: It is complicated, but this should do the whole thing:

set theLine to «data utxt2501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501250125012501» as Unicode text
set the clipboard to theLine
activate application "TextEdit"
tell application "System Events"
	tell process "TextEdit" to keystroke "v" using command down
end tell
tell application "TextEdit"
	set thePath to path of document 1
	close document 1 saving yes
end tell

set theFile to POSIX file thePath
-- RTF works only with 8 bit color so RGB values must be converted
set red to "\\red" & 37522 div 256 as string
set green to "\\green" & 37265 div 256 as string
set blue to "\\blue" & 65278 div 256 as string
-- read file
set RTFtext to read file theFile
set {TID, text item delimiters} to {text item delimiters, "{\\colortbl;"}
set {a1, a2} to text items of RTFtext
-- extract color table
set text item delimiters to "}"
set colorTable to text item 1 of a2
set text item delimiters to ";"
set colorIndex to ((count text items of colorTable) as string)
set text item delimiters to ""
-- add new color
set colorTable to colorTable & red & blue & green & ";}"
set theText to a1 & "{\\colortbl;" & colorTable
set text item delimiters to "}"
-- put the text together
set theText to (theText & text items 2 thru -1 of a2) as string
set text item delimiters to "\\'84\\'aa"
-- find beginning and end of the inserted line
set a to text items of theText
set text item delimiters to "\\cf"
-- get color of text after the inserted line
set lastColor to character 1 of last text item of text item 1 of a
set text item delimiters to ""
-- change colors
set item 1 of a to item 1 of a & "\\cf" & colorIndex & space
set item -1 of a to "\\cf" & lastColor & space & item -1 of a
set text item delimiters to "\\'84\\'aa"
-- put the text together
set theText to a as string
set text item delimiters to TID
-- write file back to disk
set ff to open for access file theFile with write permission
write theText to ff
close access ff

Hi Stefan,

I don’t have the same version, but wouldn’t something like this work:

tell application “TextEdit”
set color of paragraph c of front document to {37522, 37265, 65278}
end tell

assuming the pasted string is the whole paragraph.

Edited: here’s a version that works in my os with AppleScript 1.9.1:


set u to run script "«data utxt2501»" as Unicode text
set l to {}
repeat 40 times
	set end of l to u
end repeat
set s to l as Unicode text
tell application "TextEdit"
	activate
	set the clipboard to s
end tell
tell application "System Events"
	tell process "TextEdit"
		keystroke "v" with command down
	end tell
end tell
tell application "TextEdit"
	set t to text of front document
end tell
set o to offset of s in t
set temp_t to text 1 thru o of t
set c to count paragraphs of temp_t
tell application "TextEdit"
	set color of paragraph c of front document to {37522, 37265, 65278}
end tell

Not sure if the color is supposed to be about a medium blue. Also, assumed that the pasted line is the only line.

gl,

Hi kel,

oh my God, yes, I forgot that you can assign the color directly to text elements of the text.
Anyway, it was fun to puzzle out this monster :slight_smile:

Thank you both for suggestion solution code,
However when I test either code-set in a blank TextEdit document
it does not produce anything! No line!

Now I thought I got the hex code right:
In TextEdit I selected the Edit-menu and then selected the menu option at the bottom: “Special Characters …”
and then I find the Unicode for the line symbol-character I wanted:
when I select its character in the palette,
the bottom of the display box says:
Name: BOX DRAWING HEAVY HORIZONTAL
Unicode: 2501 UTF8: E2 94 81

So can I assume that the unicode value will work here for hex code,
or does some coding need to be adjusted?

Thanks again for taking a whack at this problem, ANW

Hi,

I assumed, there is already an existing file,
here is a part of the script above, which works also with an empty new file

...
tell application "System Events"
	tell process "TextEdit" to keystroke "v" using command down
end tell
tell application "TextEdit"
	if name of window 1 contains "Untitled" then
		set theFile to (choose file name with prompt "Save document")
		save document 1 in theFile
		close document 1
	else
		set thePath to path of document 1
		close document 1 saving yes
		tell me to set theFile to POSIX file thePath
	end if
end tell

set theFile to theFile as Unicode text
-- RTF works only with 8 bit color so RGB values must be converted
set red to "\\red" & 37522 div 256 as string
...

Thanks Stefan
but here is what I came up with - after a lot of trial and error,
and it works in a new TextEdit file (named “Untitled 4” for example) as well as in an existing TextEdit file
(which I maintain for a log of daily activities, with different dates and their events separated by this “divider line”)
Here’s the code that works (Mac OSX 10.4.8, Textedit v 1.4 , & Script Editor v2.1.1)

set l to {}
repeat 40 times
set end of l to «data utxt2501» as Unicode text
end repeat
set end of l to (ASCII character 10) – adds a carriage return
set s to l as Unicode text
tell application “TextEdit”
activate
set the clipboard to s
end tell
tell application “System Events”
tell process “TextEdit”
keystroke “v” using command down
end tell
end tell
tell application “TextEdit”
set t to text of front document
end tell
set o to offset of s in t
set temp_t to text 1 thru o of t
set c to count paragraphs of temp_t
tell application “TextEdit”
set color of paragraph c of front document to {37522, 37265, 65278}
end tell

Note a couple of things:
I had to correct the utxt2501 quoting to make it work:
the original was: set u to run script “«data utxt2501»” as Unicode text — and it didn’t work
what worked was: set u to «data utxt2501» as Unicode text
I incorporated the utxt2501 in the repeat loop (simple enough)
I added a carriage return (ASCII 10) to complete the line and move the cursor to the next line down
I did not have to alter the color coding code as you did in your most recent post,
I did not have to add special exception code to to handle new TextEdit document
BUT (big but):
while I can execute my code successfully from the Script Editor when I have a TextEdit document open (new or old),
(i.e., it inserts the blue divider line 40-characters long and moves cursor to next line)
I cannot get this Applescript code to run from an assigned key-command (I use the application KeyXing), e.g. CTRL-OPT-L
and yet every other Applescript I have coded for TextEdit (such as change color of highlighted text to blue)
runs just fine after assigning to a key-sequence – and this bugs the heck outa me!
ah well, more later. Aloha.

The above code is almost good enough, but it fails when I tried to assign it to a function key.
Why? because there is no delay between assigning the line to the clipboard and then pasting the clipboard contents
You need a delay of 1 at the least.
But in addition I had very flaky results for coloring the line - and it depended on where the new divider line was in the document. So I went back to some code hints regarding using System Events for using special AXSelectedTextRange style code: and as a result I now have very workable and complete code:
for creating a divider line, for pasting it into a new or existing TextEdit document, and for coloring it, and finally placing the curosr just below the line - to allow you to start typing text – and above all I can assign it to a function-key shortcut in KeyXing or XKeys.
Here’s the code:

– Section 1: create a 40-char line and copy to clipboard & after a delay paste it
set l to {}
repeat 40 times
set end of l to «data utxt2581» as Unicode text
end repeat
set end of l to (ASCII character 10)
set s to l as Unicode text
set the clipboard to s as Unicode text
– need a delay here between set clipboard & paste from it
delay 1
tell application “System Events” to tell process “TextEdit” to keystroke “v” using {command down}

– Section2: set color of inserted line
– note: cursor control key codes: 123=left, 124=right,125=down, 126=up:
– following code is from script: set highlighted text to a color:
– we highlighted the just-created line, and then we can color it
tell application “System Events” to tell process “TextEdit” to key code 126 --up
tell application “System Events” to tell process “TextEdit” to key code 123 using {command down} – left to start of line
tell application “System Events” to tell process “TextEdit” to key code 124 using {shift down, command down} --highlight to end of line
tell application “System Events” to tell text area 1 of scroll area 1 of window 1 of process “TextEdit” to if exists then
set {x, y} to value of attribute “AXSelectedTextRange”
if x ≤ y then tell application “TextEdit” to set color of document 1’s characters x thru y to {37522, 37265, 65278}
end if
– move cursor to start of line, then down to new line
tell application “System Events” to tell process “TextEdit” to key code 123 using {command down} – left to start of line
tell application “System Events” to tell process “TextEdit” to key code 125 --down to new line

Hope someone else can use it.