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

Place embedded scriptable ?

New Here ,
Nov 13, 2024 Nov 13, 2024

Hello, I'm trying to create around 500 mockups using the file: hand-poster-mockup.psd. I want the script to open the smart object thumbnail, place embedded the design, save the smart object and the new file, and like this loop through all of my designs. 

I'm uploading a few designs. 

whenever I try scripting, all of my files are poorly placed. here are some scripts 

// Define file paths

// Main function to create mockups for all .jpg files in the folder
function createMockupsForAllFiles() {
var postersFolder = new Folder(postersFolderPath);
var designFiles = postersFolder.getFiles("*.jpg"); // Get all .jpg files in the folder

if (designFiles.length === 0) {
alert("No .jpg files found in the folder: " + postersFolderPath);
return;
}

for (var i = 0; i < designFiles.length; i++) {
var designFile = designFiles[i];
var designFileName = designFile.name.replace(".jpg", "").replace("-no-frame", ""); // Remove ".jpg" and "-no-frame"
createMockup(designFile, designFileName);
}

alert("All mockups created successfully.");
}

// Function to create a single mockup
function createMockup(designFile, designFileName) {
var outputFileName = designFileName + "-hands-poster-mockup.jpg";
var outputFilePath = outputFolderPath + "/" + outputFileName;

// Check if the output file already exists
var outputFile = new File(outputFilePath);
if (outputFile.exists) {
$.writeln("Mockup already exists, skipping: " + outputFileName);
return; // Skip if file already exists
}

// Open the mockup template
var mockupDoc = open(File(mockupTemplatePath));

// Find the smart object layer
var smartObjectLayer = findSmartObjectLayer(mockupDoc);
if (smartObjectLayer == null) {
alert("Smart object layer not found in mockup template.");
return;
}

// Open the smart object
openSmartObject(smartObjectLayer);

// Place the design file into the smart object using Place Embedded
placeEmbedded(designFile);

// Fit the design to the smart object bounds
fitLayerToCanvas();

// Save and close the smart object to update main mockup
app.activeDocument.close(SaveOptions.SAVECHANGES);

// Save the updated mockup as a .jpg file with quality level 7
saveAsJPEG(mockupDoc, outputFilePath);

// Close the mockup document
mockupDoc.close(SaveOptions.DONOTSAVECHANGES);

$.writeln("Mockup created: " + outputFileName);
}

// Function to find the smart object layer by type
function findSmartObjectLayer(doc) {
for (var i = 0; i < doc.layers.length; i++) {
if (doc.layers[i].kind == LayerKind.SMARTOBJECT) {
return doc.layers[i];
}
}
return null;
}

// Function to open the smart object
function openSmartObject(layer) {
app.activeDocument.activeLayer = layer;
var idPlacedLayerEditContents = stringIDToTypeID("placedLayerEditContents");
executeAction(idPlacedLayerEditContents, undefined, DialogModes.NO);
}

// Corrected function to place the design file as an embedded layer
function placeEmbedded(filePath) {
var actDesc = new ActionDescriptor();
actDesc.putPath(charIDToTypeID('null'), filePath); // Corrected 'null' reference
actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa')); // Fit to canvas
executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO);
}

// Function to fit the pasted layer to the smart object bounds
function fitLayerToCanvas() {
var layer = app.activeDocument.activeLayer;
layer.resize(100, 100); // Scale the layer to fit the canvas
layer.translate(-(layer.bounds[0].as("px")), -(layer.bounds[1].as("px"))); // Position it at the top left
}

// Function to save the document as JPEG with quality level 7
function saveAsJPEG(doc, path) {
var jpegFile = new File(path);
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 7; // Set quality to 7

doc.saveAs(jpegFile, jpegOptions, true); // Save as a copy in JPEG format
}

// Run the script for all files
createMockupsForAllFiles();



I've spent a couple of days trying to figure it out, any help will be much appreciated! 


TOPICS
Actions and scripting
451
Translate
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
Adobe
Community Expert ,
Nov 13, 2024 Nov 13, 2024

Yes, place linked and embedded are scriptable. An example here:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/layers-creating-layers/td-p/13252109

 

As is editing a smart object and smart object replacement and relinking etc.

Translate
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
New Here ,
Nov 13, 2024 Nov 13, 2024

Hey Stephen, thank you! I tried following your logic but it's not working yet 😞 

function placeEmbedded(filePath) {
// Place the file as an embedded smart object
var desc = new ActionDescriptor();
desc.putPath(charIDToTypeID("null"), filePath);
desc.putEnumerated(charIDToTypeID("FTcs"), charIDToTypeID("QCSt"), charIDToTypeID("Qcsa"));
executeAction(charIDToTypeID("Plc "), desc, DialogModes.NO);

// Get the newly placed layer (smart object layer)
var smartObjectLayer = app.activeDocument.activeLayer;

// Calculate the target dimensions to fit proportionally
var docBounds = app.activeDocument.bounds;
var docWidth = docBounds[2].as("px") - docBounds[0].as("px");
var docHeight = docBounds[3].as("px") - docBounds[1].as("px");

var layerBounds = smartObjectLayer.bounds;
var layerWidth = layerBounds[2].as("px") - layerBounds[0].as("px");
var layerHeight = layerBounds[3].as("px") - layerBounds[1].as("px");

// Calculate scale factor to fit proportionally within the document
var scaleFactor = Math.min(docWidth / layerWidth, docHeight / layerHeight);
smartObjectLayer.resize(scaleFactor * 100, scaleFactor * 100);

// Center the layer within the document
var newLayerBounds = smartObjectLayer.bounds;
var offsetX = (docWidth - (newLayerBounds[2].as("px") - newLayerBounds[0].as("px"))) / 2 - newLayerBounds[0].as("px");
var offsetY = (docHeight - (newLayerBounds[3].as("px") - newLayerBounds[1].as("px"))) / 2 - newLayerBounds[1].as("px");
smartObjectLayer.translate(offsetX, offsetY);
}

Translate
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
New Here ,
Nov 13, 2024 Nov 13, 2024
LATEST

this is what i'm getting 

Translate
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