Skip to main content
Mohamed Hameed21513110
Inspiring
July 11, 2023
Question

Add some lines for the selected layers

  • July 11, 2023
  • 2 replies
  • 1133 views

Greetings to all

I collected some lines and came up with this code, which is to create a Smart Object for the layer, and then open the Smart Object and save the file based on the dimensions of the document

But I just want a simple modification, which is to perform this procedure on the selected layers (meaning the layers you selected) and the procedure is performed on them

is this possible??

 

This is the code

var currentDoc = app.activeDocument;
if (app.activeDocument.activeLayer.kind == "LayerKind.SMARTOBJECT") {
    runMenuItem(stringIDToTypeID('placedLayerEditContents'))
    Save ();
  
} else {
    app.runMenuItem(stringIDToTypeID('newPlacedLayer'));
    runMenuItem(stringIDToTypeID('placedLayerEditContents'));
    Save ();
}

function Save () {
var doc = app.activeDocument;
var currentDoc = app.activeDocument;
var docpath = doc.path;
var activeLayer = doc.activeLayer;
var Sizze= Math.round(currentDoc.width)+ "       " + Math.round(currentDoc.height)
var saveFile = File(docpath + "/" + Sizze + ".jpg");
while(saveFile.exists){
    Sizze = ' '+ Sizze ;    
    saveFile = File(docpath + "/" +Sizze + ".jpg")
    };
  saveJPG(saveFile, 10);
function saveJPG(saveFile, jpegQuality) {
  var jpgSaveOptions = new JPEGSaveOptions();
  jpgSaveOptions.embedColorProfile = true;
  jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
  jpgSaveOptions.matte = MatteType.NONE;
  jpgSaveOptions.quality = jpegQuality;
  activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
};
app.activeDocument.close();
}

 I just want to add some lines to perform the action on the selected layers, not just one layer

This topic has been closed for replies.

2 replies

Stephen Marsh
Community Expert
Community Expert
July 12, 2023

@Mohamed Hameed21513110 

 

The first question to ask is – does the script work as intended as it currently is?

 

I don't believe that it does.

 

Mohamed Hameed21513110
Inspiring
July 12, 2023

@Stephen Marsh 

Sorry sir, I don't understand what you mean

I sent you the code after adding my lines
Can you try the code to show you the problem

Stephen Marsh
Community Expert
Community Expert
July 12, 2023

Line 1 sets the variable to the original/parent/containing layered file:

 

var currentDoc = app.activeDocument;

 

Then line 14 redefines the variable again, this time to the opened smart object document.

 

Then the JPEG is saved into the folder using the path of the variable of the smart object document, which is a buried temp folder if the SO is embedded.

 

So changing line 1 to:

 

var parentDoc = app.activeDocument;

 

Then line 15 to:

 

var docpath = parentDoc.path;

 

Will at least save the JPEG file to the parent PSD file location, rather than the temporary directory for the smart object PSB file.

 

So before trying to make this work for multiple selected layers, it needs to work for a single selected layer. But perhaps I misunderstood your intent.

 

P.S. Do you really wish to have files named such as "503       172.jpg" with 7 word spaces?

 

This example was when I had my rulers set to pixels, but when changing the rulers to CM I get "6       2.jpg". Rather than presuming the unit of measure, it is best to explicitly set it in the script:

 

https://gist.github.com/MarshySwamp/898ae3237305787d0683125166aeb480

 

Mohamed Hameed21513110
Inspiring
July 11, 2023

@Stephen Marsh 

thank you sir
But I find it difficult to understand these codes, and when I put my code with this article, it did not work, and this problem appeared with me

 

 

 

Mohamed Hameed21513110
Inspiring
July 12, 2023

@Stephen Marsh 

I also tried the second code, but it only works on one layer, with a message showing that there is a problem with the layers

I don't know where the problem is

if (app.documents.length > 0) {
    var myDocument = app.activeDocument;
    myDocument.suspendHistory("Undo script...", "main(myDocument)");
}

function main() {
    var theLayers = getSelectedLayersIdx();
    for (var p = 0; p < theLayers.length; p++) {
        selectLayerByIndex(theLayers[p], false);

        // Your code here
        if (activeDocument.activeLayer.kind == "LayerKind.SMARTOBJECT") {
    runMenuItem(stringIDToTypeID('placedLayerEditContents'))
  
} else {
    app.runMenuItem(stringIDToTypeID('newPlacedLayer'));
    runMenuItem(stringIDToTypeID('placedLayerEditContents'));

}
       }
}


///// Functions /////

function selectLayerByIndex(index, add) {
    add = undefined ? add = false : add
    var ref = new ActionReference();
    ref.putIndex(charIDToTypeID("Lyr "), index);
    var desc = new ActionDescriptor();
    desc.putReference(charIDToTypeID("null"), ref);
    if (add) desc.putEnumerated(stringIDToTypeID("selectionModifier"), stringIDToTypeID("selectionModifierType"), stringIDToTypeID("addToSelection"));
    desc.putBoolean(charIDToTypeID("MkVs"), false);
    try {
        executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
    } catch (e) {
        alert(e.message);
    }
}

// by paul mr
function getSelectedLayersIdx() {
    var selectedLayers = new Array;
    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc = executeActionGet(ref);
    if (desc.hasKey(stringIDToTypeID('targetLayers'))) {
        desc = desc.getList(stringIDToTypeID('targetLayers'));
        var c = desc.count
        var selectedLayers = new Array();
        for (var i = 0; i < c; i++) {
            try {
                activeDocument.backgroundLayer;
                selectedLayers.push(desc.getReference(i).getIndex());
            } catch (e) {
                selectedLayers.push(desc.getReference(i).getIndex() + 1);
            }
        }
    } else {
        var ref = new ActionReference();
        ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("ItmI"));
        ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
        try {
            activeDocument.backgroundLayer;
            selectedLayers.push(executeActionGet(ref).getInteger(charIDToTypeID("ItmI")) - 1);
        } catch (e) {
            selectedLayers.push(executeActionGet(ref).getInteger(charIDToTypeID("ItmI")));
        }
    }
    return selectedLayers;
}