Script to create a SMART OBJECT from a GROUP

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

aquafat11

Script to create a SMART OBJECT from a GROUP

Post by aquafat11 »

Hi there! I stumbled upon this forum months ago and it's been an invaluable resource for me the past few months for write one of my first PS scripts. I've just registered now because I have a question of my own. Hope someone can help me!

I've generated a script that will look for certain smart objects with specific names in an active file and export them as png's. I'm trying to figure out now how to script the action of selecting a group named "icon_large" and convert it to a smart object. The group will always have the name "icon_large", but the contents of the group may change. That shouldn't matter for the purposes of what the script will hopefully do.

Thanks in advance for any help you all may be able to offer! I promise to donate if I can get this problem solved! Cheers!

-aquafat11

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

jamesrmac

Script to create a SMART OBJECT from a GROUP

Post by jamesrmac »

Code: Select allexecuteAction(app.stringIDToTypeID('newPlacedLayer'), undefined, DialogModes.NO);
That code will turn whatever layers or layerset that is selected into a smart object

i use a function i found and modifed to find layers by name. Here is a full script that selects the icon_large layerset and makes it a smart object.
Code: Select allif(layerExist("icon_large")){
    executeAction(sTID('newPlacedLayer'), undefined, DialogModes.NO); 
   }

function layerExist(lyrName){       //checks to see if layer exist and makes it active if it does
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putName( cTID( "Lyr " ), lyrName);
    desc.putReference( cTID( "null" ), ref );
    desc.putBoolean( cTID( "MkVs" ), false );
    try{ executeAction( cTID( "slct" ), desc, DialogModes.NO ); }catch(e){return false;}
    return true;
};
 
function cTID(s) { return app.charIDToTypeID(s); };             // these lines make the codeso much cleaner, thanks xbytor!
function sTID(s) { return app.stringIDToTypeID(s); };
aquafat11

Script to create a SMART OBJECT from a GROUP

Post by aquafat11 »

awesome! Thanks for the help, this worked and more importantly, I was able to figure out what i was doing wrong! Thank you!
jamesrmac

Script to create a SMART OBJECT from a GROUP

Post by jamesrmac »

Glad to help. This place has helped me a lot and I'm glad to give back.
aquafat11

Script to create a SMART OBJECT from a GROUP

Post by aquafat11 »

One more addition:

If I wanted to add code to ensure that "icon_large" (or whatever it's named) is visible, or if it's not visible to turn it on, would I add this?

Code: Select allif(layerExist("icon_large")){
    executeAction(sTID('newPlacedLayer'), undefined, DialogModes.NO); 
   }

function layerExist(lyrName){       //checks to see if layer exist and makes it active if it does
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putName( cTID( "Lyr " ), lyrName);
    desc.putReference( cTID( "null" ), ref );
    desc.putBoolean( cTID( "MkVs" ), false );
    try{ executeAction( cTID( "slct" ), desc, DialogModes.NO ); }catch(e){return false;}
    return true;
    app.activeDocument.activeLayer.visible = !app.activeDocument.activeLayer.visible;

};
 
function cTID(s) { return app.charIDToTypeID(s); };             // these lines make the codeso much cleaner, thanks xbytor!
function sTID(s) { return app.stringIDToTypeID(s); };



aquafat11

Script to create a SMART OBJECT from a GROUP

Post by aquafat11 »

One more addition:

If I wanted to add code to ensure that "icon_large" (or whatever it's named) is visible, or if it's not visible to turn it on, would I add this?

Code: Select allif(layerExist("icon_large")){
    executeAction(sTID('newPlacedLayer'), undefined, DialogModes.NO); 
   }

function layerExist(lyrName){       //checks to see if layer exist and makes it active if it does
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putName( cTID( "Lyr " ), lyrName);
    desc.putReference( cTID( "null" ), ref );
    desc.putBoolean( cTID( "MkVs" ), false );
    try{ executeAction( cTID( "slct" ), desc, DialogModes.NO ); }catch(e){return false;}
    return true;
app.activeDocument.activeLayer.visible = !app.activeDocument.activeLayer.visible;

};
 
function cTID(s) { return app.charIDToTypeID(s); };             // these lines make the codeso much cleaner, thanks xbytor!
function sTID(s) { return app.stringIDToTypeID(s); };



aquafat11

Script to create a SMART OBJECT from a GROUP

Post by aquafat11 »

note that I want to turn on the group "icon_large" BEFORE I convert it to a smart object. I've noticed that if I don't have the group visible before it's converted to a smart object it creates an empty smart object...
jamesrmac

Script to create a SMART OBJECT from a GROUP

Post by jamesrmac »

I think the line you added will turn the layer on if its off, but it will turn it off if it is on. Just use Code: Select allactiveDocument.activeLayer.visible = true; to make sure it is on no matter what.
Or you could get fancy and sayCode: Select allif(activeDocument.activeLayer.visible == false){activeDocument.activeLayer.visible =true;}
and that would only turn it on if it wasnt already.

Oh I just noticed you put that line after the return statement in a function, so it would never get played. you should move it before the return statement, or better yet, out of the function. This is how I would do it.
Code: Select allif(layerExist("icon_large")){
    activeDocument.activeLayer.visible = true;
    executeAction(sTID('newPlacedLayer'), undefined, DialogModes.NO);
   }

function layerExist(lyrName){       //checks to see if layer exist and makes it active if it does
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putName( cTID( "Lyr " ), lyrName);
    desc.putReference( cTID( "null" ), ref );
    desc.putBoolean( cTID( "MkVs" ), false );
    try{ executeAction( cTID( "slct" ), desc, DialogModes.NO ); }catch(e){return false;}
    return true;
};
 
function cTID(s) { return app.charIDToTypeID(s); };             // these lines make the codeso much cleaner, thanks xbytor!
function sTID(s) { return app.stringIDToTypeID(s); };
Mike Hale

Script to create a SMART OBJECT from a GROUP

Post by Mike Hale »

If your layerSet is a top level set you could use
Code: Select allapp.activeDocument.activeLayer = app.activeDocument.layers.getByName('icon_large');to select the layerSet.

But if you want to use the action manager code to select the layer, you can use that same code to make sure it is visible when selected. Just change this lineCode: Select alldesc.putBoolean( cTID( "MkVs" ), false );toCode: Select alldesc.putBoolean( cTID( "MkVs" ), true );The charID 'MkVs' means 'make visible'.

Note that using either the Object model or action manager version, it will select the top most layer with that name if there is more than one layer with that name. Using the Object model you can be very specific about which layerSet is selected. For example you could do something likeCode: Select allapp.activeDocument.activeLayer = app.activeDocument.layerSets.getByName('UI Elements').layerSets.getByName('icon_large');// select icon_large set in the UI Elements setorCode: Select allapp.activeDocument.activeLayer = app.activeDocument.layerSets[2].layerSets.getByName('icon_large');// select icon_large in the third top level layerSet