Copying and Pasting Text

I am just starting out with Applescript Studio and am having trouble working out how to copy and paste text.

Basically what I want to do is to be able to copy text from a text view box and save it to a plain-text file, and to read plain-text files and paste them into a text view box.

I’ve tried the guides included in the Developer tools but I just can’t find what I’m looking for. I seem to be going around in circles.

Can anyone point me to a simple, straightforward explantion of how to use text in Studio, or perhaps give me an idea as to what the basics are.

Help with this would be appreciated.

There are a few ways to do both reading data and writing data. You could create a document-based application and incorporate the built-in document handling ‘data representation’ handlers. You could also use the cocoa connections save: and open: to launch the built-in file handling features. I’ve provided a few links to apple-dev website for you below to get you on track. I’d take a look at the simple text editor and save/open panel demo apps, and also search this forum for more info, as this topic’s been covered before.

If you are just doing basic text->file->text handling the following will work fine for you. You’ll have to modify the code to point to the file you want to manage. It’s pretty basic and has some issues that you’ll have to deal with depending on your application, but it works.

on clicked theObject
	if name of theObject is "Save" then --> Save the data to the file
		set path2File to (((path to desktop) as string) & "test.txt") as file specification
		set theData to contents of text view "textView" of scroll view "scrollView" of window "window"

			open for access path2File with write permission
			set eof of path2File to 0
			write (theData) to path2File starting at eof as string
			close access path2File
		on error
				close access path2File
			end try
		end try

	else if name of theObject is "Read" then --> Read the data from the file
		set path2Data to (((path to desktop) as string) & "test.txt") as file specification
			set fileContents to read path2Data as string
		on error
			set fileContents to "Error reading file"
		end try
		set contents of text view "textView" of scroll view "scrollView" of window "window" to (fileContents as string)
	end if
end clicked

Some apple links…

Good luck…

Many thanks, jobu. I now see some light at the end of the tunnel.

I’ve read through your script a few times and I can see what it’s doing. The structure is very similar to doing the same thing in Visual Basic.

This is a big help, and I’ll experiment with it over the long weekend (it’s Bank Holiday here in the UK).