Italicize certain text in ClarisWorks

I have a several plays typed in ClarisWorks that I would like to format for possible publication. The plays have some stage directions and actions of characters that need to be italicized, but are presently in plain text. What I think would work, if it’s possible, would be a script that would find the material between 2 parentheses and italicise it, including the paren marks, while leaving the rest of the text as is. The play has probably 100 or more blocks this type of text that I want to italicize. So the script would go through the text, finding stuff in parens, and italizicing it. Can this be done?

I have been able to write very simple applescripts but as soon as they get complicated I get lost. If you know how to do this, can you provide an example that I can follow and adapt to my use?

Thanks for comments and any help.

You don’t say which version of ClarisWorks you’re using. I have version 4.0, which has a rather sparse AppleScript dictionary. However, the following seems to work with documents of modest size - if not particularly rapidly. I hope it’s of some use to you. :slight_smile:

-- Italicises parenthesised text in ClarisWorks 4.0
-- Doesn't allow for nested parentheses
tell application "ClarisWorks"
	if not ((the front window exists) and (the front window's name ends with "(WP)")) then
		display dialog "ClarisWorks' front window should be a word processor document!" with icon stop
		error number -128
	end if
	-- Get the text of the front document as an AppleScript string
	set theText to the front document's text
end tell

set astid to AppleScript's text item delimiters

-- Calculate the offset of the first "(" in the text
set AppleScript's text item delimiters to "("
set p to (count theText's first text item) + 1
-- Get all the sections of text that follow a "("
set restOfSections to the rest of theText's text items

set AppleScript's text item delimiters to ")"

set counter to 0
repeat with thisSection in restOfSections
	-- Get the length of this section and calculate the offset in it of ")"
	set c to (count thisSection)
	set o to (count thisSection's first text item) + 1
	-- If the calculated offset > the length of the section then the section does not contain ")"
	-- Otherwise select the equivalent parenthesised text in the document
	-- and run ClarisWorks' built-in "Italic" macro
	if o is not greater than c then
		set counter to counter + 1
		tell application "ClarisWorks"
			set the selection to characters p thru (p + o) of the front document's text
			do script "Italic"
		end tell
	end if
	-- Calculate the offset in the document of the next "("
	set p to p + c + 1
end repeat

set AppleScript's text item delimiters to astid

tell application "ClarisWorks"
	display dialog "The Italic macro was run " & counter & " times." with icon note
end tell

Thanks for the script. I only have ClarisWorks 2.1, but I am definitiely going to try out your script. I’ve copied it to a word processor “ Mariner Write “ and will now copy it to the Script Editor of Applescript and try it. I don’t care how slow it is as long as it works.


I have been trying to run the script but have been running into problems.

One problem I ran into was “Expected class name but found property”, and “Applescript” was highlighted. I tried to modify the script and then got “Some data could not be read”, with “text” highlighted. I don’t know what caused these problems or how to fix them.

Would it be possible to simplify the script so that it gets only the first example of text in parens?

There are five occurrences of the word “AppleScript” in the script. None of these should cause any problems if the lines they’re in are entered correctly. The only way I can duplicate your error is to uncomment the comment in the first ‘tell’ block and omit the word “an”. I then get the above message as a compilation error.

There are seven occurrences of the word “text”. Only two instances are likely to be a problem: where they occur in the expression ‘the front document’s text’. These depend on ‘text’ being recognised by ClarisWorks 2.1 as a property of a ‘document’. It’s recognised by ClarisWorks 4.0 as such, though the entry for ‘document’ in the dictionary is completely blank.

There’s also a variable in the script called ‘theText’, which is used three times. This must be entered as one word. I’d recommend copy/pasting the script from your browser to your script editor and trying again. If it doesn’t compile or doesn’t run or doesn’t work, make a note of the problem lines and let me know. I’ll try to help, but it’ll be a bit tricky trying to second-guess an AppleScript implementation that’s two versions older than the rickety one I have! :-

If the above problems can be solved, then yes. Is that what you want?

Thanks for the help. Here’s what I have:

AppleScript 1.5.5 (part of OS 9.1)
ClarisWorks 2.1 v3

Some of the formatting of the play was done on Mariner Write version 1.6.2bUS. This involved the use of 2 or 3 so-called rulers, paragraphing, and tabs. I then copied this back to ClarisWorks and used the Find and Replace function to remove rulers, extra paragraphing, extra tabbing, etc. Also removed were a whole bunch of e’s with an accent and a’s with an umlat.

Since I couldn’t get the italicizing script to work, I decided the write a very simple text with a few words, then a few words in parens, then another few words. If I could get this to work, I might be able to find out what was wrong. Something like:

I couldn’t get this to work. Is an upgrade to Claris works still available?