Repeat Routines in AppleScript Studio

Hi there,

I’m currently trying to put together a fairly basic AppleScript Studio application in Xcode but I’ve run into a few problems with repeating the script.

Basically I’m trying to write a simple app with one main window with a box for text input and a button beneath, that when clicked, takes the information in the text box and puts it into a TextEdit document and saves that document.

So far I’ve built the interface and linked it to the script, and I’ve got the script to transfer the info from the text box into a textedit doc and bring up a save as box. It currently looks like this:



on clicked theObject

	tell window of theObject
		set paragraph 1 of document of application "TextEdit" to contents of text field "name"
	end tell
	tell window of theObject
		delete contents of text field "name"
	end tell
	tell document of application "TextEdit"
		save as "Document 1"
	end tell

end clicked

The problem I’m having is that I would like the script to tell TextEdit that for each click of the button in my app, it should take the new information in the text box and put it in the next paragraph in the TextEdit document.

In other words, I want the script to be able to do the following: The first word is typed into the textbox, button clicked, which enters this word into paragraph 1 of the TextEdit document and deletes the text in the text box (this is already working). Then when a second word is typed into the text box and the button clicked, this word will be entered on the second line of the text edit document, and so on - But unfortunately I really don’t know how to do this (I’m rather new to applescript). Currently the script will just replace paragraph 1 of the textedit document when a second word is entered into the text box and clicked.

I’m really open to any suggestion of a way to script the app so that it knows to put each new entry in the text box onto the next line in the textedit document - I’ve just suggesting basing it off the number of clicks as I couldn’t really think of any other way to do it off the top of my head! I’ve done quite a lot of reading and searching but I haven’t been able to figure out how to do this.

The other question I have is whether I can make TextEdit automatically save a document with a name that I specify in the script when it opens? (Currently it brings up a save as dialog).

Many thanks in advance for the assistance, I spent all of last night just reading Applescript manuals to get to where I am now, and I’m really stuck on this at the moment.

Regards,

Mac_guy

Hi,

if you don’t need RTF text implicitly,
it’s easier to use AppleScripts read/write capabilities.
The subroutine writes the text into the specified file as plain text appending it to the end of the file

on clicked theObject
	set myFile to ((path to desktop as Unicode text) & "Document 1.txt")
	tell window of theObject
		set theText to contents of text field "name"
		delete contents of text field "name"
	end tell
	write_to_file from theText & return into myFile with _append
end clicked

on write_to_file from theData into theFile given _append:_append
	try
		set datastream to open for access file theFile with write permission
		if _append is false then set eof of datastream to 0
		write theData to datastream starting at eof
		close access datastream
	on error
		try
			close access file theFile
		end try
	end try
end write_to_file

Thankyou very much StefanK!! That’s is exactly what I wanted it to do, using that text file is much easier than interacting with TextEdit (I didn’t need RTF). As you may have guessed I have very little programming experience, but I’m trying to self learn a few things as a bit of a hobby. Finding this site has been great, lots of helpful tutorials to read through. Thanks again for your solution!

Regards,

Mac_guy