Preferences and Resources

I have an AS .txt text editor (all on my public iDisk imadrichard in the scripts folder named Richard’s Text Editor 3) which has a dialog text entry, which I want to be a large text field in ASOC.

ATM I have this around the start:

set val1 to (read file ((path to resource "Save Location Pref.txt") as string)) --get contents of of file, which will be written as a path as string to be made an alias later because when distributed it is set to "Default"

And these at the end:

open for access (file ((path to resource "Save Location Pref.txt") as string)) with write permission
	set eof (file ((path to resource "Save Location Pref.txt") as string)) to 0
	write (val3 as string) to (file ((path to resource "Save Location Pref.txt") as string)) as text
	close access (file ((path to resource "Save Location Pref.txt") as string))

(Would you believe all those brackets are needed! lol)

What is the equivalent resource method in ASOC?

Also is it easier to use .plist ?

Here is a way to read a text file inside your bundle. Writing to it is very similar.
Look at NSString in the docs for writeToFile method.

	on buttonClick_(sender)
		tell current application's class "NSBundle"
			tell its mainBundle()
				set textFile to its pathForResource_ofType_("sample", "txt")
			end tell
		end tell
		
		tell class "NSString" of current application
			-- NSUTF8StringEncoding has a value of 4
			set textFileContents to its stringWithContentsOfFile_encoding_error_(textFile, 4, missing value)
		end tell
		textView's setString_(textFileContents)
	end buttonClick_

It’s probably worth pointing out that you can still use your AS read/write commands if you wish.

But I wonder if what you should really be looking at is using user defaults instead.

I would just like the least ObjC like (unless it compromises things) way to do what my script already does. However I will need a write command for writing to the edit file too. OK, I think at this point it’s easiest to just paste the whole thing for people to see.

display dialog "Open last saved or choose file" buttons {"Open last", "Open other"} default button 2
if button returned of result is "Open last" then
	set val1 to (read file ((path to resource "Save Location Pref.txt") as string))
	--Read pref file to find out path of last edited file.
	if val1 is "Default" then
		EditFile("no")
	else
		EditFile(val1 as alias)
	end if
else
	EditFile("no")
end if

on EditFile(val2)
	if val2 is "no" then
		set val3 to (choose file with prompt "Please select a .txt file:

To save a text file as .txt in TextEdit, click 'Make Plain Text' in the Format menu and save the file.")
		--Get file path.
	else
		set val3 to val2
	end if
	if (get eof val3) is 0 then --Read doesn't like eof (end of file) to be 0 bytes.
		set val4 to ""
	else
		set val4 to read val3 --Read file before it gets erased.
	end if
	open for access val3 with write permission --Open file.
	set eof val3 to 0 --Erase file.
	repeat
		display dialog "Edit your text." default answer val4 buttons {"Revert", "Save"}
		set {val5, val6} to {text returned of result, button returned of result}
		if val6 is "Save" then
			write val5 to val3
			exit repeat
		end if
	end repeat
	--Write to file giving old data as default (to give the same affect as as read and edit).
	close access val3
	open for access (file ((path to resource "Save Location Pref.txt") as string)) with write permission
	set eof (file ((path to resource "Save Location Pref.txt") as string)) to 0
	write (val3 as string) to (file ((path to resource "Save Location Pref.txt") as string)) as text
	close access (file ((path to resource "Save Location Pref.txt") as string))
	--Save path of edited file to pref file.
end EditFile

You probably only need to use pathForResource_ofType_ instead of “path to resource”, as in Craig’s post, and put the read/write stuff inside a “tell current application” block.

Is this another ASOC thing? Do I have to put many standard commands inside tell current app?

And what are the parameters for the pathForResource_ofType_ command?

:)… :frowning: I’m getting quite confused

I’ve only found it with the read/write commands so far.

Get used to using Xcode’s Help → Developer Documentation. Search for pathToResource:ofType:, and you’ll see it requires the name of the file and the extension. So in your case: “Save Location Pref”, “txt”

OK but search doesn’t work

OK reeaalllyyy weird thing just happened. I tried out of interest to put a blank text file in the resources folder of the project and run basically a copy of my AppleScript. I do, it should crash after clicking open last but it actually finds the last used file from the bundle version, it even keeps working on build and run without the text file in the resources folder. I did copy and paste the whole script from AS Editor and insert the awakeFromNib bits so did the path to resource command remember the path to the bundle or something?