Hello everybody!
This weekend I gave some more studies about these UXP Plugins and I addressed the theme of importing images again.
The theoretically simple processes at first sight type:
1 - Add some layers of frames
2 - Add photos only on selected layers: OK
OBS! I couldn’t find something that shows how to get and run something in just selected layers, I improvised something like this:
2.1: Group selected layers
2.2: Browse, get and create a matrix only with the names of the grouped layers
2.3: Go back to the previous step in the history to return the layers to their proper positions index .
3 - Test if the selected layers match: OK
Sample 1: Teste 01 - YouTube
4 - I inserted images in them: Error
The respective image is only inserted when selecting twice until done.
Sample 2: Teste erro 1 - YouTube
5 - When selecting the next layers, the images are inserted in the layers already processed and then inserted in the current selection: Error
Sample 3: Teste erro 2 - YouTube
Finally, I have no idea what could be going on.
Here the js that you add the images
document.querySelector("#btn2").addEventListener("click", evt => {
core.executeAsModal( async() => { await obterCamadas()})
})
async function obterCamadas(){
selects =[]
//Group selected layers
await app.batchPlay( [{"_obj":"make","_target":[{"_ref":"layerSection"}],"from":{"_enum":"ordinal","_ref":"layer","_value":"targetEnum"}}], { synchronousExecution: false, });
/////////
const doc = app.activeDocument;
//Gets the names of the selected layers
const result = await app.batchPlay( [ {_obj: 'get', _target: [{ _property: 'name' }, { _ref: "layer", _enum: "ordinal", _value: "targetEnum" } ] }], { synchronousExecution: false, });
const name = result[0].name;
//Loops, gets and adds the names in an array
doc.layers.forEach(layer =>{ if (layer.kind == "group"){ layer.layers.forEach(camada =>{ selects.push(camada.name);})}});
//Go back to the previous step in the history
await app.batchPlay( [{"_obj":"select","_target":[{"_enum":"ordinal","_ref":"historyState","_value":"previous"}]}], { synchronousExecution: false, });
// Invert the order of the list in ascending order
sel = selects.reverse();
for(var i = 0;i<sel.length; i++){
sel[i];
await app.batchPlay( [{"_obj":"select","_target":[{"_name":sel[i],"_ref":"layer"}],"layerID":[739],"makeVisible":false}], { synchronousExecution: false, });
await addImagens();
//alert(sel[i])
}
}
async function addImagens(){
const fs = require('uxp').storage.localFileSystem;
let entry = await fs.getFileForOpening();
let token = fs.createSessionToken(entry);
await action.batchPlay([
// import
{"descriptor": {"ID":709,"_obj":"placeEvent","freeTransformCenterState":{"_enum":"quadCenterState","_value":"QCSAverage"},"null":{"_kind":"local","_path":token },"offset":{"_obj":"offset","horizontal":{"_unit":"distanceUnit","_value":0.0},"vertical":{"_unit":"distanceUnit","_value":0.0}},"replaceLayer":{"_obj":"placeEvent","from":{"_id":659,"_ref":"layer"},"to":{"_id":709,"_ref":"layer"}}}, "options": {"dialogOptions": "display"}},
// Transform
{"descriptor": {"_obj":"transform","freeTransformCenterState":{"_enum":"quadCenterState","_value":"QCSAverage"},"height":{"_unit":"percentUnit","_value":105.45300722965703},"offset":{"_obj":"offset","horizontal":{"_unit":"distanceUnit","_value":-1.3642420526593923e-14},"vertical":{"_unit":"distanceUnit","_value":1.3642420526593923e-14}},"replaceLayer":{"_obj":"transform","from":{"_id":727,"_ref":"layer"},"to":{"_id":727,"_ref":"layer"}},"width":{"_unit":"percentUnit","_value":105.45300722965692}}, "options": {"dialogOptions": "display"}}
], { });
}