Copy link to clipboard
Copied
I work with 7 digit and 8 digit filenames that are based on skus. They typically look like 100-100_A or 100-1000_A.
Problem #1
Right now I have 2 different scripts that will save into a folder structure based on the filename. One for the 7 digit filenames and one for the 8 digit. Is there anyway to combine these? I am new to scripting and have tried numerous different things with no luck. The scripts may not be perfect but they work.
Problem #2
I would love to work in error checking. These scripts run as part of actions. Right now it only alerts when the file saves. I would like it to return an error if it doesn't save.
Script #1
//only 7 Digit filenames are allowed. ZZ12345_A or 123-456_A
var aDoc = activeDocument, nme = aDoc.name.replace(/\.[^\.]+$/,""), reg = /^.{7}_./, tmp = nme.split("");
checkAndSave();
function checkAndSave() {
if( reg.test( nme ) == false) {
alert ("wrong file name"); return; }
else { tmp.splice(-2);
for( i = 1; i < 7; i++ ) { tmp = tmp[i-1] + tmp; };
var saveFol = Folder ("/Catalog-3/Agility/AgilityImages/Products/7-Digit"); //if script stops saving try changing to /Catalog, /Catalog-1, /Catalog-2 etc. You can also find the active path by running activepath.jsx on a file in the 7-digit product folder.
for ( i = 0; i < tmp.length; i++ ) {
saveFol = Folder( saveFol + "/" + tmp );
if (!saveFol.exists) { saveFol.create() };
};
var saveName = "/" + nme + ".tif";
var saveFile = new File( saveFol + saveName );
if(saveFile.exists){
if(!confirm( saveFile + "\nFile already exists.. Do you want to overwrite?", false, "Overwrite file?"))
return;
}
saveAsTIFF( saveFile ); alert( saveFol + saveName + " saved" )
}; return;
};
function saveAsTIFF( saveFile ) {
tiffSaveOptions = new TiffSaveOptions();
tiffSaveOptions.layers = true;
tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
activeDocument.saveAs( saveFile, tiffSaveOptions, false);
}
Script#2
// only 8 digit filenames allowed. 12345678_A or 123-4567_A
var aDoc = activeDocument, nme = aDoc.name.replace(/\.[^\.]+$/,""), reg = /^.{8}_./, tmp = nme.split("");
checkAndSave();
function checkAndSave() {
if( reg.test( nme ) == false) {
alert ("wrong file name"); return; }
else { tmp.splice(-2);
for( i = 1; i < 8; i++ ) { tmp = tmp[i-1] + tmp; };
var saveFol = Folder ("/Catalog-3/Agility/AgilityImages/Products/8-Digit"); //if script stops saving try changing to /Catalog, /Catalog-1, /Catalog-2 etc. You can also find the active path by running activepath.jsx on a file in the 7-digit product folder.
for ( i = 0; i < tmp.length; i++ ) {
saveFol = Folder( saveFol + "/" + tmp );
if (!saveFol.exists) { saveFol.create() };
};
var saveName = "/" + nme + ".tif";
var saveFile = new File( saveFol + saveName );
if(saveFile.exists){
if(!confirm( saveFile + "\nFile already exists.. Do you want to overwrite?", false, "Overwrite file?"))
return;
}
saveAsTIFF( saveFile ); alert( saveFol + saveName + " saved" )
}; return;
};
function saveAsTIFF( saveFile ) {
tiffSaveOptions = new TiffSaveOptions();
tiffSaveOptions.layers = true;
tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
activeDocument.saveAs( saveFile, tiffSaveOptions, false);
}
test this script
///////////////////////////////////////////////////////////////////////////
var aDoc = activeDocument;
var nme = aDoc.name.replace(/\.[^\.]+$/,"");
var tmp = nme.split("");
var reg7 = /^.{7}_./;
var reg8 = /^.{8}_./;
var digits;
if (reg7.test(nme))
{
digits = 7;
checkAndSave();
}
else if (reg8.test(nme))
{
digits = 8;
checkAndSave();
}
else
{
alert ("Wrong file name");
}
function checkAndSave() {
tmp.splice(-2);
for( i = 1; i < digits;
...Copy link to clipboard
Copied
test this script
///////////////////////////////////////////////////////////////////////////
var aDoc = activeDocument;
var nme = aDoc.name.replace(/\.[^\.]+$/,"");
var tmp = nme.split("");
var reg7 = /^.{7}_./;
var reg8 = /^.{8}_./;
var digits;
if (reg7.test(nme))
{
digits = 7;
checkAndSave();
}
else if (reg8.test(nme))
{
digits = 8;
checkAndSave();
}
else
{
alert ("Wrong file name");
}
function checkAndSave() {
tmp.splice(-2);
for( i = 1; i < digits; i++ ) { tmp = tmp[i-1] + tmp; };
var saveFol = Folder ("/Catalog-3/Agility/AgilityImages/Products/"+digits+"-Digit"); //if script stops saving try changing to /Catalog, /Catalog-1, /Catalog-2 etc. You can also find the active path by running activepath.jsx on a file in the 7-digit product folder.
for ( i = 0; i < tmp.length; i++ ) {
saveFol = Folder( saveFol + "/" + tmp );
if (!saveFol.exists) { saveFol.create() };
};
var saveName = "/" + nme + ".tif";
var saveFile = new File( saveFol + saveName );
if(saveFile.exists){
if(!confirm( saveFile.fsName + "\nFile already exists.. Do you want to overwrite?", false, "Overwrite file?"))
return;
}
if (saveAsTIFF( saveFile )) alert( File(saveFol + saveName).fsName + " saved", "OK" )
else alert( "SAVE ERROR!!\n \n" + File(saveFol + saveName).fsName, "Error", true )
};
function saveAsTIFF( saveFile ) {
tiffSaveOptions = new TiffSaveOptions();
tiffSaveOptions.layers = true;
tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
try { activeDocument.saveAs( saveFile, tiffSaveOptions, false); } catch (e) { return false; }
return true;
}
Copy link to clipboard
Copied
Works perfect! I was very close! Thank you