Save last scriptlistner output

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Kukurykus, Tom, xbytor

Mike Hale

Save last scriptlistner output

Post by Mike Hale »

Here is a little script that will save the last scriptlistner output. You may want to change the output folder and Mac users will have to change the location of the scriptlistner log.

Code: Select allmain = function(){
File.prototype.readByte = function() {// file function adapted from Xbytor's
  return this.read(1).charCodeAt(0);
};

var fileRef = new File("/c/ScriptingListenerJS.log");
var marker = 0;
var string = " ";

fileRef.open( "r" );
fileRef.seek( 0, 2 );
var readPos = fileRef.length;
while(marker != 47){
   marker = fileRef.readByte();
   readPos = readPos - 1;
   fileRef.seek( readPos , 0 );
};

fileRef.seek( fileRef.tell()-1, 0 );
var logFile = new File("/c/temp/"+prompt( "Enter a name")+".log");
logFile.open( "w");

while (fileRef.tell() < fileRef.length){
string = fileRef.readln();
logFile.writeln(string);
};
fileRef.close;
logFile.close;
};
main();

xbytor

Save last scriptlistner output

Post by xbytor »

Here's my take on this.

One thing to note is that the 'readByte' is not necessary since you're not reading from a binary file. File.readch works fine and takes care of any CRLF issues.

Also, being paranoid, I look for a "\n/" pattern to indicate the beginning of the last entry. If I can't find that, I check for "/" at the beginning of the file.

Code: Select allStdlib.getLastJSLogEntry = function(fptr) {
  if (fptr) {
    fptr = Stdlib.convertFptr(fptr);
  } else {
    fptr = new File("/c/ScriptingListenerJS.log");
    if (!fptr.exists) {
      throw "Unable to find SLC log.";
    }
  }

  fptr.open("r") || throwFileError(fptr, "Unable to open");

  fptr.seek(1, 2);  // start of at the end of the file
  var prev = fptr.readch();

  for (var i = 2; i < fptr.length; i++) {
    fptr.seek(i, 2);  // start of at the end of the file
    var c = fptr.readch();
    if (c == '\n' && prev == '/') {
      break;
    }
    prev = c;
  }
  if (i == fptr.length && prev != '/') {
    return undefined;
  }

  fptr.readln();
  var str = fptr.read();
  fptr.close();
  return str;
};

Mike Hale

Save last scriptlistner output

Post by Mike Hale »

Thanks, X

We can use my version as an example of someone trying thier best to code like you, but without your knowledge.

xbytor

Save last scriptlistner output

Post by xbytor »

It's more like mine is the paranoid version, which probably makes it a bit more difficult to read.

Larry Ligon

Save last scriptlistner output

Post by Larry Ligon »

Here is how I do this:

bb/viewtopic.php?t=662