Skip to main content
New Participant
January 17, 2025
Answered

Script for placing a linked smart object into a new document with specific parameters

  • January 17, 2025
  • 1 reply
  • 574 views

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);
    }

})();

 

 

 

Correct answer Stephen Marsh

Later versions of Photoshop offer an option to reset the SO transformation (here with an optional additional centre on canvas):

 

try {
// Reset the SO transform
executeAction(stringIDToTypeID("placedLayerResetTransforms"), undefined, DialogModes.NO);
// Centre the SO layer on the canvas
app.activeDocument.activeLayer.translate(app.activeDocument.width / 2 - (app.activeDocument.activeLayer.bounds[0] + app.activeDocument.activeLayer.bounds[2]) / 2,
app.activeDocument.height / 2 - (app.activeDocument.activeLayer.bounds[1] + app.activeDocument.activeLayer.bounds[3]) / 2);
} catch (err) {
alert("Error!" + "\r" + err + "\r" + 'Line: ' + err.line);
}

 

 

1 reply

Stephen Marsh
Community Expert
January 18, 2025

@kle0211 try this updated function:

 

    function placeLinkedSmartObject(fileRef) {
        var s2t = function (s) {
            return app.stringIDToTypeID(s);
        };
        var AD = new ActionDescriptor();
        AD.putInteger(s2t("ID"), 1);
        AD.putPath(s2t("null"), fileRef);
        AD.putBoolean(s2t("linked"), true); // false for embedded
        AD.putEnumerated(s2t("freeTransformCenterState"), s2t("quadCenterState"), s2t("QCSAverage"));
        AD.putUnitDouble(s2t("horizontal"), s2t("pixelsUnit"), 0);
        AD.putUnitDouble(s2t("vertical"), s2t("pixelsUnit"), 0);
        AD.putObject(s2t("offset"), s2t("offset"), AD);
        executeAction(s2t("placeEvent"), AD, DialogModes.NO);
    }

 

kle0211Author
New Participant
January 18, 2025

Thank you @Stephen Marsh!

 

Is there any way for the linked smart object to populate in the new document as native resolution? It's scaling 100% height. 

 

 I appreciate your help!

Adobe Photoshop 26.2.0M1 Mac Mini 16GB ramVentura 13.4
Stephen Marsh
Stephen MarshCorrect answer
Community Expert
January 18, 2025

Later versions of Photoshop offer an option to reset the SO transformation (here with an optional additional centre on canvas):

 

try {
// Reset the SO transform
executeAction(stringIDToTypeID("placedLayerResetTransforms"), undefined, DialogModes.NO);
// Centre the SO layer on the canvas
app.activeDocument.activeLayer.translate(app.activeDocument.width / 2 - (app.activeDocument.activeLayer.bounds[0] + app.activeDocument.activeLayer.bounds[2]) / 2,
app.activeDocument.height / 2 - (app.activeDocument.activeLayer.bounds[1] + app.activeDocument.activeLayer.bounds[3]) / 2);
} catch (err) {
alert("Error!" + "\r" + err + "\r" + 'Line: ' + err.line);
}