Incrementing Sequential Page Numbers

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Mike Hale

Incrementing Sequential Page Numbers

Post by Mike Hale »

Better yet,

return decodeURI(f.name).match(/^\d{3,9}\s-\s.+\..+/);

Will let you pick up where the script failed. It will only match files starting with at least 3 digits. I put a high top range so you should be good to go.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

redeemed

Incrementing Sequential Page Numbers

Post by redeemed »

Mike,

The first example I had already tried the + quantifier but the script was then recognizing 100 as smaller than 20 so I figured a sorting function would fix that.

The second example of course starts the numbering over at 1 so 100 would have to be added to the page number before the insertion.

Alternatively, I could batch rename all the files to something like 001 - filename.psd if that is the path of least resistance.

Thoughts?
Mike Hale

Incrementing Sequential Page Numbers

Post by Mike Hale »

Sorry, again. I guess I am trying to do too many things at once.

This line controls the loop

for(var p=1;p<pages.length;p++){

So you should be able to control where it starts page numbering. I would try that first - I think if the pages were saved in order they should already be in that order for the script.

If you do need to sort, I will post a alphanum sort for file object a little later. That is on my other computer which is bogged down doing a large test run for another script I am working on.
Paul MR

Incrementing Sequential Page Numbers

Post by Paul MR »

Here is a alphaNumeric sort Mike, only takes strings not file objects though.

Code: Select allfunction sortAlphaNumeric(a, b) {
   var x = a.split("/");
   var y = b.split("/");
   x = x[x.length-1].replace(/\\\s/g," ").split(/(\d+)/);
   y = y[y.length-1].replace(/\\\s/g," ").split(/(\d+)/);
   for (var i in x) {
      if (x && !y || isFinite(x) && !isFinite(y)) {
         return -1;
      } else if (!x && y || !isFinite(y) && isFinite(y)) {
         return 1;
      } else if (!isFinite(x) && !isFinite(y)) {
         x[i] = x[i].toLowerCase();
         y[i] = y[i].toLowerCase();
         if (x[i] < y[i]) return -1;
         if (x[i] > y[i]) return 1;
      } else {
         x[i] = parseFloat(x[i]);
         y[i] = parseFloat(y[i]);
         if (x[i] < y[i]) return -1;
         if (x[i] > y[i]) return 1;
      }
   }
   return 0;
}
Mike Hale

Incrementing Sequential Page Numbers

Post by Mike Hale »

It looks like that is sorting file path strings because it splits on '/'

So I think all you would need to is...

var x = decodeURI(a.fullName).split("/");
var y = decodeURI(b.fullName).split("/");

And maybe add a if statement to check the args so it can handle both file objects and path strings.
Paul MR

Incrementing Sequential Page Numbers

Post by Paul MR »

Spot on Mike yes thar works fine I just changed it to ..

Code: Select allfunction sortAlphaNumeric(a, b) {
    if(a  instanceof File){
        var x = decodeURI(a.fullName).split("/");
        var y = decodeURI(b.fullName).split("/");
        }else{
   var x = a.split("/");
   var y = b.split("/");
    }
   x = x[x.length-1].replace(/\\\s/g," ").split(/(\d+)/);
   y = y[y.length-1].replace(/\\\s/g," ").split(/(\d+)/);
   for (var i in x) {
      if (x && !y || isFinite(x) && !isFinite(y)) {
         return -1;
      } else if (!x && y || !isFinite(y) && isFinite(y)) {
         return 1;
      } else if (!isFinite(x) && !isFinite(y)) {
         x[i] = x[i].toLowerCase();
         y[i] = y[i].toLowerCase();
         if (x[i] < y[i]) return -1;
         if (x[i] > y[i]) return 1;
      } else {
         x[i] = parseFloat(x[i]);
         y[i] = parseFloat(y[i]);
         if (x[i] < y[i]) return -1;
         if (x[i] > y[i]) return 1;
      }
   }
   return 0;
}


Works fine for file objects now, thank you.
Mike Hale

Incrementing Sequential Page Numbers

Post by Mike Hale »

I think I would also change the name to something that make clearer what the function does, like uriAlphaNumSort. I don't think it works with Windows style paths and may have problems with other kinds of strings the have forward slashes.
redeemed

Incrementing Sequential Page Numbers

Post by redeemed »

Success!

I had to revisit the original code you posted since it worked well up to 99.

I had to add this line of code:

var p2 = parseInt(p,10) + 100; // perl is sooo much easier on the eyes: $p += 100

And modify this one:

addPageNumber( p2 );

And all is well.

Of course I spent as much time on this as it would have taken to manually edit the files, but I learned a couple of things.

Javascript is a strange animal to me. It was also cumbersome to test the script after changes were made.

Gents, thanks again.

Redeemed
Paul MR

Incrementing Sequential Page Numbers

Post by Paul MR »

Thanks once again Mike.
What I have normally done is to pass the filenames only when they have come from one folder