saving text in a text view

I have a basic Applescript application in which I want to save (from the save menu) the contents of text view “textToChange” of scroll view “textChange” of window “main” as a text file and then open that file in the same text view from the open menu.
Thanks in advance.

Is your app created as a document-based app or a regular applescript studio app?

If it is document-based app, there are some basic examples of how to use the doc-based format in the plain text editor and task list example projects found in /developer/examples/applescript studio/.

If you are writing your own routines in a regular (non-document-based) ASS app, then you need to do three things. First, for the open and save menu items, you need to remove the default connections to the cocoa methods. Select each, and in the “Connections” pane select the appropriate connection and then click the ‘Disconnect’ button at the bottom. Then, in your script add the code to write and read the file from disk. Last, attach a choose menu item handler to the open and save menu items, and work the two read/write bits of code into the handlers for those items to enable the save and open features.

(* Read a file *)
set {File_OK, File_Contents} to read_File("Macintosh HD:Users:jed:Desktop:Test.txt")

to read_File(tmp_File_Path)
	try
		set File_Contents to (read file tmp_File_Path) as string
		return {true, File_Contents}
	on error
		return {false, ""}
	end try
end read_File

(* Write a file *)
write_File("Macintosh HD:Users:jed:Desktop:Test.txt")

to write_File(tmp_File_Path)
	try
		set tmp_File_Path to (tmp_File_Path as file specification)
		set File_Contents to "<<<Provide your text here>>>"
		open for access tmp_File_Path with write permission
		set eof of tmp_File_Path to 0
		write (File_Contents) to tmp_File_Path starting at eof
		close access tmp_File_Path
		return true
	on error
		try
			close access tmp_File_Path
		end try
		return false
	end try
end write_File

j

Thanks for the reply. It is a non-document based app. so I didn’t realize I had to remove the connections. I’ll give it a shot when I get a chance. I really appreciate the time you took to answer. Hopefully I won’t have to pick your brain again on this subject but it’s not a guarantee… :slight_smile:

Maybe I’m just rushing it and missing something, but I’m not sure where to put what bit of code. I filled in my info. Not sure which part of this code to put into the choose menu command. Sorry for the trouble.

(* Write a file *)
write_File("MacHD:Users:erod:Desktop:Test.txt")

to write_File(tmp_File_Path)
	try
		set tmp_File_Path to (tmp_File_Path as file specification)
		set File_Contents to contents of text view "textToConvert" of scroll view "textConvert" of window "main" as string
		open for access tmp_File_Path with write permission
		set eof of tmp_File_Path to 0
		write (File_Contents) to tmp_File_Path starting at eof
		close access tmp_File_Path
		return true
	on error
		try
			close access tmp_File_Path
		end try
		return false
	end try
end write_File

Thanks again.

Only the…

…line, or a derivative thereof, should be in your choose menu item handler (along with any other code that prepares to write the file). The rest of the code is a subroutine which must be outside of any handlers or other subroutines. Just paste it to the end of your script and it should work fine.

The code I posted was the bare basics of the code. I would probably actually do something like the following…

on choose menu item theObject
	if name of theObject is "Save" then
		set tmpFile to "MacHD:Users:erod:Desktop:Test.txt"
		--> OR...
		--> Insert another method of gathering a file path here

		(* Try to write the file *)
		if (write_File(tmpFile)) is false then display dialog "Error saving file!" with icon 2

	end if
end choose menu item

Note that this only works when you have a valid path already established. You’ll have to use open and save panels, or standard additions’ choose file/choose folder/choose file name, to gather your path info assuming your don’t want one single path hardcoded into your app.

Both the read and write subroutines return some sort of boolean value indicating whether the routine was successful, so you can use the routine itself to evaluate it’s success. In the above example, if the file is not written, it returns a dialog stating so.

Good luck,
j

Thanks again jobu. You rock. I will try it soon. Right now I have to get some recording done. Programming doesn’t pay the bills…ha ha

Now I´ve come this far, I found the function write_to_file somewhere, I havn´t analyzed it yet, but it seems to work.

What I need now as the loop thing where a file is created and written to, and this for all the urls in an array or soething like it.

set this_url to "http://www.5clubs.com/backup/?username=xxx&password=xxx"

on get_url(this_url)
	set browser_string to "'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9'"
	return do shell script "curl " & (quoted form of this_url) & " -A " & browser_string
end get_url

set shortDate to do shell script "date +%y%m%d"

set browser_string to "'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/125.4 (KHTML, like Gecko) Safari/125.9'"

set sHtml to do shell script "curl " & (quoted form of this_url) & " -A " & browser_string


set this_file to (((path to desktop folder) as text) & "MY STORY.txt")
my write_to_file(sHtml, this_file, false)

on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file