Copy link to clipboard
Copied
I have a psd file with an embedded psd file. Using photoshop normally, I double on the layer icon to edit the contents (or choose "edit contents" from the dropdown). I can not seem to find out how to open/edit it with extendscript/javascript. It is a legend that we use on our plans and would like to add content via scripting but need to find out how to edit the contents first. Note: this is not a linked file externally, it is embedded.
Thanks in advance.
One way:
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
Copy link to clipboard
Copied
This is the line causing that error
if (current == app.activeDocument) throw theLayer + " Smart Object Did not Open";
Changing the file to .js vs .jsx doesnt help, it still returns that error. Its asif it doesnt understand throw statement.
On inspecting the throw error. I think your using it wrong, i you look at this example. Throw needs to be used with try and catch. Like you did a bit up in the function.
I dont think the try and catch make is very useful, its just adds more lines of code. Simply using if and then alert is about the same right?
i corrected the code and now the script does run. But it opens the wrong file.
Now with fuction working properly nothing is actually opened. As a check i added alert to see what layer is selected, however nothing is happening. I believe your function should alert if the smartobject cant be opened but it doesnt do that
////// open smart object //////
function openSmartObject(theLayer) {
current = app.activeDocument;
alert(theLayer)
if (theLayer.kind == "LayerKind.SMARTOBJECT") {
runMenuItem(stringIDToTypeID('placedLayerEditContents'));
if (current == app.activeDocument) {
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
r.putIdentifier(stringIDToTypeID("layer"), theLayer.id);
var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));
} catch (e) {
throw theLayer + " Smart Object Did not Open";
}
var workFile = new File(Folder.temp + "/" + name); // May work for both Windows and Mac
if (workFile.exists) app.open(File(workFile));
try {
if (current == app.activeDocument) {}
} catch(e) {
throw theLayer + " Smart Object Did not Open";
}
}
} else {
alert(theLayer +" is not a smartobject");
}
return app.activeDocument
};
Copy link to clipboard
Copied
Ow i see why its not working. Though the function should work it only works when the smartobject is openen alreadt otherwise that tempfile is not there and it passes resulting in nothing to happen. I check by forcing the file to be opened. Ill check what happen if i run the runMenuItem, ps shouldnt that be "app.runMenuItem", than add a wait or app.refresh then check if the file is there
Copy link to clipboard
Copied
Perhaps its just this photoshop version. I just noticed that using the button "Edit contents" in the properties panel actually doesnt do anything, thats kinda weird. The shortcut for it does work. That probably also why the runMenuItem doesnt do anything. Very strange, feels very buggy.
Then after closing and opening the file the button does work again, really strange behavior!
Copy link to clipboard
Copied
Can you please post a PSD template you are having problem with. I trying to find out what kind of objects do not open with editobject synchronously. So far the only template object that does not Open for me synchronous with editobject in my open smart object function is in a template "Wall Interiors.psd" someone posted in this forum. The Temp work file created by Photoshop for editObject is a PSB file that has two layers. The top layer is a vector smart object layer an AI file the layer has a layer style the color overlay effect. Basically ii is an AI text layer with a style added.
%PDF-1.6
%âãÏÓ
1 0 obj
<</Metadata 2 0 R/OCProperties<</D<</ON[5 0 R]/Order 6 0 R/RBGroups[]>>/OCGs[5 0 R]>>/Pages 3 0 R/Type/Catalog>>
endobj
2 0 obj
<</Length 3397/Subtype/XML/Type/Metadata>>stream
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 6.0-c002 79.164460, 2020/05/12-16:04:17 ">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/"
xmlns:illustrator="http://ns.adobe.com/illustrator/1.0/"
xmlns:xmp="http://ns.adobe.com/xap/1.0/"
xmlns:pdf="http://ns.adobe.com/pdf/1.3/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<xmpMM:RenditionClass>proof:pdf</xmpMM:RenditionClass>
<xmpMM:DocumentID>uuid:7d9c780a-68e8-49b8-8ac2-57ee75c99122</xmpMM:DocumentID>
<xmpMM:InstanceID>uuid:3cfe8f29-f6e4-4e60-82d1-caae093a436b</xmpMM:InstanceID>
<illustrator:CreatorSubTool>Adobe Illustrator</illustrator:CreatorSubTool>
<xmp:CreateDate>2020-11-23T13:38:29+08:00</xmp:CreateDate>
<xmp:CreatorTool>Adobe Illustrator 24.2 (Windows)</xmp:CreatorTool>
<xmp:ModifyDate>2020-11-23T13:38:29+07:00</xmp:ModifyDate>
<xmp:MetadataDate>2020-11-23T13:38:29+07:00</xmp:MetadataDate>
<pdf:Producer>Adobe PDF library 15.00</pdf:Producer>
<dc:format>application/pdf</dc:format>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
However, I see PDF in the actual Object file at the top of the vector smart object file looking at the .ai file via a text editor. If I open the vector smart object via a text editor. I do not install AI. I have set extension .ai to open with Notepad++ on my windows machine.
The bottom Layer in the work document is a rectangular Shape layer the Object size that has a solid fill layer. color.
However, when I create a smars object layer with a two layet object like that the Object opens with editObject synchronously. Ther is a document switch.
When I edit the object and have Photoshop update the template the Object that failed to open with editObject When my script uses editObject for the next replacement image the updated smarts object layer the object work file open with editObject in the Open Smart object function. I do not need to add the open command for the work file to speed processing up.
Normally editObject in my open smart object function opens the object work file into a photoshop as work document for the object. There seem toe be some exceptions. I'm trying to determine why some object do not open synchronously for Mockup scripts next steps are designed to update the smart object work file. . For some reason the first time some smart object layers have editContent used on them do not open in Photoshop synchronously.
Copy link to clipboard
Copied
hey sorry i forgot to post my link... oopps https://we.tl/t-amPqhIM4pC
Well i done more testing, still nothing. Ive now tested multiple variations of pauzing the script before running the place command. still the place-command is done before the smartobject is open. Ive tried 2 different timer variants, actually 3. One is $.sleep(500) which is sleep for 500 milliseconds, still placement is done before the smartobject is opened. I also tried using a timer function from Mike Hall or Tom Ruark, again the file is placed before the smartobject is opened.
I even use 5 seconds for a pause, still it will not work properly and i really dont understand why.
Im reading this thread now at ps-script, the user also needs something i believe to sleep for a while
#target photoshop
var docRef = app.activeDocument;
var actLayer = docRef.activeLayer;
function cTID(s) {
return app.charIDToTypeID(s);
};
function sTID(s) {
return app.stringIDToTypeID(s);
};
function waitForRedraw() {
var eventWait = charIDToTypeID("Wait");
var enumRedrawComplete = charIDToTypeID("RdCm");
var typeState = charIDToTypeID("Stte");
var keyState = charIDToTypeID("Stte");
var desc = new ActionDescriptor();
desc.putEnumerated(keyState, typeState, enumRedrawComplete);
executeAction(eventWait, desc, DialogModes.NO);
return;
}
////// open smart object //////
function openSmartObject(theLayer) {
current = app.activeDocument;
if (theLayer.kind == "LayerKind.SMARTOBJECT") {
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
waitForRedraw();
// alert(current)
if (current == app.activeDocument) {
// try {
// var r = new ActionReference();
// r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
// r.putIdentifier(stringIDToTypeID("layer"), theLayer.id);
// var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));
// } catch (e) {
// throw theLayer + " Smart Object Did not Open";
// }
app.activeDocument[1];
// var workFile = new File(Folder.temp + "/" + name); // May work for both Windows and Mac
// if (workFile.exists) app.open(File(workFile));
// // else app.open(File(workFile));
// try {
// if (current == app.activeDocument) {}
// } catch(e) {
// throw theLayer + " Smart Object Did not Open";
// }
}
} else {
alert(theLayer +" is not a smartobject");
}
// return app.activeDocument
};
// Check if object is PS
// R-Bin - JJ Mack
// https://community.adobe.com/t5/photoshop/extendscript-photoshop-edit-contents-smart-layer/m-p/10706542#M273949
function objectIsPsObject(SOlayer) {
//Thanks to r-bin
var ext = smartobject_file_ext(SOlayer);
var rc = true;
switch (ext)
{
case "nef":
case "cr3":
case "cr2":
case "crw":
case "raf":
case "orf":
case "mrw":
case "dcr":
case "mos":
case "raw":
case "pef":
case "srf":
case "dng":
case "x3f":
case "erf":
case "sr2":
case "kdc":
case "mfw":
case "mef":
case "arw":
case "nrw":
case "rw2":
case "rwl":
case "iiq":
case "3fr":
case "fff":
case "srw":
case "ai":
case "svg":
case "pdf":
case "esp":
rc = false;
break;
case "error":
rc = false;
break;
default:
rc = true;
break;
}
return rc;
}
function smartobject_file_ext(layer) {
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
r.putIdentifier(stringIDToTypeID("layer"), layer.id);
var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));
var n = name.lastIndexOf(".");
if (n < 0) return "";
return name.substr(n+1).toLowerCase();
}
catch (e) { return "error"; }
}
function placeImage(newFile){
// app.refresh(); // doesnt work
// app.redraw();// doesnt work
// waitMoment();
// $.sleep(5000);
// Switch document
// var desc111 = new ActionDescriptor();
// var ref13 = new ActionReference();
// ref13.putOffset( cTID('Dcmn'), 1 );
// desc111.putReference( cTID('null'), ref13 );
// desc111.putInteger( cTID('DocI'), 344 );
// executeAction( cTID('slct'), desc111, DialogModes.NO );
// Place images
// alert(newFile)
// alert(app.activeDocument)
app.activeDocument = [1];
var desc174 = new ActionDescriptor();
desc174.putInteger( cTID('Idnt'), 885 );
desc174.putPath( cTID('null'), new File(newFile));
desc174.putBoolean( cTID('Lnkd'), true );
desc174.putEnumerated( cTID('FTcs'), cTID('QCSt'), cTID('Qcsa') );
var desc175 = new ActionDescriptor();
desc175.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), 0.000000 );
desc175.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), 0.000000 );
desc174.putObject( cTID('Ofst'), cTID('Ofst'), desc175 );
desc174.putUnitDouble( cTID('Wdth'), cTID('#Prc'), 234.782609 ); // i know the scale > this will make the image fit the SO file
desc174.putUnitDouble( cTID('Hght'), cTID('#Prc'), 234.697509 ); // i know the scale > this will make the image fit the SO file
executeAction( cTID('Plc '), desc174, DialogModes.NO );
// alert(app.activeDocument)
// alert(app.activeDocument)
// app.activeDocument.close(SaveOptions.SAVECHANGES);
}
function saveTmpDoc() {
// alert(tmpDoc)
var tmpDoc = app.activeDocument
var tmpPath = tmpDoc.path;
var tmpSave = tmpPath + osPath + tmpDoc.name; //tmpDoc + '/' + tmpDoc.name;
// alert(tmpSave)
// alert(osPath)
// alert(tmpSave)
var saveTmp = new ActionDescriptor();
saveTmp.putPath( cTID('In '), new File( tmpSave ) );
saveTmp.putInteger( cTID('DocI'), 587 );
saveTmp.putEnumerated( sTID('saveStage'), sTID('saveStageType'), sTID('saveSucceeded') );
executeAction( cTID('save'), saveTmp, DialogModes.NO );
return true
};
// Object: Timer
// Usage: Time how long things take or delay script execution
// Input: <none>
// Return: Timer object
// Example:
//
// var a = new Timer();
// for (var i = 0; i < 2; i++)
// a.pause(3.33333);
// a.getElapsed();
// jeff tranberry
///////////////////////////////////////////////////////////////////////////////
function Timer() {
// member properties
this.startTime = new Date();
this.endTime = new Date();
// member methods
// reset the start time to now
this.start = function () { this.startTime = new Date(); }
// reset the end time to now
this.stop = function () { this.endTime = new Date(); }
// get the difference in milliseconds between start and stop
this.getTime = function () { return (this.endTime.getTime() - this.startTime.getTime()) / 1000; }
// get the current elapsed time from start to now, this sets the endTime
this.getElapsed = function () { this.endTime = new Date(); return this.getTime(); }
// pause for this many seconds
this.pause = function ( inSeconds ) {
var t = 0;
var s = new Date();
while( t < inSeconds ) {
t = (new Date().getTime() - s.getTime()) / 1000;
}
}
}
function exportMockupDesigns() {
// Get OS
// fromt "_LastLogEntry.jsx"
isWindows = function() {
return $.os.match(/windows/i);
};
isMac = function() {
return !isWindows();
};
var osPath = (isMac() ? '/' : "/"); // resolve windows vs osx paths
// alert(isMac())
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;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 8;
// check if layer is smart object;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else if (objectIsPsObject(actLayer) != true) {
alert("The SmartOjbect is not a PSB or PSD file")
return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script
} else {
// select files;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg;*.jpeg;", true)
} else {
var theFiles = File.openDialog("please select files", getFiles, true)
};
// select Folder;
// var theFolder = Folder.selectDialog("select folder");
if (theFiles) {
// var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|png)$/i);
// Select destination folder
var destinationFolder = Folder.selectDialog("Please select a destination folder");
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// replace smart object;
openSmartObject(theLayer);
// app.refresh();
// alert(editSO)
// waitMoment()
// $.sleep(500)
// waitForRedraw();// notworking
var pauseTime = new Timer();
pauseTime.pause(5)
// pause.Timer(.5);
// setTimeout( placeImage(theFiles[m]), 500); doesnt excist in extendedscript
placeImage(theFiles[m]);
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
//save jpg;
myDocument.saveAs((new File(destinationFolder.toString() + "/" + theName + "_" + theNewName + ".jpg")), jpgopts, true);
// }
}
};
}
};
////// get psds, tifs and jpgs from files //////
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|tif|png|jpg|jpeg)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
};
};
exportMockupDesigns();
Copy link to clipboard
Copied
hi sorry i had something uploading but kinda forgot to paste the link here. Heres a wetransfer with the mockup file and some test files i trying to placeinside the red layer. https://we.tl/t-amPqhIM4pC
This is second time i comment this because somehow my other response didnt get posted and neither did i get an error while posting.
I also noticed that weird behavior, thats what im experiencing. Ive tried a couple timer function now and it should wait till it executes, well it actually does it. but the file is just not opened in time. everytime it places the image in the wrong document. Meaning the place command is already run when the opening is still in progress, pausing the script doesnt work. Ive tried many variations of this.
Ive tried another timer function i found on ps-script, i already have version of that from Tom Ruark, this one also has a pause function. But still not the result im aiming for.
PS i altered your edit smartobject, it wasnt working as well due to this. because of the script is to fast, it was actually opening 2 instances of the same document causing the linking of temp psb not to work correctly.
Perhaps i need to test what happens if that smartobject layer isnt a tmp file but a real PSD file with a "physical" address aside of the mockup. You also stated i believe that some of your templates where acting weird.
#target photoshop
var docRef = app.activeDocument;
var actLayer = docRef.activeLayer;
function cTID(s) {
return app.charIDToTypeID(s);
};
function sTID(s) {
return app.stringIDToTypeID(s);
};
function waitForRedraw() {
var eventWait = charIDToTypeID("Wait");
var enumRedrawComplete = charIDToTypeID("RdCm");
var typeState = charIDToTypeID("Stte");
var keyState = charIDToTypeID("Stte");
var desc = new ActionDescriptor();
desc.putEnumerated(keyState, typeState, enumRedrawComplete);
executeAction(eventWait, desc, DialogModes.NO);
return;
}
////// open smart object //////
function openSmartObject(theLayer) {
current = app.activeDocument;
if (theLayer.kind == "LayerKind.SMARTOBJECT") {
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
waitForRedraw();
// alert(current)
if (current == app.activeDocument) {
// try {
// var r = new ActionReference();
// r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
// r.putIdentifier(stringIDToTypeID("layer"), theLayer.id);
// var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));
// } catch (e) {
// throw theLayer + " Smart Object Did not Open";
// }
app.activeDocument[1];
// var workFile = new File(Folder.temp + "/" + name); // May work for both Windows and Mac
// if (workFile.exists) app.open(File(workFile));
// // else app.open(File(workFile));
// try {
// if (current == app.activeDocument) {}
// } catch(e) {
// throw theLayer + " Smart Object Did not Open";
// }
}
} else {
alert(theLayer +" is not a smartobject");
}
// return app.activeDocument
};
// Check if object is PS
// R-Bin - JJ Mack
// https://community.adobe.com/t5/photoshop/extendscript-photoshop-edit-contents-smart-layer/m-p/10706542#M273949
function objectIsPsObject(SOlayer) {
//Thanks to r-bin
var ext = smartobject_file_ext(SOlayer);
var rc = true;
switch (ext)
{
case "nef":
case "cr3":
case "cr2":
case "crw":
case "raf":
case "orf":
case "mrw":
case "dcr":
case "mos":
case "raw":
case "pef":
case "srf":
case "dng":
case "x3f":
case "erf":
case "sr2":
case "kdc":
case "mfw":
case "mef":
case "arw":
case "nrw":
case "rw2":
case "rwl":
case "iiq":
case "3fr":
case "fff":
case "srw":
case "ai":
case "svg":
case "pdf":
case "esp":
rc = false;
break;
case "error":
rc = false;
break;
default:
rc = true;
break;
}
return rc;
}
function smartobject_file_ext(layer) {
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
r.putIdentifier(stringIDToTypeID("layer"), layer.id);
var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));
var n = name.lastIndexOf(".");
if (n < 0) return "";
return name.substr(n+1).toLowerCase();
}
catch (e) { return "error"; }
}
function placeImage(newFile){
// app.refresh(); // doesnt work
// app.redraw();// doesnt work
// waitMoment();
// $.sleep(5000);
// Switch document
// var desc111 = new ActionDescriptor();
// var ref13 = new ActionReference();
// ref13.putOffset( cTID('Dcmn'), 1 );
// desc111.putReference( cTID('null'), ref13 );
// desc111.putInteger( cTID('DocI'), 344 );
// executeAction( cTID('slct'), desc111, DialogModes.NO );
// Place images
// alert(newFile)
// alert(app.activeDocument)
app.activeDocument = [1];
var desc174 = new ActionDescriptor();
desc174.putInteger( cTID('Idnt'), 885 );
desc174.putPath( cTID('null'), new File(newFile));
desc174.putBoolean( cTID('Lnkd'), true );
desc174.putEnumerated( cTID('FTcs'), cTID('QCSt'), cTID('Qcsa') );
var desc175 = new ActionDescriptor();
desc175.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), 0.000000 );
desc175.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), 0.000000 );
desc174.putObject( cTID('Ofst'), cTID('Ofst'), desc175 );
desc174.putUnitDouble( cTID('Wdth'), cTID('#Prc'), 234.782609 ); // i know the scale > this will make the image fit the SO file
desc174.putUnitDouble( cTID('Hght'), cTID('#Prc'), 234.697509 ); // i know the scale > this will make the image fit the SO file
executeAction( cTID('Plc '), desc174, DialogModes.NO );
// alert(app.activeDocument)
// alert(app.activeDocument)
// app.activeDocument.close(SaveOptions.SAVECHANGES);
}
function saveTmpDoc() {
// alert(tmpDoc)
var tmpDoc = app.activeDocument
var tmpPath = tmpDoc.path;
var tmpSave = tmpPath + osPath + tmpDoc.name; //tmpDoc + '/' + tmpDoc.name;
// alert(tmpSave)
// alert(osPath)
// alert(tmpSave)
var saveTmp = new ActionDescriptor();
saveTmp.putPath( cTID('In '), new File( tmpSave ) );
saveTmp.putInteger( cTID('DocI'), 587 );
saveTmp.putEnumerated( sTID('saveStage'), sTID('saveStageType'), sTID('saveSucceeded') );
executeAction( cTID('save'), saveTmp, DialogModes.NO );
return true
};
// Object: Timer
// Usage: Time how long things take or delay script execution
// Input: <none>
// Return: Timer object
// Example:
//
// var a = new Timer();
// for (var i = 0; i < 2; i++)
// a.pause(3.33333);
// a.getElapsed();
// jeff tranberry
///////////////////////////////////////////////////////////////////////////////
function Timer() {
// member properties
this.startTime = new Date();
this.endTime = new Date();
// member methods
// reset the start time to now
this.start = function () { this.startTime = new Date(); }
// reset the end time to now
this.stop = function () { this.endTime = new Date(); }
// get the difference in milliseconds between start and stop
this.getTime = function () { return (this.endTime.getTime() - this.startTime.getTime()) / 1000; }
// get the current elapsed time from start to now, this sets the endTime
this.getElapsed = function () { this.endTime = new Date(); return this.getTime(); }
// pause for this many seconds
this.pause = function ( inSeconds ) {
var t = 0;
var s = new Date();
while( t < inSeconds ) {
t = (new Date().getTime() - s.getTime()) / 1000;
}
}
}
function exportMockupDesigns() {
// Get OS
// fromt "_LastLogEntry.jsx"
isWindows = function() {
return $.os.match(/windows/i);
};
isMac = function() {
return !isWindows();
};
var osPath = (isMac() ? '/' : "/"); // resolve windows vs osx paths
// alert(isMac())
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;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 8;
// check if layer is smart object;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else if (objectIsPsObject(actLayer) != true) {
alert("The SmartOjbect is not a PSB or PSD file")
return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script
} else {
// select files;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg;*.jpeg;", true)
} else {
var theFiles = File.openDialog("please select files", getFiles, true)
};
// select Folder;
// var theFolder = Folder.selectDialog("select folder");
if (theFiles) {
// var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|png)$/i);
// Select destination folder
var destinationFolder = Folder.selectDialog("Please select a destination folder");
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// replace smart object;
openSmartObject(theLayer);
// app.refresh();
// alert(editSO)
// $.sleep(500)
// waitForRedraw();// notworking
var pauseTime = new Timer();
pauseTime.pause(.5);
waitMoment();
// pause.Timer(.5);
// setTimeout( placeImage(theFiles[m]), 500); doesnt excist in extendedscript
placeImage(theFiles[m]);
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
//save jpg;
myDocument.saveAs((new File(destinationFolder.toString() + "/" + theName + "_" + theNewName + ".jpg")), jpgopts, true);
// }
}
};
}
};
////// get psds, tifs and jpgs from files //////
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|tif|png|jpg|jpeg)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
};
};
exportMockupDesigns();
Copy link to clipboard
Copied
Ive now made a second version of this mockup file and then made that smartobject layer linked, the file is placed in the same folder. Still there is this delay because of the opening. I thought it was working just now but it doesnt
Copy link to clipboard
Copied
Wha not sure what going on, ive now responded twice and made full new responds and it will not post?!?!
Copy link to clipboard
Copied
i cant respond anymore?!? i have responded twice now and lost my complete message
Copy link to clipboard
Copied
Sorry my message didnt post twice, then i got some kind of ban and needed to wait a hour. Kinda stupid dumb it does a page refresh and not post the message. Twice did i made a long new post, both times lost 😞 This forum still isnt working really well i guess.
Anyways here's the link. https://we.tl/t-amPqhIM4pC
I had it ready forgot about, but than it also got delete twice because of the prior post issue
Copy link to clipboard
Copied
#target photoshop
var docRef = app.activeDocument;
var actLayer = docRef.activeLayer;
function cTID(s) {
return app.charIDToTypeID(s);
};
function sTID(s) {
return app.stringIDToTypeID(s);
};
function waitForRedraw() {
var eventWait = charIDToTypeID("Wait");
var enumRedrawComplete = charIDToTypeID("RdCm");
var typeState = charIDToTypeID("Stte");
var keyState = charIDToTypeID("Stte");
var desc = new ActionDescriptor();
desc.putEnumerated(keyState, typeState, enumRedrawComplete);
executeAction(eventWait, desc, DialogModes.NO);
return;
}
// Check if object is PS
// R-Bin - JJ Mack
function objectIsPsObject(SOlayer) {
//Thanks to r-bin
var ext = smartobject_file_ext(SOlayer);
var rc = true;
switch (ext)
{
case "nef":
case "cr3":
case "cr2":
case "crw":
case "raf":
case "orf":
case "mrw":
case "dcr":
case "mos":
case "raw":
case "pef":
case "srf":
case "dng":
case "x3f":
case "erf":
case "sr2":
case "kdc":
case "mfw":
case "mef":
case "arw":
case "nrw":
case "rw2":
case "rwl":
case "iiq":
case "3fr":
case "fff":
case "srw":
case "ai":
case "svg":
case "pdf":
case "esp":
rc = false;
break;
case "error":
rc = false;
break;
default:
rc = true;
break;
}
return rc;
}
function smartobject_file_ext(layer) {
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
r.putIdentifier(stringIDToTypeID("layer"), layer.id);
var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));
var n = name.lastIndexOf(".");
if (n < 0) return "";
return name.substr(n+1).toLowerCase();
}
catch (e) { return "error"; }
}
function placeImage(newFile){
// app.refresh(); // doesnt work
// app.redraw();// doesnt work
// waitMoment();
// $.sleep(5000);
// Switch document
// var desc111 = new ActionDescriptor();
// var ref13 = new ActionReference();
// ref13.putOffset( cTID('Dcmn'), 1 );
// desc111.putReference( cTID('null'), ref13 );
// desc111.putInteger( cTID('DocI'), 344 );
// executeAction( cTID('slct'), desc111, DialogModes.NO );
// Place images
// alert(newFile)
// alert(app.activeDocument)
// app.activeDocument = [1];
var desc174 = new ActionDescriptor();
desc174.putInteger( cTID('Idnt'), 885 );
desc174.putPath( cTID('null'), new File(newFile));
desc174.putBoolean( cTID('Lnkd'), true );
desc174.putEnumerated( cTID('FTcs'), cTID('QCSt'), cTID('Qcsa') );
var desc175 = new ActionDescriptor();
desc175.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), 0.000000 );
desc175.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), 0.000000 );
desc174.putObject( cTID('Ofst'), cTID('Ofst'), desc175 );
desc174.putUnitDouble( cTID('Wdth'), cTID('#Prc'), 234.782609 ); // i know the scale > this will make the image fit the SO file
desc174.putUnitDouble( cTID('Hght'), cTID('#Prc'), 234.697509 ); // i know the scale > this will make the image fit the SO file
executeAction( cTID('Plc '), desc174, DialogModes.NO );
// alert(app.activeDocument)
// alert(app.activeDocument)
// app.activeDocument.close(SaveOptions.SAVECHANGES);
}
function saveTmpDoc() {
// alert(tmpDoc)
var tmpDoc = app.activeDocument
var tmpPath = tmpDoc.path;
var tmpSave = tmpPath + osPath + tmpDoc.name; //tmpDoc + '/' + tmpDoc.name;
// alert(tmpSave)
// alert(osPath)
// alert(tmpSave)
var saveTmp = new ActionDescriptor();
saveTmp.putPath( cTID('In '), new File( tmpSave ) );
saveTmp.putInteger( cTID('DocI'), 587 );
saveTmp.putEnumerated( sTID('saveStage'), sTID('saveStageType'), sTID('saveSucceeded') );
executeAction( cTID('save'), saveTmp, DialogModes.NO );
return true
};
// Object: Timer
// Usage: Time how long things take or delay script execution
// Input: <none>
// Return: Timer object
// Example:
//
// var a = new Timer();
// for (var i = 0; i < 2; i++)
// a.pause(3.33333);
// a.getElapsed();
// jeff tranberry
///////////////////////////////////////////////////////////////////////////////
function Timer() {
// member properties
this.startTime = new Date();
this.endTime = new Date();
// member methods
// reset the start time to now
this.start = function () { this.startTime = new Date(); }
// reset the end time to now
this.stop = function () { this.endTime = new Date(); }
// get the difference in milliseconds between start and stop
this.getTime = function () { return (this.endTime.getTime() - this.startTime.getTime()) / 1000; }
// get the current elapsed time from start to now, this sets the endTime
this.getElapsed = function () { this.endTime = new Date(); return this.getTime(); }
// pause for this many seconds
this.pause = function ( inSeconds ) {
var t = 0;
var s = new Date();
while( t < inSeconds ) {
t = (new Date().getTime() - s.getTime()) / 1000;
}
}
}
function switchForward(){
// Switch document
var desc111 = new ActionDescriptor();
var ref13 = new ActionReference();
ref13.putOffset( cTID('Dcmn'), 1 );
desc111.putReference( cTID('null'), ref13 );
desc111.putInteger( cTID('DocI'), 344 );
executeAction( cTID('slct'), desc111, DialogModes.NO );
}
function switchBackward(){
// Switch document
var desc111 = new ActionDescriptor();
var ref13 = new ActionReference();
ref13.putOffset( cTID('Dcmn'), -1 );
desc111.putReference( cTID('null'), ref13 );
desc111.putInteger( cTID('DocI'), 344 );
executeAction( cTID('slct'), desc111, DialogModes.NO );
}
///////////////////////////////////////////////////////////////////////////////
// Function: checkPlacedLayers
// Usage: Count the comps' names collecting duplicates
// Input: collection of comps
// Return: object with names as keys and as values, an object with total count and an index (for incrementing during file naming)
// Source: Layer-Comps-To-Files.jsx
///////////////////////////////////////////////////////////////////////////////
function checkPlacedLayers(list,actLayer){
// var obj = {};
for(var i = 0; i<list.length; i++){
var name = list[i].name.match(/(.*)\.[^\.]+$/)[1];
// if(!actLayer.visible) alert(actLayer.name)
try {
// alert(name +" "+ actLayer.name)
if(name == actLayer.name){
// if(name == actLayer.name && name.visible){
// if(!actLayer.visible) alert(actLayer.visible)
// if(name in obj){
// alert("active layer can be taken out")
// var isVisible = desc.getBoolean(charIDToTypeID("Vsbl"));
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
desc.putReference( cTID('null'), ref );
var reff = new ActionReference();
reff.putProperty(app.charIDToTypeID("Prpr"), stringIDToTypeID("targetLayers"))
reff.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"))
// what do I want to do this this list? Define an description of an action.
var desclay = executeActionGet(reff);
desclay = desclay.getList(stringIDToTypeID("targetLayers"))
// alert(desclay);
lyrIndex = desclay.getReference(0).getIndex();
// alert(lyrIndex);
var lyrIndex;
try {
docRef.backgroundLayer // try to select a background layer, if I can then adjust the index counting. (Background layers change index counitng of all layers by 1)
lyrIndex = desclay.getReference(0).getIndex();
} catch (e) {
lyrIndex = desclay.getReference(0).getIndex() + 1;
}
// Added from getLayerInfo
var lyrRef = new ActionReference();
lyrRef.putIndex(charIDToTypeID("Lyr "), lyrIndex)
var lyrDesc = executeActionGet(lyrRef);
// alert(lyrDesc.getInteger(stringIDToTypeID("layerKind")))
var Id = lyrDesc.getInteger(stringIDToTypeID("layerID"));
// alert(Id)
var list33 = new ActionList();
list33.putInteger( 180 );
desc.putList( cTID('LyrI'), list33 );
executeAction( cTID('Dlt '), desc, DialogModes.NO );
break
}
} catch(e) {
alert(e)
break
}
}
// return obj;
}
function exportMockupDesigns() {
// Get OS
// fromt "_LastLogEntry.jsx"
isWindows = function() {
return $.os.match(/windows/i);
};
isMac = function() {
return !isWindows();
};
var osPath = (isMac() ? '/' : "/"); // resolve windows vs osx paths
// alert(isMac())
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;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 8;
// check if layer is smart object;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else if (objectIsPsObject(actLayer) != true) {
alert("The SmartOjbect is not a PSB or PSD file")
return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script
} else {
// select files;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg;*.jpeg;", true)
} else {
var theFiles = File.openDialog("please select files", getFiles, true)
};
// select Folder;
// var theFolder = Folder.selectDialog("select folder");
if (theFiles) {
// var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|png)$/i);
// Select destination folder
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
var destinationFolder = Folder.selectDialog("Please select a destination folder");
var totalTime = new Timer(); // Timer Tom Ruark // Source: Getter.jsx
function placeDesigns(){
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
// app.refresh();
// alert(editSO)
// $.sleep(500)
// waitForRedraw();
// var pauseTime = new Timer();
// pauseTime.pause(.5);
// Delete prior placed layer > script takes 6sec without also 6
var count = checkPlacedLayers(theFiles, app.activeDocument.activeLayer); // check names of files > we can compare and delete unused layers when smartobject is open
checkPlacedLayers(theFiles, app.activeDocument.activeLayer); // check names of files > we can compare and delete unused layers when smartobject is open
placeImage(theFiles[m]);
// app.activeDocument.close(SaveOptions.SAVECHANGES);
app.activeDocument.save(SaveOptions.SAVECHANGES);
switchBackward();
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
//save jpg;
myDocument.saveAs((new File(destinationFolder.toString() + "/" + theName + "_" + theNewName + ".jpg")), jpgopts, true);
switchForward();
}
}
app.activeDocument.suspendHistory("Script > Place Designs", 'placeDesigns()');
};
}
};
////// get psds, tifs and jpgs from files //////
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|tif|png|jpg|jpeg)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
};
// app.activeDocument.close(SaveOptions.DONTSAVECHANGES); // close smartobject again
alert("Script Time: " + totalTime.getElapsed())
};
app.activeDocument.suspendHistory("Script > Export Mockup Designs", 'exportMockupDesigns()');
// exportMockupDesigns();
I also get this forum erro sometimes. Not sure whats going on, after using preview. I notice that the placed script is being altered. I had links to other threads in there commented out, i do that so that people can see where i got the source from. But this forum seems to pasting in some kind of text from those threads?!
Copy link to clipboard
Copied
Can you please upload some your templates you are having problems with. I only have one template that has a Smart Object layer that object does not open immediately synchronously when my scripts opens smart object function uses
runMenuItem(stringIDToTypeID('placedLayerEditContents'));
I believe that template may have been create on a Mac. For on my pc I can not create a template like that one. Its smart object layer looks like it was created from two Photoshop layers. A large rectangle shape layer that is filled with a color. This shape layer was larger than the templates canvas size. Above that layer there is a Vector Smart Object Layer the same the size as the Shape layer and that layer has a layer style added. Those two layers were converted into a smart object layer which was then sized and position for the markup template lower layers composition. The smart object layer's object transform was adjusted to make the smart object fit on the template canvas, positioned over a picture frame and add a little bit of perspective.
When I replicate that Template on my Windows PC the replicated template's smart object opens immediately synchronously when my scripts opens smart object function uses
runMenuItem(stringIDToTypeID('placedLayerEditContents'));
I do not install AI so I cannot create the vector smart object layer. AI file. Here are the steps I used to replicate the template on my PC from template that hast the issue.
First I open the template that has the Problem "Wall Interiors.psd"
I duplicated the document to a new document with the name "Wall Interiors jjmack"
In "Wall Interiors jjmack" I set Guide lines to the bounds of the smart object layer then deleted the smart object layer.
In "Wall Interiors" I opened the smart object layer object and closed "Wall Interiors" template document.
The opened smart object work document "Rectangle 12.psb" has two layer the top layer name is Vector Smart Object has it has a Layer Style Color Overlay. The Bottom layer the Rectangle Shape Layer with a solid fill color and named "Your Design Here".
I duplicate the bottom Shape layer into document "Wall Interiors jjmack" then Duplicated the top Vector Smart Object layer into "Wall Interiors jjmack" and I closed document "Rectangle 12.psb".
In Document "Wall Interiors jjmack" I Targeted layers "Vector Smart Object" and "Your Design Here" and converted them into a smart object I renamed to "Your Designs" the name the original template smart object layer had.
I then transform the smart object layer to fit in the guide lines and add the perspective for the lower layers frame. To size Smart Object Layer like the original template's object was sized.
Finally I removed the guide lines and saved "Wall Interiors jjmack.psd"
The Template "Wall Interiors jjmack.psd" does not have the problem "Wall Interiors.psd" has. Therefore I think "Wall Interiors.psd" may have been created on a Mac for you use a Mac and are having the problem I see in "Wall Interiors.psd"
Copy link to clipboard
Copied
Can you please upload some your templates you are having problems with. I only have one template that has a Smart Object layer that object does not open immediately synchronously when my scripts opens smart object function uses
runMenuItem(stringIDToTypeID('placedLayerEditContents'));
I believe that template may have been create on a Mac. For on my pc I can not create a template like that one. Its smart object layer looks like it was created from two Photoshop layers. A large rectangle shape layer that is filled with a color. This shape layer was larger than the templates canvas size. Above that layer there is a Vector Smart Object Layer the same the size as the Shape layer and that layer has a layer style added. Those two layers were converted into a smart object layer which was then sized and position for the markup template lower layers composition. The smart object layer's object transform was adjusted to make the smart object fit on the template canvas, positioned over a picture frame and add a little bit of perspective.
When I replicate that Template on my Windows PC the replicated template's smart object opens immediately synchronously when my scripts opens smart object function uses
runMenuItem(stringIDToTypeID('placedLayerEditContents'));
I do not install AI so I cannot create the vector smart object layer. AI file. Here are the steps I used to replicate the template on my PC from template that hast the issue.
First I open the template that has the Problem "Wall Interiors.psd"
I duplicated the document to a new document with the name "Wall Interiors jjmack"
In "Wall Interiors jjmack" I set Guide lines to the bounds of the smart object layer then deleted the smart object layer.
In "Wall Interiors" I opened the smart object layer object and closed "Wall Interiors" template document.
The opened smart object work document "Rectangle 12.psb" has two layer the top layer name is Vector Smart Object has it has a Layer Style Color Overlay. The Bottom layer the Rectangle Shape Layer with a solid fill color and named "Your Design Here".
I duplicate the bottom Shape layer into document "Wall Interiors jjmack" then Duplicated the top Vector Smart Object layer into "Wall Interiors jjmack" and I closed document "Rectangle 12.psb".
In Document "Wall Interiors jjmack" I Targeted layers "Vector Smart Object" and "Your Design Here" and converted them into a smart object I renamed to "Your Designs" the name the original template smart object layer had.
I then transform the smart object layer to fit in the guide lines and add the perspective for the lower layers frame. To size Smart Object Layer like the original template's object was sized.
Finally I removed the guide lines and saved "Wall Interiors jjmack.psd"
The Template "Wall Interiors jjmack.psd" does not have the problem "Wall Interiors.psd" has. Therefore I think "Wall Interiors.psd" may have been created on a Mac for you use a Mac and are having the problem I see in "Wall Interiors.psd"
Here is a link to a zip file containing the two PSD template files See if the both have your problem on your Mac Mockups.zip
Copy link to clipboard
Copied
#target photoshop
var docRef = app.activeDocument;
var actLayer = docRef.activeLayer;
function cTID(s) {
return app.charIDToTypeID(s);
};
function sTID(s) {
return app.stringIDToTypeID(s);
};
function waitForRedraw() {
var eventWait = charIDToTypeID("Wait");
var enumRedrawComplete = charIDToTypeID("RdCm");
var typeState = charIDToTypeID("Stte");
var keyState = charIDToTypeID("Stte");
var desc = new ActionDescriptor();
desc.putEnumerated(keyState, typeState, enumRedrawComplete);
executeAction(eventWait, desc, DialogModes.NO);
return;
}
// Check if object is PS
// R-Bin - JJ Mack
function objectIsPsObject(SOlayer) {
//Thanks to r-bin
var ext = smartobject_file_ext(SOlayer);
var rc = true;
switch (ext)
{
case "nef":
case "cr3":
case "cr2":
case "crw":
case "raf":
case "orf":
case "mrw":
case "dcr":
case "mos":
case "raw":
case "pef":
case "srf":
case "dng":
case "x3f":
case "erf":
case "sr2":
case "kdc":
case "mfw":
case "mef":
case "arw":
case "nrw":
case "rw2":
case "rwl":
case "iiq":
case "3fr":
case "fff":
case "srw":
case "ai":
case "svg":
case "pdf":
case "esp":
rc = false;
break;
case "error":
rc = false;
break;
default:
rc = true;
break;
}
return rc;
}
function smartobject_file_ext(layer) {
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));
r.putIdentifier(stringIDToTypeID("layer"), layer.id);
var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));
var n = name.lastIndexOf(".");
if (n < 0) return "";
return name.substr(n+1).toLowerCase();
}
catch (e) { return "error"; }
}
function placeImage(newFile){
// app.refresh(); // doesnt work
// app.redraw();// doesnt work
// waitMoment();
// $.sleep(5000);
// Switch document
// var desc111 = new ActionDescriptor();
// var ref13 = new ActionReference();
// ref13.putOffset( cTID('Dcmn'), 1 );
// desc111.putReference( cTID('null'), ref13 );
// desc111.putInteger( cTID('DocI'), 344 );
// executeAction( cTID('slct'), desc111, DialogModes.NO );
// Place images
// alert(newFile)
// alert(app.activeDocument)
// app.activeDocument = [1];
var desc174 = new ActionDescriptor();
desc174.putInteger( cTID('Idnt'), 885 );
desc174.putPath( cTID('null'), new File(newFile));
desc174.putBoolean( cTID('Lnkd'), true );
desc174.putEnumerated( cTID('FTcs'), cTID('QCSt'), cTID('Qcsa') );
var desc175 = new ActionDescriptor();
desc175.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), 0.000000 );
desc175.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), 0.000000 );
desc174.putObject( cTID('Ofst'), cTID('Ofst'), desc175 );
desc174.putUnitDouble( cTID('Wdth'), cTID('#Prc'), 234.782609 ); // i know the scale > this will make the image fit the SO file
desc174.putUnitDouble( cTID('Hght'), cTID('#Prc'), 234.697509 ); // i know the scale > this will make the image fit the SO file
executeAction( cTID('Plc '), desc174, DialogModes.NO );
// alert(app.activeDocument)
// alert(app.activeDocument)
// app.activeDocument.close(SaveOptions.SAVECHANGES);
}
function saveTmpDoc() {
// alert(tmpDoc)
var tmpDoc = app.activeDocument
var tmpPath = tmpDoc.path;
var tmpSave = tmpPath + osPath + tmpDoc.name; //tmpDoc + '/' + tmpDoc.name;
// alert(tmpSave)
// alert(osPath)
// alert(tmpSave)
var saveTmp = new ActionDescriptor();
saveTmp.putPath( cTID('In '), new File( tmpSave ) );
saveTmp.putInteger( cTID('DocI'), 587 );
saveTmp.putEnumerated( sTID('saveStage'), sTID('saveStageType'), sTID('saveSucceeded') );
executeAction( cTID('save'), saveTmp, DialogModes.NO );
return true
};
// Object: Timer
// Usage: Time how long things take or delay script execution
// Input: <none>
// Return: Timer object
// Example:
//
// var a = new Timer();
// for (var i = 0; i < 2; i++)
// a.pause(3.33333);
// a.getElapsed();
// jeff tranberry
///////////////////////////////////////////////////////////////////////////////
function Timer() {
// member properties
this.startTime = new Date();
this.endTime = new Date();
// member methods
// reset the start time to now
this.start = function () { this.startTime = new Date(); }
// reset the end time to now
this.stop = function () { this.endTime = new Date(); }
// get the difference in milliseconds between start and stop
this.getTime = function () { return (this.endTime.getTime() - this.startTime.getTime()) / 1000; }
// get the current elapsed time from start to now, this sets the endTime
this.getElapsed = function () { this.endTime = new Date(); return this.getTime(); }
// pause for this many seconds
this.pause = function ( inSeconds ) {
var t = 0;
var s = new Date();
while( t < inSeconds ) {
t = (new Date().getTime() - s.getTime()) / 1000;
}
}
}
function switchForward(){
// Switch document
var desc111 = new ActionDescriptor();
var ref13 = new ActionReference();
ref13.putOffset( cTID('Dcmn'), 1 );
desc111.putReference( cTID('null'), ref13 );
desc111.putInteger( cTID('DocI'), 344 );
executeAction( cTID('slct'), desc111, DialogModes.NO );
}
function switchBackward(){
// Switch document
var desc111 = new ActionDescriptor();
var ref13 = new ActionReference();
ref13.putOffset( cTID('Dcmn'), -1 );
desc111.putReference( cTID('null'), ref13 );
desc111.putInteger( cTID('DocI'), 344 );
executeAction( cTID('slct'), desc111, DialogModes.NO );
}
///////////////////////////////////////////////////////////////////////////////
// Function: checkPlacedLayers
// Usage: Count the comps' names collecting duplicates
// Input: collection of comps
// Return: object with names as keys and as values, an object with total count and an index (for incrementing during file naming)
// Source: Layer-Comps-To-Files.jsx
///////////////////////////////////////////////////////////////////////////////
function checkPlacedLayers(list,actLayer){
// var obj = {};
for(var i = 0; i<list.length; i++){
var name = list[i].name.match(/(.*)\.[^\.]+$/)[1];
// if(!actLayer.visible) alert(actLayer.name)
try {
// alert(name +" "+ actLayer.name)
if(name == actLayer.name){
// if(name == actLayer.name && name.visible){
// if(!actLayer.visible) alert(actLayer.visible)
// if(name in obj){
// alert("active layer can be taken out")
// var isVisible = desc.getBoolean(charIDToTypeID("Vsbl"));
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
desc.putReference( cTID('null'), ref );
var reff = new ActionReference();
reff.putProperty(app.charIDToTypeID("Prpr"), stringIDToTypeID("targetLayers"))
reff.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"))
// what do I want to do this this list? Define an description of an action.
var desclay = executeActionGet(reff);
desclay = desclay.getList(stringIDToTypeID("targetLayers"))
// alert(desclay);
lyrIndex = desclay.getReference(0).getIndex();
// alert(lyrIndex);
var lyrIndex;
try {
docRef.backgroundLayer // try to select a background layer, if I can then adjust the index counting. (Background layers change index counitng of all layers by 1)
lyrIndex = desclay.getReference(0).getIndex();
} catch (e) {
lyrIndex = desclay.getReference(0).getIndex() + 1;
}
// Added from getLayerInfo
var lyrRef = new ActionReference();
lyrRef.putIndex(charIDToTypeID("Lyr "), lyrIndex)
var lyrDesc = executeActionGet(lyrRef);
// alert(lyrDesc.getInteger(stringIDToTypeID("layerKind")))
var Id = lyrDesc.getInteger(stringIDToTypeID("layerID"));
// alert(Id)
var list33 = new ActionList();
list33.putInteger( 180 );
desc.putList( cTID('LyrI'), list33 );
executeAction( cTID('Dlt '), desc, DialogModes.NO );
break
}
} catch(e) {
alert(e)
break
}
}
// return obj;
}
function exportMockupDesigns() {
// Get OS
// fromt "_LastLogEntry.jsx"
isWindows = function() {
return $.os.match(/windows/i);
};
isMac = function() {
return !isWindows();
};
var osPath = (isMac() ? '/' : "/"); // resolve windows vs osx paths
// alert(isMac())
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;
var jpgopts = new JPEGSaveOptions();
jpgopts.embedProfile = true;
jpgopts.formatOptions = FormatOptions.STANDARDBASELINE;
jpgopts.matte = MatteType.NONE;
jpgopts.quality = 8;
// check if layer is smart object;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else if (objectIsPsObject(actLayer) != true) {
alert("The SmartOjbect is not a PSB or PSD file")
return 'cancel'; // quit, returning 'cancel' (dont localize) makes the actions palette not record our script
} else {
// select files;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg;*.jpeg;", true)
} else {
var theFiles = File.openDialog("please select files", getFiles, true)
};
// select Folder;
// var theFolder = Folder.selectDialog("select folder");
if (theFiles) {
// var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|png)$/i);
// Select destination folder
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
var destinationFolder = Folder.selectDialog("Please select a destination folder");
var totalTime = new Timer(); // Timer Tom Ruark // Source: Getter.jsx
function placeDesigns(){
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
// app.refresh();
// alert(editSO)
// $.sleep(500)
// waitForRedraw();
// var pauseTime = new Timer();
// pauseTime.pause(.5);
// Delete prior placed layer > script takes 6sec without also 6
var count = checkPlacedLayers(theFiles, app.activeDocument.activeLayer); // check names of files > we can compare and delete unused layers when smartobject is open
checkPlacedLayers(theFiles, app.activeDocument.activeLayer); // check names of files > we can compare and delete unused layers when smartobject is open
placeImage(theFiles[m]);
// app.activeDocument.close(SaveOptions.SAVECHANGES);
app.activeDocument.save(SaveOptions.SAVECHANGES);
switchBackward();
var theNewName = theFiles[m].name.match(/(.*)\.[^\.]+$/)[1];
//save jpg;
myDocument.saveAs((new File(destinationFolder.toString() + "/" + theName + "_" + theNewName + ".jpg")), jpgopts, true);
switchForward();
}
}
app.activeDocument.suspendHistory("Script > Place Designs", 'placeDesigns()');
};
}
};
////// get psds, tifs and jpgs from files //////
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|tif|png|jpg|jpeg)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
};
// app.activeDocument.close(SaveOptions.DONTSAVECHANGES); // close smartobject again
alert("Script Time: " + totalTime.getElapsed())
};
app.activeDocument.suspendHistory("Script > Export Mockup Designs", 'exportMockupDesigns()');
// exportMockupDesigns();
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Not adding much to the conversation, but thank you for the good laugh!
"I have no desire to look at your code."
I almost died laughing at the office. 😄
Copy link to clipboard
Copied
Thanks for posting this update! Definitely helps to have the other function to be able to detect. As per usual PS scripts are an enigma, even with the script listener plugin.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
The problem is strange and does not always fail the same way. The Problem seems to be related to some smart object layer and the way the are crated. For example the only template I had where the smart object did open right away with
runMenuItem(stringIDToTypeID('placedLayerEditContents'));
was one I download from the web. When I replicated the Template on my Windows machine it did open right away. However, if I ran the same batch job sever time in a row occasionally the object would fail to open right away in the middle of a batch but the code I added to wait for it to open or to add a open would solve the problem most of the time. Waiting always seem to work however occasionally other problems would happen if I added the open command. Perhaps I open and update the object then the opened again open again and missed up my script because it was the processing the wrong document.
Now the Template you posted only one smart object layer needs to be updated but the template has several smart object layers on the top of the stack. My mockup scripts are design to only update the top of the layers stack smart object layers. It is easy to convert Mockup templates to be compatible with my scripts design. So I modified your template. I simply grouped all the layer above the Background into a group named Mockup. I the used Ctrl+J to dupe the smart object layer that needs to be updated and move the copy out of the mockup group to the top of the layer stack and I removed the masks changed opacity and fill to 0 and turn off its visibility.
Right away the Smart Object fail to open but my script had not problem populating the template. I then replicated your template. I saved the smart object work file as a PSD File. I them placed the PSD file into the replicated template above the smart object layer that needs the replacement. and did a Ctrl+J to dupe it and moved the dupe to the top of the stack set fill and opacity to 0 and turn off its visibility. and deleted the old top of the stack smart object layer.. I then moved the vector layer mask off of the smart object layer to the placed psd smart object layer and the deleted the smart object layer that needed to be replaced. I clipped the Smart object layer to the Phone case layer at the sized the object with a transform to cover the phone case and Saved the Template. On my machine this template object open right away. Running many batches occasionally it may fails to open. This is a strange problem. I have not test in PS 2020 just 2021.
Here is a link to a zip file the has my text templates and scripts Mockups.zip
Copy link to clipboard
Copied
I have now tested with CS6, CC 2014, CC 2018, CC 2019, PS 2020 and PS 2021. All seen to fail to open the Smart Object Layer's object that your Mac created right away like the one I had downloaded. Where in PS 2021 version 22.1 if I wait using a wait till message the object will open. In prior Photoshop the object never opens I have to add the open for the temp work file that was created by Photoshop for placeLeyerEditContents but did not open.
So the scripts in my toolkit work in Photoshop version prior to PS 2021 on windows machines. I'm not positive they will on Mac for I'm not sure of where Photoshop created the temp work file on a mac. This is what I use for Windows it may be the same for Mac. depends on Forder.temp beint the place the file is created
var workFile = new File(Folder.temp + "/" + name); // May work for both Windows and Mac
PS 22.1 can occasionally have problem if I add the Open Command. However, adding the open performs faster than waiting for the object to eventually open So I will not be changing my scripts for waiting requires human interaction which is not appropriate for a batch process.
I look at you script its seem to work on my machine but has issues with Place scaling images and it leave the object open. I particularly do not like templates that have smart object layer where the object is layered and to populate the template you have to edits late within the object. Templates like that are too hard to use and requite a custom script for automation. It is far easier to create templates that simply need the smart object replaced. So I simplified your Template or the one you purchased or downloaded. I rasterized some layers and moved the tool bar layers into the template from the object and added a camera layer. This template can be batch processed with my script and if you use its edit option replacement images can be any size. The phone screen will be filled with image content. If replacement images are Phone size my scripts default isuse Replace content not edit here the template. samsung-s20-mockup-xxx.psd
http://www.mouseprints.net/old/dpr/samsung-s20-mockup-xxx.psd
Copy link to clipboard
Copied
thanks once more! I didnt create this template so im not sure on what machine it was created. When i started this script i started coding on OSX using 2018cc and at one point it worked like i wanted it. Then i started tweaking it and it kept failing also on windows using newer version of PS. It took me couple days and hours each day to do tests and made many variants of this script. The version i linked works fine on Windows now, i still need to test OSX.
Concerning the placed object, i dont think it matters on what machine it was created. Since the PSB is not linked it should open on any machine. Only when its linked and you switch OS's then the path becomes an issue.
About the order in the templage. Since i didnt create it i didnt take to much notice of it. Since i dont really use this very much myself. I started this script to help someone which wanted a variant of replacing smartobject, then i noticed several people asking to use this for mockup designs.
The way always works for me is open the smartobject once. Then select the layer above the phone and under the top placed AI file, the phone battery and charge part. I understand you method of duplicating and placing it on top. Yet, i dont think it really matters in the end. If you make sure what layer is selected it should always work.
Ill try you updated version as well, see if it also works for me here
Copy link to clipboard
Copied
I tried you mockup setup, but ran in the issue with not having the obj1,obj2,obj3 subfolders. I dont think i like this setup. It makes things to complicated. I also dont quite understand the need why the files which need to go in the smartobject need to be in subfolders?! It asks for 3 folder, but i selected a folder with 10 items?! I guess that is tailored to those examples i saw on you linked page. I see 3 images inside a main page, thats the only thing i could think of why it wants 3 subfolders.
What i do like is how you setup that dialog, picking a base file, then pick the files need to go in the smartobject. Thats very nice!
Copy link to clipboard
Copied
You can use the BatchReplaceOneObject.jsx in my package when you only need one smart object layer replaced. You do not need to create the sub folders smart object layers replacement queues obj0, obj1, ..., objN.
The BatchMockupTemplates.jsx script can populate any number of templates that have some number of smart object layer 0, 1, ..., N that need to be populated with replacemennts. The top of the layer stack consecutive smart object layers objects. Layer stack level 0, 1, ..., N. the subfolders obj0, obj1, ..., objN contain the queue of replacement image for each of the smart of the smart objects layers. You could have 100 psd templates that have a 100 smart object layer on the top of the layer stack, and have 100 replacement image is each of the objN subfolders. The script would populate the 10,000 replacement images into the 100 Templates. Each of the 100 Templates would be populated 100 times . The Script would save out 10,000 populate jpeg files.
My script are for batch processing templates. If you only need one output file populate the template manually. If you want to batch process templates you need to have a template design that is easy to populate with automation and some setup will be required. Use the Help button in my Scripts dialog.
It would be very easy to populate the template you posted manually but it harder then it need to be, For you have to open the smart object tanh know how to update the object. Update the object and commit the update. It would be easier if all you needed to do was replace the smart object content.
Copy link to clipboard
Copied
Well i tried my working windows version of the script on OSX with 2018cc and there it wont work. Even when i place the "placedLayerEditContents" on the second line in the script right after #target photoshop, it doesnt open until everything else has run. This is really strange! I added waits, refresh and sleep, nothing helps.
Then i tried your function "openSmartObject", which does work in this scenario. This function does not work when i use it on windows. First i that because of the alert, it slows things down. This happens on Windows actually, some script work when i add an alert. But i also tried your function without that alert it gives about opening. It still runs just fine on Mac. perhaps this has something todo that on OSX we see that open command right after "placedLayerEditContents", i dont think i see that on Windows. I need to check it. Then it would make sense why the app.runMenuItem doesnt work
So tonigtht i try this new version on Windows again and see if it work with your function. I just need to try it again. Otherwise i use 2 method for the 2 OS's the "app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));" works fine on Windows. So im thinking of doing it like this:
if(isMac()){
openSmartObject (theLayer)
} else {
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
}
So if we have a Mac then we use JJmack script if we have Windows we run this command. Its best of both worlds 😉
Copy link to clipboard
Copied
And now Apple has its own processor M1. Adobe stated they will have a Photoshop version for it sometime in 2021.
I believe I made the right choice in 2000 that is Microsoft. Microsoft respects their users, knows they need to fix bugs and develop Windows it a way that users old software will run. If not possible help users providing compatibility troubleshooters, compatibility modes.