Escaping greater than and lesser than symbol InDesign

Hi,
my script is not working.

Here is some text from an InDesign file:

Skinner's Experiments

B. F. Skinner (1904“1990) was a college English major and an aspiring writer who, seeking a new direction, entered graduate school in psychology. He went on to become modern behaviorism's most influential and controversial figure. Skinner's work elaborated what psychologist Edward L. Thorndike (1874“1949) called the [law of effect:] Rewarded behavior is likely to recur (Figure 6.11).

I want to select the paragraphs and change the paragraph styles if the text starts with

,

, etc.

So here is the script


global myh1
global myh2
global myh3


set myh1 to "<h1>" as text
set myh2 to "<h2>" as text
set myh3 to "<h3>" as text



tell application "Adobe InDesign CS3"
	set myParagraphCount to count of paragraphs of selection
	
	repeat with i from 1 to myParagraphCount
		if text of paragraph i of selection starts with "<CO>" then
			set applied paragraph style of paragraph i of selection to "CO-TXT-ni"
		else if (paragraph i of selection) starts with myh1 then
			set applied paragraph style of paragraph i of selection to "h1"
		else if (paragraph i of selection) starts with myh2 then
			set applied paragraph style of paragraph i of selection to "h2"
		else if (paragraph i of selection) starts with myh3 then
			set applied paragraph style of paragraph i of selection to "h3"
		else if paragraph i of selection starts with "<h4>" then
			set applied paragraph style of paragraph i of selection to "TXT-4"
		else if paragraph i of selection starts with "<h5>" then
			set applied paragraph style of paragraph i of selection to "TXT-5"
		else
			set applied paragraph style of paragraph i of selection to "TXT"
		end if
	end repeat
	
end tell


I tried using a backslash with the greater than character and lesser than character and also putting these between | and | but the test always fails. You can see that the paragraphs do start with

, so the test shouldn’t fail.

What to do?..checked all the web and this forum too.

It looks like you are doing book publishing for college or high school. I used to design text books before the company I worked for went out of business last year. I always wanted to do a similar set of scripts to help with the initial text flow and styling, and have put some thought into it, but could never convince the right people that it would be worth the effort.

What is causing you problems is not the symbols but the returned value from InDesign. The software engineers on the InDesign team love lists, and tend to return lists as values even when there is no list, and that is what is being returned when you call for the text of the paragraph, a single item list with the paragraph in it. To allow it to work you need to get that item from the list and test the item in your “if … then …” statement.

I spent a few minutes this morning reworking your code and commenting what I did:

global paragraphTags

(* I'm using a list of records with the appropriate tag and style name. This should make it easier to add or change 
    styles to test for. The last item is not used in the script since it is your default. *)
set paragraphTags to {{tag:"<CO>", parstyle:"CO-TXT-ni"}, {tag:"<h1>", parstyle:"h1"}, {tag:"<h2>", parstyle:"h2"}, {tag:"<h3>", parstyle:"h3"}, {tag:"<h4>", parstyle:"TXT-4"}, {tag:"<h5>", parstyle:"TXT-5"}, {tag:"<txt>", parstyle:"TXT"}}



tell application "Adobe InDesign CS3"
	activate
	set myParagraphCount to count of paragraphs of selection
	repeat with p from 1 to myParagraphCount
		
		(* I'm getting the text here so that there is only one call to ID for it, this should speed up the processing of the file. *)
		set theParTxt to item 1 of text of paragraph p of selection
		
		(* Since you are defaulting to the last item we need a variable to store the "found" state. This is used to set the default style, you could do this first and eliminate some of the code, but I left it here so that I could strip out the tag at the end *)
		set styleFound to false
		repeat with q from 1 to ((count of paragraphTags) - 1)
			if theParTxt starts with tag of item q of paragraphTags then
				set applied paragraph style of paragraph p of selection to parstyle of item q of paragraphTags
				
				--this line strips out the tag
				set characters 1 through (count of tag of item q of paragraphTags) of paragraph p of selection to ""
				set styleFound to true
				
				--if found there is no reason to continue repeating through the list of styles so we exit the repeat.
				exit repeat
			end if
		end repeat
		if styleFound is false then
			set applied paragraph style of paragraph p of selection to parstyle of last item of paragraphTags
			
			--this line strips out the tag
			set characters 1 through (count of tag of last item of paragraphTags) of paragraph p of selection to ""
		end if
	end repeat
end tell

Wow. This is a much better solution. I just put the word “contents” in place of “text” (My Script Editor screen didn’t refresh so I couldn’t see that option).

But yours works better and faster.

I can sympathize. I work for people who can’t see the efficacy of automation at all. Yet we just had to lay off 4 people even though we are busier than we have ever been.

If I had money, I’d start my own textbook company and we’d use scripting and XML.

So thanks very much for the reworked script. And good luck.