Copy link to clipboard
Copied
I've been using this script and action for years. Got a new Mac and now I get the attached error.
Here's the process:
Any thoughts? Thanks!
// Pfade übernehmen v2.1
// Copyright Thamer Florian
// 30.11.2010
var docRef = app.activeDocument;
var docJPG= app.activeDocument;
// Check for a "TIF","PSD","JPG" Subdirectory, which contains the Images where the paths should be applied to.
var checksum = "0";
for( var i = "1"; i <= "3"; i++){
if( i == "1" ) {
var newpath = docRef.path + "/TIF/";
newpath = new File(newpath);
if(newpath.exists) {
checksum++;
var dateityp = "TIF";
}
}
if( i == "2" ) {
var newpath = docRef.path + "/PSD/";
newpath = new File(newpath);
if(newpath.exists) {
checksum++;
var dateityp = "PSD";
}
}
if( i == "3" ) {
var newpath = docRef.path + "/JPG/";
newpath = new File(newpath);
if(newpath.exists) {
checksum++;
var dateityp = "JPG";
}
}
}
if( checksum > "1") alert("Error! please create a single tif, psd or jpg subfolder"); // Error if there is more than one possible subdirectory
if( checksum < "1") alert("Error! No tif, psd or jpg subfolder available"); // Error if there is no possible subdirectory
if( checksum == "1") { // The Script will only run if there is exacly ONE compatible subdirectory
// open the TIF/PSD/JPG file with the same name as the JPG
var jpg = docRef.name;
var fname = docRef.name.split(".");
var fname1 = "";
for (var k = 0; k <= fname.length - 2; k++) {
fname1 = fname1 + fname
}
var fnamenew = fname1 + dateityp;
var newpath = docRef.path + "/" + dateityp + "/" + fnamenew;
var docRef = app.activeDocument;
var TIF = newpath;
// Opening the TIF/PSD/JPG file
var TIFShortcut = new File(TIF);
if( !TIFShortcut.exists ) {
alert("Oops! File " + TIFShortcut + " not found!" + jpg + " remains open"); // if the file does not exist stop scripts
}
else {
var docTIF = open(TIFShortcut);
// Check Resolution/Canvas size
app.activeDocument = docJPG;
if( docTIF.resolution != docJPG.resolution ) var sizeerr = "";
if( (docTIF.width != docJPG.width) || (docTIF.length != docJPG.length) ) var sizeerr = "";
if (sizeerr != null) {
alert("Document Size / Resolution different! \ NThe file" + jpg + " has a wrong document size / resolution. \ It will remain open");
docTIF.close();
}
else {
// Check color mode, color profile and bits per channel, and correct them
if( docTIF.mode != docJPG.mode ) docJPG.convertProfile(docTIF.colorProfileName, Intent.RELATIVECOLORIMETRIC, true, true);
if( docTIF.colorProfileName != docJPG.colorProfileName ) docJPG.colorProfileName = docTIF.colorProfileName;
if( docTIF.bitsPerChannel != docJPG.bitsPerChannel) docJPG.bitsPerChannel = docTIF.bitsPerChannel;
// So this is where i copy all the layers from the e.g. TIF to the JPG with paths
// I use the Select All Layers Command (cmd+alt+A - as far as i know not accessible through a menu)
// Problem is: It select all Layers EXCEPT the background layer
// So if there is a BG Layer i convert it to a normal layer first and rebackgroundize it afterwards.
app.activeDocument = docTIF;
var ebenen = docTIF.artLayers.length-"1";
if (docTIF.artLayers[ebenen].isBackgroundLayer == true) {
docTIF.artLayers[ebenen].isBackgroundLayer = false;
var hgset = "1";
}
// Select all layers
// =======================================================
var idselectAllLayers = stringIDToTypeID( "selectAllLayers" );
var desc14 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref8 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref8.putEnumerated( idLyr, idOrdn, idTrgt );
desc14.putReference( idnull, ref8 );
executeAction( idselectAllLayers, desc14, DialogModes.NO );
// DRAG all layers to the other document, this increases speed, cause nothing goes into clipboard
// and I don't copy layer by layer.
// =======================================================
var idDplc = charIDToTypeID( "Dplc" );
var desc50 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref40 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref40.putEnumerated( idLyr, idOrdn, idTrgt );
desc50.putReference( idnull, ref40 );
var idT = charIDToTypeID( "T " );
var ref41 = new ActionReference();
var idDcmn = charIDToTypeID( "Dcmn" );
ref41.putName( idDcmn, jpg ); // jpg = target document
desc50.putReference( idT, ref41 );
var idVrsn = charIDToTypeID( "Vrsn" );
desc50.putInteger( idVrsn, 5 );
executeAction( idDplc, desc50, DialogModes.NO );
// close the original TIF/PSD/JPG file
docTIF.close(SaveOptions.DONOTSAVECHANGES);
// delete the layer from the JPG file that has been in Asia
var ebenen = docRef.artLayers.length-1;
docRef.artLayers[ebenen].remove();
// set the bottom layer as background if it was before
if (hgset) {
var ebenen = docRef.artLayers.length-1;
docJPG.artLayers[ebenen].isBackgroundLayer = true;
}
// and finally overwrite TIF/PSD/JPG in the TIF/PSD/JPG subdirectory
switch( dateityp ) {
case("TIF"): docRef.saveAs(new File(newpath),TiffSaveOptions); break;
case("PSD"): docRef.saveAs(new File(newpath),PhotoshopSaveOptions); break;
case("JPG"): docRef.saveAs(new File(newpath),JPEGSaveOptions); break;
}
docRef.close();
}
}
}
Copy link to clipboard
Copied
Are yur sure this is the correct script?
Because the line #11 (with the error --> var theImage = app.activeDocument; ) doesn't exists in the script that you have posted.
Copy link to clipboard
Copied
Yes, that was my fault. But, that was a good troubleshooting tip. It seems like, when I put the action calling the script in a file/automate/batch it has trouble finding an active document. Even though the batch should open a file and that should be the active document. Here's what I get now:
Does that make sense? Thanks!
Copy link to clipboard
Copied
Make your script this way
try {
app.bringToFront();
// ...
// put your original srcipt body here
// ...
}
catch(e) { alert("Line: " + e.line + "\n\n" + e, "Error", true);
Copy link to clipboard
Copied
Hi, r-bin. Still the same error in the last screenshot. It does run fine if I have an active doc open in Photoshop, just not from a File/Automation/Batch with the attached settings. Thanks!
Copy link to clipboard
Copied
The alert should indicate the number of the line.
UPD,
There was an error in the previous post. Missing brackets
try {
app.bringToFront();
// ...
// put your original srcipt body here
// ...
}
catch(e) { alert("Line: " + e.line + "\n\n" + e, "Error", true); }
Copy link to clipboard
Copied
Thanks, r-bin, but same result. If I hit continue on the error when running, there are errors with numbers of the line but they refer again to not having an active doc. Thanks!
Copy link to clipboard
Copied
I did not understand. Do you have at least one open document when the error occurs?
And further. Try in Bacth to change the Destination from "Save and close" to "None"
Copy link to clipboard
Copied
I don't have any doc open when the error occurs. Tried batching both way but no success. Thanks!
Copy link to clipboard
Copied
But this means that the script has nothing to do with it. Most likely something is broken in your Actcion, which calls the script, and which for some reason closes the file before calling the script.