Here you go. This will export all .png
and .jpg
embedded files to desktop.
var myDoc = activeDocument,
layers = getAllLayers();
var exportFolder = new Folder(Folder.desktop + "/" + myDoc.name); //change this line to export to a specific folder;
exportFolder.create()
for (var i = 0; i < layers.length; i++)
{
//if smart object
if (layers[i].type == 5)
{
var ref;
ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putIdentifier(charIDToTypeID('Lyr '), layers[i].id);
var smartName = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference")),
nameMatch = smartName.match(/(\.jpg|\.png)/);
if (nameMatch != null)
{
selectById(layers[i].id);
var desc12 = new ActionDescriptor();
executeAction(stringIDToTypeID('placedLayerEditContents'), desc12, DialogModes.NO); //opening a smart object so that the original file would be created in the Temp folder
var myFile = new File(Folder.temp + "/" + smartName);
if (myFile.exists) {
if (!myFile.copy(exportFolder + "/" + myFile.name)) {
alert('Couldn\'t copy ' + smartName + ' to ' + exportFolder);
}
};
activeDocument.close(); //closing the smart object
}
}
};
function getAllLayers()
{
var lyrs = [];
try
{
activeDocument.backgroundLayer;
var layers = 0
}
catch (e)
{
var layers = 1;
};
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try
{
var desc = executeActionGet(ref);
}
catch (err)
{
break;
}
var lyr = {};
lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
lyr.index = layers;
lyrs.push(lyr)
layers++;
}
return lyrs
}
function selectById(id) {
var desc1 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putIdentifier(charIDToTypeID('Lyr '), id);
desc1.putReference(charIDToTypeID('null'), ref1);
executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
};
My input:
And result: