Thursday, April 06, 2006
Logging to a text file
Many scripts encounter errors or unusual circumstances as they run. Sometimes, it is desirable for the script to simply stop in such circumstances reporting the error to the user for immediate attention. But in other situations it is better for a script to soldier on, simply logging its findings to a text file.
The function makeLogFile() facilitates this. It creates a text file (which, in truth could be used for anything) using the string aName as the basis for naming the file. The file is located in the same folder as the document passed in the argument aDoc. If aDoc is untitled, then the log file is created in the same folder as the script. The third argument (delete) is a Boolean. It determines whether the script should delete an existing file of the designated name or create a unique name based on the first argument.
The function makeLogFile() facilitates this. It creates a text file (which, in truth could be used for anything) using the string aName as the basis for naming the file. The file is located in the same folder as the document passed in the argument aDoc. If aDoc is untitled, then the log file is created in the same folder as the script. The third argument (delete) is a Boolean. It determines whether the script should delete an existing file of the designated name or create a unique name based on the first argument.
function makeLogFile(aName, aDoc, deleteIt) {Notice that this script depends on the function getScriptPath being available. It also doesn't actually create the log file, merely a file object. This means that a script which never actually logs anything won't create a log file. Here is the code I used to test this function:
var logLoc; // path to folder that will hold log file
try {
logLoc = aDoc.filePath;
} catch (e) {
logLoc = getScriptPath().parent.fsName
}
var aFile = File(logLoc + "/" + aName + ".txt");
if (deleteIt) {
aFile.remove();
return aFile;
}
var n = 1;
while (aFile.exists) {
aFile = File(logLoc + "/" + aName + String(n) + ".txt");
n++
}
return aFile
}
//DESCRIPTION: Test making log fileRun this and two text files are opened, one named test.txt and one named test1.txt. That's because the first call has delete it set to true while the second has it set false. As a result, because we logged something to the log1 file before creating the log2 file object, two separate files were created.
myDoc = app.activeDocument;
log1 = makeLogFile("test",myDoc,true);
log(log1, "Text for log1 file");
log2 = makeLogFile("test",myDoc,false);
log(log2, "Text for log2 file");
log1.execute();
log2.execute();
function makeLogFile(aName, aDoc, deleteIt) {
var logLoc; // path to folder that will hold log file
try {
logLoc = aDoc.filePath;
} catch (e) {
logLoc = getScriptPath().parent.fsName
}
var aFile = File(logLoc + "/" + aName + ".txt");
if (deleteIt) {
aFile.remove();
return aFile;
}
var n = 1;
while (aFile.exists) {
aFile = File(logLoc + "/" + aName + String(n) + ".txt");
n++
}
return aFile
}
function getScriptPath() {
// This function returns the path to the active script, even when running ESTK
try {
return app.activeScript;
} catch(e) {
return File(e.fileName);
}
}
function log(aFile, message) {
var today = new Date();
if (!aFile.exists) {
// make new log file
aFile.open("w");
aFile.write(String(today) + "\nThe following messages were logged:\n");
aFile.close();
}
aFile.open("e");
aFile.seek(0,2);
aFile.write("\n" + message);
aFile.close();
}
Comments:
<< Home
Can this script be used or modified to track and manage graphics files into an excel or filemaker Dbase?
I'm not sure I completely follow the question. You could certainly log tab or comma delimited information to a text file which could then be imported into Excel or Filemaker.
This script provides the framework for the logging. It leaves it to the calling script to decide what to log.
Dave
Post a Comment
This script provides the framework for the logging. It leaves it to the calling script to decide what to log.
Dave
<< Home