Replacing styled text

Hi.

I am trying to work out the following code. It searches for the first character of a styled text (bold, italic etc.). Later it needs to work with a repeat function, but I can’t get it to work this way.
If a text contains a bold text, char1 returns a result. When I change “item 1 of styleList” to “item 2 of styleList”, it doesn’t return a result.
Funny thing (well, funny :? ) is, that it only seems to work with bold (I tried changing the order of the list, so bold is item 2; then it returns a result.)

Does anyone know what am I doing wrong?
Thanks for your help,
Kjeld

OSX 10.3.3 / Tex-Edit 4.6.2

set styleList to {�
	{"bold", "<B>"}, �
	{"italic", "<I>"}, �
	{"underline", "<U>"}, �
	{"bold, italic", "<BI>"}, �
	{"bold, underline", "<BU>"} �
		}

tell application "Tex-Edit Plus"
	activate
	if exists window 1 then tell window 1
		set theStyle to (item 1 of (item 1 of styleList)) --change the second "item 1" to "item 2"
		set theCode to (item 2 of (item 1 of styleList))
		try
			set char1 to offset of (first character of (every style run whose style is theStyle))
		end try
	end tell
end tell
char1

Hi,

Tex-Edit has much more powerful features than the method that you’re using. It has the ‘search’, ‘replace’, etc. commands where you don’t need to go through a repeat loop to do your thing. If you use the ‘offset’ command, then it makes things very complicated. You should jump to using these features.

BTW, check out the example script “Mac → HTML/F6” in the Tex-Edit folder.

Also, if you look at the dictionary under text style info, you’ll see that the on styles are not strings. If you change your strings (at the top of the script) to key words (i.e. illiminate the quotes), then it should work.

Sorry, I’m kind of busy watching tv so editing this a lot. But, you’ll have to do something else about the text that uses more than one style.

gl,

Hi,

This seems to work ok:

set styleList to {¬
{{bold}, “”}, ¬
{{italic}, “”}, ¬
{{underline}, “”}, ¬
{{bold, italic}, “”}, ¬
{{bold, underline}, “”} ¬
}
set style_index to 4
tell application “Tex-Edit Plus”
–if exists window 1 then tell window 1
set theStyle to (item 1 of (item style_index of styleList)) --change the second “item 1” to “item 2”
set theCode to (item 2 of (item style_index of styleList))
–try
tell front document
every style run whose style is theStyle
end tell
–end try
–end tell
end tell

I like to use document instead of window here, but window is ok.

gl,

Right! That was just what I was looking for.
I checked the convert Mac → html script. It uses wildcards for searching and repalcing texts, but a wildcard is limited to 256 characters. If it’s more the script simply deletes all that text. Not exactly what I want.
I am working an a script to convert rtf-files to a text file with X-Tags. So every style needs it specific code or style sheet.

I will work it out more this weekend or next week. You will see the result in the scriptbuilders section.

Thanks,
Kjeld

Hi Kjeld,

Glad it worked.

I haven’t worked with Tex-Edit for awhile and can’t remember what was the fastest way to do this, but here’s an example. I used the following text:

plain bold underline italics (bold, italics) plain (more bold, italic) plain

except that the styles are stripped in this post. The result after running the script:

plain bold underline italics (bold, italics) plain (more bold, italic) plain

Here’s the script:

tell application “Tex-Edit Plus”
activate
tell front document
set bi_style_runs to (every style run whose style is {bold, italic})
repeat with bisr in bi_style_runs
replace looking for bisr looking for styles {style:{bold, italic}} replacing with ¬
(“” & bisr)
end repeat
end tell
end tell

I can’t remember which was the fastest way to do this on long strings, but you can see how easy it is and you don’t need wild cards. You can run into many problems using ‘offset’ like same text with different styles among others.

Scripting Tex-Edit is fun.

gl,

