Read and write - find and replace ???

Hello,
I am a new applescripter and a new Apple convert for that matter. ( I can’t believe it took me so long)
Anyway, I am writing a program that I want to store information in a text file that is dynamically dependent upon user input. (My Mac Mini in my car with a touch screen)
EXAMPLE: I want two sets of strings at 8 words long each - “text1”,“text2”,“text3”,“text4”,“text5”,“text6”,“text7”,“text8” and “app1”,“app2”,“app3”,“app4”,“app5”,“app6”,“app7”,“app8”
I want to change the text only if the user changes the text field in the “form” and if left blank it will default to what is in the text file.
I want the whole operation to work behind the scenes… so -no user file selection- just automatic search and replace.
I am not sure if I have covered all of my criteria but will answer any questions you may have and am willing to colaborate with anyone interested in this type of program. I have screen shots included.

http://www.cynicalband.com/images/main.png
http://www.cynicalband.com/images/settings.png
http://www.cynicalband.com/images/shut_down.png

Thank you,
James

I was interested, once upon a time, in a similar project involving a touch screen.

Seems you’re using Xcode/Interface Builder and AppleScript (aka “AppleScript Studio”), so I’d say you need a simple set of preferences which you can load at launch-time? If so:

http://bbs.applescript.net/viewtopic.php?id=11368

Hi jj,

I apologize for my ignorance but is there anyway you could help me further. I’m really not quite sure how to implement this into my program?
Do I need to set up the plist file if so how?
On my settings page: http://www.cynicalband.com/images/settings.png
I have the input text fields named “InputName1”, “InputAppName1”,“InputName2”, “InputAppName2” and so on.
For the main page: http://www.cynicalband.com/images/main.png
I have the text of each button assigned as “button1”, “button2” and the button has it’s own scriptas follows:
on clicked theObject
set application1 to contents of text field “AppNameStorage1” of window “main”
tell application application1
activate
end tell
end clicked

What I need to know is how to use your applescript so that users input of “InputName1” is reflected in the “button1” field and the “InputAppName2” is reflected in the “AppNameStorage1” and so on up to 8 choices?

Thank you,
James

The plist file is created automatically. See this:

--> make these vars avaible to all the script
global inputName1, appName1, inputName2, appName2

on awake from nib theObject --> attach this action to your app, so this code runs at launch time
	--> load from text file whatever values (if none, assign default ones on-the-fly)
	set {inputName1, appName1, inputName2, appName2} to ¬
		my loadprefs({{"inputName1", "iTunes"}, {"appName1", "iTunes"}, {"inputName2", "Movies"}, {"appName2", "DVD Player"}})
	
	---> update main window with the related values
	updateMainWindow()
end awake from nib

on will open theObject --> attached to the settings window
	if name of theObject is "settings" then
		--> populate preferences window's text fields with loaded values
		tell theObject
			set contents of text field "input name 1" to inputName1
			set contents of text field "app name 1" to appName1
			set contents of text field "input name 2" to inputName2
			set contents of text field "app name 2" to appName2
		end tell
	end if
end will open

on clicked theObject --> attached to your "Change" button
	if name of theObject is "Change" then
		--> pick current value of text fields
		tell window of theObject
			set inputName1 to contents of text field "input name 1"
			set appName1 to contents of text field "app name 1"
			set inputName2 to contents of text field "input name 2"
			set appName2 to contents of text field "app name 1"
		end tell
		
		--> save it as prefs (so the app "remembers" these values the next time)
		my savePrefs({{"inputName1", inputName1}, {"appName1", appName1}, {"inputName2", inputName2}, {"appName2", appName2}})
		
		---> update main window with the new values
		updateMainWindow()
	end if
end clicked

to updateMainWindow()
	tell window "main"
		set contents of text field "input_name_1" to inputName1
		set contents of text field "input_name_2" to inputName2
	end tell
end updateMainWindow

to loadprefs(thesePrefs)
	set loadedPrefs to {}
	repeat with i in thesePrefs
		set iName to i's item 1 as text
		if default entry iName of user defaults exists then
			set end of loadedPrefs to contents of default entry iName of user defaults
		else
			set defaultValue to (i's item 2)
			set end of loadedPrefs to defaultValue
			make new default entry at end of every default entry of user defaults with properties {name:iName, contents:defaultValue}
		end if
	end repeat
	return loadedPrefs
end loadprefs
to savePrefs(thesePrefs)
	repeat with i in thesePrefs
		set entryName to contents of (i's item 1)
		set entryValue to contents of (i's item 2)
		try
			set contents of default entry entryName of user defaults to entryValue
		on error
			make new default entry at end of every default entry of user defaults with properties {name:entryName, contents:entryValue}
		end try
	end repeat
end savePrefs

Moving to AS Studio.

Hi jj,
I am sorry to bother you and am very appreciative for your expertise and time you have put into this for me. I made the scripts and attached them to the corresponding elements i.e… button, onload, and onawakefromNIB etc. but I am getting this error and am not sure why?
http://www.cynicalband.com/images/Picture%202.png

Please help… I want to get this running so I can share it with everyone and maybe even get more people involved.

Thank You,
James

See (in the loadPrefs line, the following one):

my loadPrefs(({

Should be:

my loadPrefs({{

Hi jj,

I changed it and had the same result. I’m sure it’s something I did but I can not figure it out. Here is the same error:
http://www.cynicalband.com/images/Picture%202.png

Thank you,
James

jj,

I’ve been working on my program with your script and have finally figured it out! It works great and I can’t wait until I am finished!

Thank you very much,
James

Interface programming is a great waste of time: make work buttons, save/load prefs… But when you have it all assembled and able to talk to your original code (the idea behind this load of buttons and windows and dialogs), it’s very… cool? :stuck_out_tongue: