Smart Object Replacement, Saving,Naming

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

9lastic
Posts: 2
Joined: Thu Aug 17, 2017 4:37 pm

Smart Object Replacement, Saving,Naming

Post by 9lastic »

Hey guys!

I'm absolutely new to PS scripting.. I mean never really done any scripting more than actions in PS :P

So I'd be looking for some direction to scripts I could potentially piece together with just my own logical thinking rather than programming as such.
Or, if anyone might have something similar to what I need on hand, that I can modify a little.


So here is a deal:
I have a PSD file, with a linked smart object.

I need the script to:
1. relink smart object to another file in folder
2. save for web with specific preset in another folder
3. name the .jpg file that is being saved, after the name of linked smart object
4. repeat for next file in the mentioned folder in point 1

Anyone? :)
pixxxelschubser
Posts: 26
Joined: Mon Aug 01, 2016 8:59 pm

Re: Smart Object Replacement, Saving,Naming

Post by pixxxelschubser »

9lastic
Posts: 2
Joined: Thu Aug 17, 2017 4:37 pm

Re: Smart Object Replacement, Saving,Naming

Post by 9lastic »

Hahahaha, that's brilliant!
Someone I was asking to get this done, must have posted it... fecking internet...
pixxxelschubser
Posts: 26
Joined: Mon Aug 01, 2016 8:59 pm

Re: Smart Object Replacement, Saving,Naming

Post by pixxxelschubser »

The crossposting was removed. What a shame. It was the better one. With an example file.
User avatar
jaydoubleyou80
Posts: 20
Joined: Mon Oct 17, 2016 1:41 pm
Location: USA

Re: Smart Object Replacement, Saving,Naming

Post by jaydoubleyou80 »

Here's something I whipped up. It doesn't pay attention to size, or location, so if the things you are replacing the smart object with are of differing sizes, that would need to be addressed. Also, grab this PDF and you can look at what can be set for save for web options:
http://wwwimages.adobe.com/content/dam/ ... pt-ref.pdf

I don't claim to be great at this, so there may be a better way to do it. All I can say is that when I tested it with conditions that I think will closely line up with your own, it worked for me.

One other thing to be aware of, the name of the saved file is whatever the smart object replacement file is named. If they aren't unique, you'll run into an issue.

Code: Select all

#target Photoshop

var docRef=app.activeDocument
var smartObject

saveForWebOpts=new ExportOptionsSaveForWeb//check PDF mentioned in post for what you can set here

function replaceSmartObject(){
var selectedFolder= Folder.selectDialog('Please select the folder containing files for smart object replacement:', Folder('~/Desktop'))//loads your desktop, asks you to point out the files you want to be used as smart objects
var outputFolder= Folder.selectDialog('Please select the folder where files should be saved', Folder('~/Desktop'))//loads your desktop, askss you to select a location so save the files
var fileList = selectedFolder.getFiles("*.jpg");//limit file types used by putting them in the ()
for (b=0;b<fileList.length;b++){
var saveName=fileList[b].name
for (a=0;a<docRef.artLayers.length;a++){//this only looks at top level layers, if you have groups that need to be looked in, they have to be gone through differently
if (docRef.artLayers[a].kind==LayerKind.SMARTOBJECT){
smartObject=docRef.artLayers[a]
}
docRef.activeLayer=smartObject//makes your active layer the smart object
replaceContents ()
function replaceContents(smartObject){//script listener code to replace the contents of the smart object, as I don't think this is accessible without it
var idplacedLayerReplaceContents = stringIDToTypeID( "placedLayerReplaceContents" );
var desc403 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc403.putPath( idnull, new File( fileList[b] ) );
executeAction( idplacedLayerReplaceContents, desc403, DialogModes.NO );
}
var saveFile=File(outputFolder + '/' + saveName)
docRef.exportDocument (saveFile, ExportType.SAVEFORWEB, saveForWebOpts)//this seems really slow, you may want to try using script listener to see if you get better results
}
}
}
replaceSmartObject()//run the script