Saving The Contents Of Changed Text Fields And Restoring The Contents When Relaunching The AppleScirpt Studio Application.
I hope this helps someone with AppleScript Studio. This can be used to change text fields or any other fields and have the values saved and restored. Maybe I should of sent a file.
OS version: OS X
----***** SAVING THE CONTENTS OF CHANGED TEXT FIELDS AND RESTORING THE CONTENTS WHEN RELAUNCHING THE AppleScirpt Studio Application.****
----- There should be two different AppleScripts and one text file.
----- This is not one script it is an example with two scripts on one page
----- The first half should be used in one file and the second half in another file.
on clicked theObject
---- I used "on clicked theObject" but anything else will do. I linked the script to a button that loaded a page.
-- This is one way to save preferences in AppleScript Studio.
--- The first part is getting the preferences from a file and setting a text field in an AppleScript studio application.
-- This little part gets the file path and creates a path for the preferences file.
-- This string is stored in the variable theResult1.
----------
set this_filepath1 to (path to me as string)
set this_filepath1 to POSIX path of this_filepath1
set theResult1 to (quoted form of this_filepath1) & "/Contents/Resources/custabpref1.txt" as string
----------
-------------------------------------------------------------------------------------
--This part will set scriptToRun1 to a shell command with the complete file path.
set scriptToRun1 to "awk -F: ' {print $1}' " & theResult1
----- awk -F: This sets the field seperator to : .
----- '{print $1}' This will return field number 1.
------- & theResult1 That is the path and the text file name to store the preferences.
---- The text file will look something like this.
------ try to click:the brown house:red cars:whatever:whoknows:
----There are no key/value pairs. Every field is used.
-------------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- This next part just runs the shell command that was set up above.
--- The variable CustomScript1 will hold the resulting text.
set Customscript1 to ((do shell script scriptToRun1) as string)
---------------------------------------------------------------------------------------------------------------
--- The next five code groups are just like the first except the text results are stored in different variables.
--- field1:field2:field3:field4:field5:field6
---Each little group gathers its own field.
set scriptToRun2 to "awk -F: ' {print $2}' " & theResult1
set Customscript2 to ((do shell script scriptToRun2) as string)
set scriptToRun3 to "awk -F: ' {print $3}' " & theResult1
set Customscript3 to ((do shell script scriptToRun3) as string)
set scriptToRun4 to "awk -F: ' {print $4}' " & theResult1
set Customscript4 to ((do shell script scriptToRun4) as string)
set scriptToRun5 to "awk -F: ' {print $5}' " & theResult1
set Customscript5 to ((do shell script scriptToRun5) as string)
set scriptToRun6 to "awk -F: ' {print $6}' " & theResult1
set Customscript6 to ((do shell script scriptToRun6) as string)
-------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------
--------- The next six lines will set a text field to the word stored in the variables used above.
---- The text field is called "CustomScript1". This is the text field name set in AppleScript Studio.
--- Interface Builder >>> menu bar at top of page >>>> Tools >>>> Show Info >>>> AppleScript from drop down >>>> Name:"the name goes here"
----- OK , you get the point.
set the contents of text field "Customscript1" of view of tab view item "acustom" of tab view "expert" of window "main" to Customscript1
set the contents of text field "Customscript2" of view of tab view item "acustom" of tab view "expert" of window "main" to Customscript2
set the contents of text field "Customscript3" of view of tab view item "acustom" of tab view "expert" of window "main" to Customscript3
set the contents of text field "Customscript4" of view of tab view item "acustom" of tab view "expert" of window "main" to Customscript4
set the contents of text field "Customscript5" of view of tab view item "acustom" of tab view "expert" of window "main" to Customscript5
set the contents of text field "Customscript6" of view of tab view item "acustom" of tab view "expert" of window "main" to Customscript6
-----------------------------------------------------------------------------
---- OK, that's it the text fields are set to whatever was the text file.
end clicked
------*************************************************************************************************************************************
-----------------------------------------------------------------------------
----***********THE NEXT PART IS A WHOLE DIFFERENT SCRIPT. THAT SHOULD BE IN A DIFFERENT AppleScript file. *************************************
-----------------------------------------------------------------------------
-- This is the part that stores the contents of text fields in a plain text file.
on end editing theObject
-- The first part sets the path and preferences file name.
set this_filepath to (path to me as string)
set this_filepath to POSIX path of this_filepath
set theResult to (quoted form of this_filepath) & "/Contents/Resources/custabpref1.txt" as string
--------------------------------------------------
-- The variable defeditout1 is set to the contents of the text field "Customscript1"
--The rest of the line is just the location. The text field is in a tab view called "acustom" , of tab view "expert" in a window called "main".
-- The six lines are the same except for the variable and text field.
set defeditout1 to the contents of text field "Customscript1" of view of tab view item "acustom" of tab view "expert" of window "main"
set defeditout2 to the contents of text field "Customscript2" of view of tab view item "acustom" of tab view "expert" of window "main"
set defeditout3 to the contents of text field "Customscript3" of view of tab view item "acustom" of tab view "expert" of window "main"
set defeditout4 to the contents of text field "Customscript4" of view of tab view item "acustom" of tab view "expert" of window "main"
set defeditout5 to the contents of text field "Customscript5" of view of tab view item "acustom" of tab view "expert" of window "main"
set defeditout6 to the contents of text field "Customscript6" of view of tab view item "acustom" of tab view "expert" of window "main"
-----------------------------------------------------------------
--- This part runs the shell commmand "echo " notice the space after "echo ".
--- The contents of the text fields are in the variables and a ":" will seperate the strings in the preferences file.
--- The ending part of the shell command >| . This means to overwrite the file if anything is in it. I wanted the new prefs to overwrite the old.
--- The last variable is theResult which is the file name and path from above.
((do shell script "echo " & defeditout1 & ":" & defeditout2 & ":" & defeditout3 & ":" & defeditout4 & ":" & defeditout5 & ":" & defeditout6 & " >| " & theResult) as string)
----- If I edit one text field then all the text fields are gathered up and written to the preferences file.
end end editing