ok. I tried both of the options.
Replace looking for style run was pretty much slower than the one working with offset.
(about 35 seconds longer for converting 12 textfiles; with text offset ± 2:12 minutes on a 17"iMac).
Besides this, there was a strange bug in not converting bold, underline.

However, it works! Thanks for your help.

Hi Kjeld,

Scripting Tex-Edit Plus is so much fun I think I’ll try some tests. 2 minutes is too long even for a big file.

Later,

Hi,

I created a similar script to the replace script except that it searches and inserts. It ran in less than half the time as the replace script on the first test. I’ll be checking it better later on. First testing, it just searches for bold text and inserts the tag before the found text. Here’s the test script:

property the_style : {bold}
property the_tag : “

set tick1 to the ticks
tell application “Tex-Edit Plus”
activate
tell front document
select insertion point before contents
set style_runs to (every style run whose style is the_style)
set the_count to 0
repeat with this_style_run in style_runs
set the_count to the_count + 1
set the_text to (the_tag & this_style_run)
try
search looking for this_style_run looking for styles ¬
{style:the_style}
set insertion point before selection to the_tag
on error
exit repeat
end try
end repeat
end tell
end tell
set tick2 to the ticks
set the_diff to tick2 - tick1
acitvate
display dialog the_diff
–{contents of this_style_run, the_count}
–81

Later,

Ok. We’re getting off topic now, but let’s play it fair. :smiley:
I will post the complete script I used and give some explanation.

  1. One must have the possibility to add own style combinations.
  2. It needs not only to change the bold, but all the styles mentioned in the script (at least).
  3. A code has to be placed before and after the style run.
  4. Coded text needs to be set as plain text to prevent from further coding.
    You can find a sample text here; 12 of these files in 2:12 minutes (OSX3.3; iMac 800 Mhz G4).
    A beer for you if you can get it faster. :wink:

Kjeld

on open fileList
	repeat with oneFile in fileList
		
		--You can add your own style combinations or style sheets here.
		set styleList to {¬
			{{plain}, "<P>"}, ¬
			{{bold}, "<B>"}, ¬
			{{italic}, "<I>"}, ¬
			{{underline}, "<U>"}, ¬
			{{bold, italic}, "<BI>"}, ¬
			{{bold, underline}, "<BU>"}, ¬
			{{italic, underline}, "<IU>"}, ¬
			{{bold, italic, underline}, "<BIU>"}, ¬
			{{bold, all caps}, "<BK>"}, ¬
			{{bold, italic, all caps}, "<BIK>"}, ¬
			{{strikethrough}, "</>"}, ¬
			{{outline}, "<O>"}, ¬
			{{shadow}, "<S>"}, ¬
			{{all caps}, "<K>"} ¬
				}
		
		--Change the name of this style sheet to your language (and also at end of script)
		set endofStyle to "<$><@Normal>"
		
		tell application "Tex-Edit Plus"
			activate
			open oneFile
			
			if exists window 1 then tell window 1
				replace looking for "<" replacing with "<\<>"
				replace looking for "@" replacing with "<\@>"
				
				repeat with i from 1 to number of items in styleList
					set theStyle to (item 1 of (item i of styleList))
					set theCode to (item 2 of (item i of styleList))
					try
						set char1 to offset of (first character of (every style run whose style is theStyle))
						set char2 to offset of (last character of (every style run whose style is theStyle))
						repeat with x from (count of items of char1) to 1 by -1
							copy endofStyle to after character (item x of char2)
							copy theCode to before character (item x of char1)
						end repeat
						replace looking for endofStyle ¬
							replacing with endofStyle ¬
							replacing with styles {style:plain}
						replace looking for theCode ¬
							replacing with theCode ¬
							replacing with styles {style:plain}
						set (style of every style run whose style is theStyle) to plain
					end try
				end repeat
				
				--Superscript
				try
					set char1 to offset of (first character of (every style run whose baseline ascent is greater than 0))
					set char2 to offset of (last character of (every style run whose baseline ascent is greater than 0))
					repeat with i from (count of items of char1) to 1 by -1
						copy endofStyle to after character (item i of char2)
						copy "<+>" to before character (item i of char1)
					end repeat
					set baseline ascent of (every style run whose baseline ascent is greater than 0) to 0
				end try
				
				--Lowerscript
				try
					set char1 to offset of (first character of (every style run whose baseline ascent is less than 0))
					set char2 to offset of (last character of (every style run whose baseline ascent is less than 0))
					repeat with i from (count of items of char1) to 1 by -1
						copy endofStyle to after character (item i of char2)
						copy "<->" to before character (item i of char1)
					end repeat
					set baseline ascent of (every style run whose baseline ascent is less than 0) to 0
				end try
				
				--Change the name of this style sheet to your language
				copy "@Normal:" to before character 1 of paragraph 1
				--	save window 1 with creator {"TEXT"}
				--	close window 1 saving no
			end tell
		end tell
	end repeat
end open

Hi Kjeld,

I tried changing the inner repeat loop in this section using the ‘search’ command:

repeat with i from 1 to number of items in styleList
set theStyle to (item 1 of (item i of styleList))
set theCode to (item 2 of (item i of styleList))
try
– start of area to change
set char1 to offset of (first character of (every style run whose style is theStyle))
set char2 to offset of (last character of (every style run whose style is theStyle))
repeat with x from (count of items of char1) to 1 by -1
copy endofStyle to after character (item x of char2)
copy theCode to before character (item x of char1)
end repeat
– end of area to change
replace looking for endofStyle ¬
replacing with endofStyle ¬
replacing with styles {style:plain}
replace looking for theCode ¬
replacing with theCode ¬
replacing with styles {style:plain}
set (style of every style run whose style is theStyle) to plain
on error
beep
end try
end repeat

This section is not modified but added the beep in the error handler and got a lot of errors in your script. For some reason, the modified script gets bogged down with the errors. Your script was a little faster. I’m trying to figure out why the errors occur. Maybe something to do with the start and end of file?

Later,

Hi Kel,

The reasons for all of the errors was quite simple: if there is no such style as mentioned in the StyleList nothing can be changed and the script starts beeping.
So I added this lines:

set theCode to (item 2 of (item i of styleList))
			if exists (every style run whose style is theStyle) then
				try
					set char1 to ... etcetera

And after the replace section:

set (style of every style run whose style is theStyle) to plain
				on error
					display dialog "Something's going wrong here."
				end try
			else
			end if

I have been thinking about the reason why this script might be slightly faster (Hey! You owe me a beer 8-)). Probably it’s because the script only needs to remember the positions of start and end of all the style runs, and not it’s contents.
I tried above with your script as well (replace looking etc), but than there where quite a lot of errors (also in not converting some styles). Don’t know why.
Btw, that was a cool tip with the ticks. I am going to use that one more often.

