Can pre Batch script process thru the list of items?
Hello!
For a folder of image tiles with a col#_row#.jpg naming scheme, how could a pre Batch script process thru the items and get the max row# and col# to set as variables to be used in the main script?
Thanks in advance
EDIT:
Found example what I needed in another forum post. Feel free to offer improvements, though!
For the preBatch script:
var numCols = 1;
var numRows = 1;
for (j = 0; j < app.itemCount; j++) {
item = app.getItem(j);
tmpitem= item.name;
n = tmpitem.indexOf("_");
column = tmpitem.substr(0,n);
row = tmpitem.substr(n+1);
c = parseInt(column);
if (c > numCols) numCols = c+1;
r = parseInt(row);
if (r > numRows) numRows = r+1;
}
console.log(numCols);
For the Main Script (converts col_row scheme to sequential number from upper right of matrix):
tmpitem= item.name;
n = tmpitem.indexOf("_");
column = tmpitem.substr(0,n);
row = tmpitem.substr(n+1);
c = parseInt(column);
r = parseInt(row);
num = (r*numCols)+ c;
// numCols is the number of columns, set during PreBatch
item.newName = num.toString();
return item.newName
For a folder of image tiles with a col#_row#.jpg naming scheme, how could a pre Batch script process thru the items and get the max row# and col# to set as variables to be used in the main script?
Thanks in advance
EDIT:
Found example what I needed in another forum post. Feel free to offer improvements, though!
For the preBatch script:
var numCols = 1;
var numRows = 1;
for (j = 0; j < app.itemCount; j++) {
item = app.getItem(j);
tmpitem= item.name;
n = tmpitem.indexOf("_");
column = tmpitem.substr(0,n);
row = tmpitem.substr(n+1);
c = parseInt(column);
if (c > numCols) numCols = c+1;
r = parseInt(row);
if (r > numRows) numRows = r+1;
}
console.log(numCols);
For the Main Script (converts col_row scheme to sequential number from upper right of matrix):
tmpitem= item.name;
n = tmpitem.indexOf("_");
column = tmpitem.substr(0,n);
row = tmpitem.substr(n+1);
c = parseInt(column);
r = parseInt(row);
num = (r*numCols)+ c;
// numCols is the number of columns, set during PreBatch
item.newName = num.toString();
return item.newName
Pre batch:
var cols=[];
for (n=0; n<app.itemCount; n++) cols[n] = app.getItem(n).name.match(/\d*/)[0];
numCols = 1 + Math.max.apply(Math, cols);
Main script:
CR = item.name.match(/(\d*)_(\d*)/);
return CR[2] * numCols + 1 * CR[1];
var cols=[];
for (n=0; n<app.itemCount; n++) cols[n] = app.getItem(n).name.match(/\d*/)[0];
numCols = 1 + Math.max.apply(Math, cols);
Main script:
CR = item.name.match(/(\d*)_(\d*)/);
return CR[2] * numCols + 1 * CR[1];