• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Jan 17, 2025 Jan 17, 2025

Copy link to clipboard

Copied

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

})();

 

 

 

Adobe Photoshop 26.2.0
M1 Mac Mini 16GB ram
Ventura 13.4
TOPICS
Actions and scripting , macOS

Views

122

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 18, 2025 Jan 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.activeDocum
...

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 17, 2025 Jan 17, 2025

Copy link to clipboard

Copied

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 17, 2025 Jan 17, 2025

Copy link to clipboard

Copied

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.0
M1 Mac Mini 16GB ram
Ventura 13.4

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2025 Jan 18, 2025

Copy link to clipboard

Copied

Are your preferences/settings set to resize oversized on place?

 

What happens when you place linked manually? 

What pixel dimensions and resolution PPI is the source image being placed compared to the target document?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2025 Jan 18, 2025

Copy link to clipboard

Copied

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

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 18, 2025 Jan 18, 2025

Copy link to clipboard

Copied

Thanks again @Stephen Marsh 

Adobe Photoshop 26.2.0
M1 Mac Mini 16GB ram
Ventura 13.4

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2025 Jan 18, 2025

Copy link to clipboard

Copied

LATEST

You're welcome @kle0211 - did that reset transform do the job?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines