Script for placing a linked smart object into a new document with specific parameters
I want to throw a bunch of TIFFs inside a folder, create a new document, place an embedded linked smart object of the original TIFF, and change the filename.
Most of the relevant details are in the code. Can someone tell me what's wrong with the code? Bonus points if you can just edit my code so that it'll work... please. 😄
I'm receiving this error:
Error 8800: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.
- Could not place the document because of a program error.
Line: 85
-> executeAction(charIDToTypeID('Plc '), desc, DialogModes.NO);
Here's the code:
/*************************************************************
* Custom Batch "Place Linked" for .TIF portraits
*
* 1) Loops through a source folder for .TIF files
* 2) Creates a new 2800 x 920 px @ 300 ppi, RGB doc w/ white bg
* 3) Places each .TIF as a *Linked* Smart Object
* 4) Saves as "originalFilename_bio.tif" with LZW compression
* 5) Closes the doc
*************************************************************/
(function() {
// -- 1. Specify your source/destination folders --
var sourceFolder = Folder("/Users/m1.k/project.k/2025-01-06_GW_Keker/Selects");
var destFolder = Folder("/Users/m1.k/project.k/2025-01-06_GW_Keker/bio");
// -- 2. Make sure folders exist --
if (!sourceFolder.exists) {
alert("Source folder does not exist: " + sourceFolder.fsName);
return;
}
if (!destFolder.exists) {
destFolder.create();
}
// -- 3. Grab all .TIF files in the source folder --
var files = sourceFolder.getFiles(/\.(tif|tiff)$/i);
if (files.length === 0) {
alert("No .TIF files found in:\n" + sourceFolder.fsName);
return;
}
// -- 4. Iterate over each file --
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Parse the file name (strip extension)
var baseName = decodeURI(file.name).replace(/\.[^\.]+$/, "");
// e.g. "portrait_01.TIF" -> "portrait_01"
// -- 4a. Create a new doc (2800 x 920 px, 300 ppi, RGB, white background) --
var docWidth = 2800; // px
var docHeight = 920; // px
var docRes = 300; // ppi
var docName = baseName + "_bio";
var newDoc = app.documents.add(
docWidth,
docHeight,
docRes,
docName,
NewDocumentMode.RGB,
DocumentFill.WHITE
);
// -- 4b. Place the file as a Linked Smart Object --
placeLinkedSmartObject(file);
// (Optional) No positioning or scaling is done here—feel free to move/resize
// your Smart Object manually afterward.
// -- 4c. Save as TIF with LZW compression in the dest folder --
var saveFile = File(destFolder + "/" + baseName + "_bio.tif");
saveAsTifLZW(newDoc, saveFile);
// -- 4d. Close the doc (no extra save prompt) --
newDoc.close(SaveOptions.DONOTSAVECHANGES);
}
alert("All done! Created " + files.length + " expanded TIF files.");
// ======================================================================
// HELPER FUNCTIONS
// ======================================================================
/**
* Places the given file as a Linked Smart Object into the current doc.
* Uses Photoshop's Action Manager code to do "Place Linked."
*/
function placeLinkedSmartObject(fileRef) {
var desc = new ActionDescriptor();
var desc2 = new ActionDescriptor();
desc2.putPath(charIDToTypeID('null'), fileRef);
desc2.putBoolean(stringIDToTypeID('linked'), true);
desc.putObject(charIDToTypeID('Usng'), stringIDToTypeID('placeEvent'), desc2);
executeAction(charIDToTypeID('Plc '), desc, DialogModes.NO);
}
/**
* Saves current doc as a TIF with LZW compression.
*/
function saveAsTifLZW(docRef, outFile) {
var tifOptions = new TiffSaveOptions();
tifOptions.imageCompression = TIFFEncoding.TIFFLZW; // LZW compression
tifOptions.layers = true; // Keep layers (smart object remains)
docRef.saveAs(outFile, tifOptions, true, Extension.LOWERCASE);
}
})();
