At my level of scripting, I found it easier to work from a plain text file.
The following example writes 3 separate lines which will be used as variables to a file on the desktop:
/* WRITE TO PREF FILE */
// Pref file platform specific LF options
var os = $.os.toLowerCase().indexOf("mac") >= 0 ? "mac" : "windows";
if (os === "mac") {
prefFileOutLF = "Unix"; // Legacy = "Macintosh"
} else {
prefFileOutLF = "Windows";
}
// Create the preference file
var prefFileOut = new File('~/Desktop/My_Preference_File.txt');
if (prefFileOut.exists)
prefFileOut.remove();
prefFileOut.open("w");
prefFileOut.encoding = "UTF-8";
prefFileOut.lineFeed = prefFileOutLF;
var dateTime = new Date().toLocaleString();
prefFileOut.writeln(dateTime + ", " + "Source Doc: " + app.activeDocument.name);
prefFileOut.writeln(activeDocument.width.value);
prefFileOut.writeln(activeDocument.height.value);
prefFileOut.close();
The following script will read the 3 separate lines as variables from the text file and transform two of the variables from strings to numbers:
/* READ FROM PREF FILE */
var prefFileIn = File('~/Desktop/My_Preference_File.txt');
if (File(prefFileIn).exists && File(prefFileIn).length > 0) {
// Read the preference file from the user's desktop
prefFileIn.open('r');
// Read the 1st line from the log file, a means to an end...
var logInfo = prefFileIn.readln(1);
// Read the 2nd line from the log file & convert the string to a number/integer
var prefFileWidthValue = Math.floor(prefFileIn.readln(2));
//alert(prefFileWidthValue.toSource());
// Read the 3rd line from the log file & convert the string to a number/integer
var prefFileHeightValue = ~~prefFileIn.readln(3);
//alert(prefFileHeightValue.toSource());
prefFileIn.close();
// Demo - alert on the variables
alert(logInfo);
alert(prefFileWidthValue);
alert(prefFileHeightValue);
// Debugging
$.writeln(logInfo);
$.writeln(prefFileWidthValue);
$.writeln(prefFileHeightValue);
} else {
app.beep();
alert('There is no valid file named "My_Preference_File.txt" on the desktop!');
}