Kjeld

Hi Kjeld,

I wasn’t finished yet! :slight_smile:

Anyway, I figured out why the modified script (the one that uses the ‘search’ command) was bogging down on the errors. Here’s the 2 repeat loops:

Original script:

repeat with i from 1 to number of items in styleList
set theStyle to (item 1 of (item i of styleList))
set theCode to (item 2 of (item i of styleList))
try
– start of changed area
set char1 to offset of (first character of (every style run whose style is theStyle))
set char2 to offset of (last character of (every style run whose style is theStyle))
repeat with x from (count of items of char1) to 1 by -1
copy endofStyle to after character (item x of char2)
copy theCode to before character (item x of char1)
end repeat
– end of changed area
replace looking for endofStyle ¬
replacing with endofStyle ¬
replacing with styles {style:plain}
replace looking for theCode ¬
replacing with theCode ¬
replacing with styles {style:plain}
set (style of every style run whose style is theStyle) to plain
on error
beep
end try
end repeat

Modified script section:

repeat with i from 1 to number of items in styleList
select insertion point before contents
set theStyle to (item 1 of (item i of styleList))
set theCode to (item 2 of (item i of styleList))
try
– start of are to change
–set char1 to offset of (first character of (every style run whose style is theStyle))
–set char2 to offset of (last character of (every style run whose style is theStyle))
–repeat with x from (count of items of char1) to 1 by -1
set style_runs to (every style run whose style is theStyle)
if style_runs is {} then error – simulate error
repeat with this_run in style_runs
search looking for this_run looking for styles {style:theStyle}
copy endofStyle to after selection --character (item x of char2)
copy theCode to before selection --character (item x of char1)
end repeat
–end repeat
– end of area to change
replace looking for endofStyle ¬
replacing with endofStyle ¬
replacing with styles {style:plain}
replace looking for theCode ¬
replacing with theCode ¬
replacing with styles {style:plain}
set (style of every style run whose style is theStyle) to plain
on error
beep
end try
end repeat

In the original script, the offset was erroring right at the beginning of the loop when it couldn’t find the tyle. In the modified script, getting the style runs resulted in an empty list an no error occured. Then, it would run through the rest of the stuff in the loop. Adding the line to make it error if the list was empty, made a big difference and it seems faster and it was a little faster now than the original script. I’m thinking of other stuff to make it faster, but might leave it here util I have more time. Also, thinking about comparing the two modified test files to see if the changes are the same and something else is not going wrong.

You know what you’re doing so I figure you can make the good decisions. It was fun.

gl,