0

When you drag an image into Photoshop, it creates it as a smart object and if I understand correctly, it embeds a copy of the original image inside the PSD file. So if you were to go into the smart object and export the image as a JPEG, you should be able to maintain the full quality of the original image. (correct me if I'm wrong here)

I have some Photoshop files that have many images in them but we lost the original images on the computer and we need them back again. To extract all the images as JPEGS, I would have to manually double click each smart object and export to JPG. There are hundreds of these. Is there a script that can do this in a batch?

I can't create an action to loop through all smart objects because there are many other types of smart objects that we DON'T need.

6
  • 1
    How the script would differentiate between the smart object you need and you don't? Commented Feb 7, 2019 at 6:49
  • Simple. Scripts have Javascript so you have the freedom to dig much deeper into coding than actions. The script can look at the file name in the smart object and see if it's a jpg. You can also give it parameters to meet your needs and distinguish between what you need and what you don't, like underlying image dimensions. Commented Feb 7, 2019 at 11:00
  • I know what scripts can do, I write them :). My question was the exact conditions you need to distinguish between the smart objects. For example if I put a file in the document it doesn't have an extension: prntscr.com/mhuby1 Commented Feb 7, 2019 at 11:13
  • ok, I got it, I can check if the original file is jpg if you didn't modify it Commented Feb 7, 2019 at 11:52
  • Wow, I will check your script tomorrow because it's late here, but to answer what you asked - when you open a smart object, the tab that opens, has the full original file name - with the extension. Commented Feb 7, 2019 at 17:09

1 Answer 1

1

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:

enter image description here

And result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.