-
-
Save laryn/0a1f6bf0dab5b713395a835f9bfa805c to your computer and use it in GitHub Desktop.
// Replace SmartObject’s Content and Save as JPG | |
// 2017, use it at your own risk | |
// Via @Circle B: https://graphicdesign.stackexchange.com/questions/92796/replacing-a-smart-object-in-bulk-with-photoshops-variable-data-or-scripts/93359 | |
// JPG code from here: https://forums.adobe.com/thread/737789 | |
#target photoshop | |
if (app.documents.length > 0) { | |
var myDocument = app.activeDocument; | |
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]; | |
var thePath = myDocument.path; | |
var theLayer = myDocument.activeLayer; | |
// JPG Options; | |
jpgSaveOptions = new JPEGSaveOptions(); | |
jpgSaveOptions.embedColorProfile = true; | |
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; | |
jpgSaveOptions.matte = MatteType.NONE; | |
jpgSaveOptions.quality = 8; | |
// Check if layer is SmartObject; | |
if (theLayer.kind != "LayerKind.SMARTOBJECT") { | |
alert("selected layer is not a smart object") | |
} else { | |
// Select Files; | |
if ($.os.search(/windows/i) != -1) { | |
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true) | |
} else { | |
var theFiles = File.openDialog("please select files", getFiles, true) | |
}; | |
if (theFiles) { | |
for (var m = 0; m < theFiles.length; m++) { | |
// Replace SmartObject | |
theLayer = replaceContents(theFiles[m], theLayer); | |
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1]; | |
// Save JPG. If you have need of saving different artboards into separate files, | |
// see https://gist.github.com/laryn/0a1f6bf0dab5b713395a835f9bfa805c#gistcomment-3996733 | |
myDocument.saveAs((new File(thePath + "/" + theName + "_" + theNewName + ".jpg")), jpgSaveOptions, true,Extension.LOWERCASE); | |
} | |
} | |
} | |
}; | |
// Get PSDs, TIFs and JPGs from files | |
function getFiles(theFile) { | |
if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") { | |
return true | |
}; | |
}; | |
// Replace SmartObject Contents | |
function replaceContents(newFile, theSO) { | |
app.activeDocument.activeLayer = theSO; | |
// ======================================================= | |
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents"); | |
var desc3 = new ActionDescriptor(); | |
var idnull = charIDToTypeID("null"); | |
desc3.putPath(idnull, new File(newFile)); | |
var idPgNm = charIDToTypeID("PgNm"); | |
desc3.putInteger(idPgNm, 1); | |
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO); | |
return app.activeDocument.activeLayer | |
}; |
It seems like the FileDropper web server is down for a week or more now. Is there another link that you can post? Cheers
Ive added to my own github. > https://github.com/schroef/Photoshop-Scripts#replace-smartobject-image
I added it quickly so not much info. Make sure you have the same structure as i show in that image, especially the naming. If this doesnt match, the script will return errors
How could I amend this to include linked smart objects nested within regular smart objects? Also is it possible to ignore smart objects with no link?
For nested smart objects inside a smartonbject it would need a extra loop and call the same function again.
What do you mean by smart objects without a link? Do you you mean those layers which are converted to smartobject and once being edited are a temp psb file?
Hi, I am new to using scripts and I tried this one, but for some reason it places image incorrectly, as if moves my original smart object. Any idea what I am setting up incorrectly?
here's link to example psd file https://drive.google.com/drive/folders/1JSJu0UH32buuP6Kh7b0x9dkpTOBmQpGU?usp=sharing
@dbashbusiness
I just tried it and it works fine for me with the send link. PS is it perhaps you used a different image. I see a image with green leaves and not pink leaves.
Have you tried this version?
https://github.com/schroef/Photoshop-Scripts#replace-smartobject-image
EDIT
okay after checking one time more. I noticed this seems to be happening because your initial smartobject is skewed or transformed into perspective. Once i test it using a not deformed image it looks better. But still doesnt fit properly because the mask is skewed. Perhaps it would need more work. Im not sure if we can also apply proper skewing or perspective transformation using script.
Hi,
I am trying a different approach :D
Here's a different mockup I downloaded. I tried adjusting the layers to match your requirements - changed names on smart object and existing mask layer, ungrouped the folder.
However, now the image is too zoomed in. Is there something I do incorrectly with the layers? I am not quite sure how to create correct mask layer on smart object myself..
here's the original mockup as well as the one edited by me, if you would have time to review.
THANK YOU!
https://drive.google.com/drive/folders/1Vf9VRgtPEw93HCdHoiRCTfCSNcAaltD_?usp=sharing
Hi, I am trying a different approach :D Here's a different mockup I downloaded. I tried adjusting the layers to match your requirements - changed names on smart object and existing mask layer, ungrouped the folder. However, now the image is too zoomed in. Is there something I do incorrectly with the layers? I am not quite sure how to create correct mask layer on smart object myself.. here's the original mockup as well as the one edited by me, if you would have time to review.
THANK YOU!
https://drive.google.com/drive/folders/1Vf9VRgtPEw93HCdHoiRCTfCSNcAaltD_?usp=sharing
You need to use the exaxt same layer setup as i showed earlier. I wont work otherwise.
This is the correct order
PS i found a different script and have altered it. That script now works on your earlier example and also does keep the correct perspective. Ill post a link tomorrow
Thank you so much! I will wait for the link.
Would you be able to guide me through creating your layers?
I'm not sure what exactly is done to get this masked layer.
@dbashbusiness
Sorry for the long wait. Ill try to show you the workflow which is needed for this other version of the script.
Relink SmartObjects - v2
- Open the PSD file containing the artwork
- Selected linked SmartObject layer or place (link) a new SmartObject.
- Now convert this layer (SmartObject) again to a SmartObject. The earlier layer/SmartObject will become nested in the other. Name this layer "REPLACE DESIGN"
You can copy this layer and move it to the top and set visibility OFF for easy access. - Now apply your perspective transformation on the layer/smartobject in the main document.
- Optionally you can add a mask and transform the layer so its a bit bigger. This will fix the issue if a new linked image has a different ratio causing blank areas to appear. See images below for more clear explanation of why we want to do that.
I think im going to merge these 2 versions of the scripts into 1 main script. Then when with a dialog a user can pick either method. Thats way we can keep it as 1 script
This is the layer order you need to have now
Issue which shows when you relink images with different image ratio.
Relink SmartObjects keeping Perspective
@milqnomadu have you already tried the version which I linked above?
They do exactly what you ask for, minus that prompt at the end. That's easy to add yourself. You can also very easily adjust the export settings to your need.
There are 2 versions now of this export file. One does not adjust the image of it's skewed or perspective transformer. However it will fit your images inside a mask. Proportions will matter depending on your mask. Meaning borders can appear showing the image below if a new linked image has different proportions vs the mash proportions.
The other version will linked images inside a transformed smartobject and will keep that transformation. That is something the original script here did not do.
I have an issue with the code, I will link the detailed explaination because I cannot add images here:
@schroef I tried the version above, however the output is the same I describe there. I don't really know how to cope with it.
@milqnomadu
If you go to my GitHub link, I added it a couple posts above, it will show a layout in the layer panel. This is need to make it scale down the image to the mask dimensions. This will fix the issue I saw in that post you linked. I I understand correctly, your new linked image "design" is to big for the mockup, is that correct?
Edit
Here's the link to my repo
https://github.com/schroef/Photoshop-Scripts
Notice the layer panel and the order, this is very important for the resizing to work properly
Ive been working on a new version of this script. After someone contacted me about a small issue with the current script. The original script did not resize the image. By adding the code from @magicsun and adding a mask, that work. But the image aspect ratio can cause an issue in that version. If the new images don't fit in terms of the image ratio, you would get blank spots. Ive also now have a version which replaces the content inside the linked SmartObject. This is a different approach and in some cases solves the issue.
I've also found a function on Adobe Community by Stephen A Marsh which allows the user to save as WebP.
I'm stilling testing out if this script can be one version or perhaps we keep both methods. Anyway, im working on adding a dialog window. The user can define settings and placement of the new image. This is for the version which edit the content of the SmartObject. The script also uses ObjectToDescriptor, this means when you reload the script it restores all last settings.
This is how it looks now, it already works with hard-coded save to WebP. Im now in the process of adding all other formats; BMP,JPG,PDF,PSD,TARGA,TIFF,WEBP,PNG-8,PNG-24.
To replace a smart object within a PSD with selected image(s) and save the result as a JPG using a Photoshop script, you can refer to the detailed instructions provided in the article on BulkMockup. The article covers the use of scripts to automate the process and includes steps for replacing smart objects in bulk. The script mentioned in the article applies transformations to the smart objects and ensures that the changes are saved as JPG files.
@shohidur333
My new versions actually does this, I've implemented 2 version now. One which edit the link itself, the other will edit the content inside the smartobject. The latter is slower however to the workflow need. But it's still a fraction of doing this all manually .
It's nice you have such expanded tutorials and writing. But it's a commercial product. $15 per month. Kinda steep price if you can get ir for free here
The script is indeed effective, but it does have limitations when dealing with various template and art file scenarios. For those seeking a more comprehensive solution, the Bulk Mockup plugin (a paid tool) offers enhanced functionality:
- Multiple smart object layer replacement
- Design file alignment using a 3x3 matrix (9 anchor points)
- Nested smart object layer replacement
- Animated and video file export capabilities
- Customizable file naming for exports
- Flexible organization of exported files into folders and subfolders
While the original script works well for basic needs, Bulk Mockup addresses its limitations and provides greater versatility for complex projects.
But... its free and not hidden behind a pay-wall! Even weirder, a monthly subscription for a script?!?!? mind-bogling!
@stahlsparre
If you check the last nk i added to the file dropper website couple comments back. You'll find an update version of this script. This will adjust the new added image to match the correct size.
You new image has a bigger resolution than when that smart object initially was created
See this post how the new version works
https://gist.github.com/laryn/0a1f6bf0dab5b713395a835f9bfa805c?permalink_comment_id=4334299#gistcomment-4334299
That WeTransfer like NJ doesn't work anymore, a couple posts below is a file dropper link. That site is safe, chrome Warns of it but it's okay