Script TextEdit paragraph problem

The requirement is to make a file consisting of a variable number of single lines (paragraphs) of data that will be saved after presenting it to the user. I have chosen to script TextEdit to make the presentation to the user.

I can not make TextEdit get beyond the first line.

This script illustrates the problem I have.



set theDate to "2004-08-06"
set docTitle to theDate & " Data"
set dataLine to {"This is the first line of data", "There is a second data line", "The third line of data is the last"}

tell application "TextEdit"
	save document 1 in (((path to desktop) as string) & docTitle) with replacement
	repeat with i from 1 to 3 --(count of dataLine)
	(*
		display dialog i
		display dialog (item i of dataLine)
	*)
		set paragraph i of document 1 to ((item i of dataLine) & return)
		make at end of document 1 new paragraph
	end repeat
	save document 1 in (((path to desktop) as string) & docTitle) with replacement
	quit application "TextEdit"
end tell

The script saves the empty document on the desktop at the first “save” instruction. It writes the first line of data to the document inside the repeat loop. When the two display dialog lines are not commented out, it displays that i reaches 2 and displays the second data line. Then it halts with “TextEdit got an error: NSInternalScriptError” at the “set paragraph i of document 1 to ((…” line.

When I set the limits on i in the repeat line to “1 to 1”, the program runs to completion.

How do I make TextEdit accept that I need more than one line in my document?

OS X v10.3.4
AppleScript 1.9.3
Script Editor 2.0 (v43)
G4 Gigabit E’net

Thanks for hints…

jim

This will insert your text into the end of the TextEdit document:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Is there a reason why you’re using TextEdit to do this, when AppleScript can do it on its own, in far less code, and far, far faster:

set theDate to "2004-08-06"
set docTitle to theDate & " Data"
set dataLine to {"This is the first line of data", "There is a second data line", "The third line of data is the last"}

set oldDelims to my text item delimiters
set outputFile to open for access file (((path to desktop) as string) & docTitle)
set my text item delimiters to return
write dataLine as string to outputFile
close access outputFile
set my text item delimiters to oldDelims

The text item delimiters lines take care of automatically inserting return characters between each text item in the list.

My thanks to Jon and Camelot for the help.

It could be the cost of ignorance on my part. It’s not clear from running the sample code that I’m smart enough to display the output to the user before comiting to the save if I incorporate the method you suggest. I’ll look further. This is my first attempt at AppleScript since OS 7.5.

jim

yes, can someone further clarify this? i’d like to edit a text document from applescript without opening another app.

Hi,

You can read/write to text files using the standard additions scripting additions read/write commands. Check out the standard additions dictionary. There are 6 commands with this scripting addition: open for access, close access, read, write, get eof, and set eof. For further info besides what the dictionary provides, you can download the ScriptingAdditions.PDF guide:

http://docs.info.apple.com/article.html?artnum=50096

It’s a bit old but clarifies the command definitions and gives a few examples. You might want to set up a text file and think about something you want to do. For example, you might want to start with writing something to the end of the file and ask if you have any questions.

EDITTED: you might want to use an error handler when writing to file. The error handler would catch an error and ‘close access’ the file you’re writing to. If an error occurs, then you could be left with a file that can’t be trashed or even a machine that can’t shut down. If this happens and you’re stuck, then type in the script editor:

close access refernceToTheFile

and run it. This has gotten me out of jams especially with typographical errors in my scripts.

gl,