• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Photoshop Scripting: How can I duplicate the document to a new file and run my export actions on the new one?

New Here ,
Aug 02, 2018 Aug 02, 2018

Copy link to clipboard

Copied

Hi, so I have many actions that will resize, edit, export, and close the document I'm currently working with. I'd like to write a Photoshop script that will duplicate the document to a new file and run all the actions that I'd normally run on the original document. I'd like it to give me a user input box that would be the name of the new document. Currently this is what I came up with last week, and it's not quite working:

var newName = prompt("Enter in a new name for the file, without the extension.", "", "New Document Name");

if (null !== newName) {

     var doc = app.activeDocument; 

     var extension = doc.name.split('.')[1] 

     newName += '.' + extension; 

     var docName= doc.fullName; 

     var file = new File(docName); 

     file.rename (newName);

     app.open(file);

     docLay=app.activeDocument.layers;

     l=app.activeDocument.layers.length;

     while (l>0) {

     l--;

     docLay.isBackgroundLayer = false;

     docLay.allLocked = false;

     }

     doc.close (SaveOptions.SAVECHANGES); 

}

Basically I just want to get the new name for the new document, create that document by copying current document, and then run my actions on that new document without altering the original document. I'd also not like to deal with saving, and I only say this because I've had errors in the past trying to get this to work where it would say that the document must be saved or something, so in short I don't need any psd files associated with my workflow.

Thanks in advance for anyone who could help me!!

TOPICS
Actions and scripting

Views

5.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Aug 02, 2018 Aug 02, 2018

Copy link to clipboard

Copied

Image > Duplicate does what you want (although I have no idea how you script that).

But maybe you don't have to. I do this all the time with actions. There are two tricks:

One, have a fixed destination folder somewhere. Mine is just called "outbox" and sits on the desktop. These files are all going somewhere.

Two, the crucial part: End the action with close without saving. This is why you need the outbox folder. You include that in the action, either as Save As, Export, or Save For Web. They all work. Then you have a branched out copy saved, and the original can be closed without saving.

This way, the original is never touched. It always goes back home as if nothing happened.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2018 Aug 02, 2018

Copy link to clipboard

Copied

BTW, this can be batched, and then you need to set "Destination: None" in the batch dialog. Again, the destination is recorded in the action, immediately before the final close.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 02, 2018 Aug 02, 2018

Copy link to clipboard

Copied

That's ununderstable the way you want to do it as normally that should be done other way, but here's a script:

function sTT(v) {return stringIDToTypeID(v)}

$.level = 0; displayDialogs = DialogModes.NO

function rM(v) {runMenuItem(stringIDToTypeID(v))}

newName = prompt('New name:', '', 'New name')

ext = (aD = activeDocument).name.match(/\.[^\.]+$/)

fle = aD.path + '/' + newName + ext[0]

aD.fullName.copy(fle), rM('selectAllLayers')

function AD() {return new ActionDescriptor()}

(referecne = new ActionReference()).putEnumerated

(sTT('layer'), sTT('ordinal'), sTT('targetEnum'));

(dsc = AD()).putReference(sTT('null'), referecne)

dsc.putObject(lL = sTT('layerLocking'), lL, AD())

executeAction(sTT('applyLocking'), dsc)

try{

     vis = (bG = aD.backgroundLayer).visible

     bG.isBackgroundLayer = false

     if (!vis) bG.visible = true

}

catch(err){rM('selectNoLayers')}

aD.close(SaveOptions.SAVECHANGES), open(File(fle))

// doAction('Action Name', 'Action Set Name')

Take into consideration 'duplicated' document will get state of last saving of original document.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 22, 2021 Sep 22, 2021

Copy link to clipboard

Copied

Hi, how would I approach the same action while keeping same file name?

 

The reason I want this is to duplicate the document, then resize it, save the file keeping the original name but adding suffix of _review and finally close duplicated file without saving any changes. 

 

I've got everything working but the document duplicate. 

 

var doc = app.activeDocument;

// scale the doc

var fWidth = 1500;
var fHeight = 1000;

// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}

//save new .psb with "_review" suffix
var d = app.activeDocument;
var dPath = d.path;
var dname = d.name.replace(/\.[^\.]+$/, '');
var suffix = '_review';

var idsave = charIDToTypeID( "save" );
var desc17 = new ActionDescriptor();
var idAs = charIDToTypeID( "As  " );
var desc18 = new ActionDescriptor();
var idPhteight = charIDToTypeID( "Pht8" );
desc17.putObject( idAs, idPhteight, desc18 );
var idIn = charIDToTypeID( "In  " );
desc17.putPath( idIn, new File( dPath + '/' + dname + suffix + '.psb' ) );
var idDocI = charIDToTypeID( "DocI" );
desc17.putInteger( idDocI, 195 );
var idCpy = charIDToTypeID( "Cpy " );
desc17.putBoolean( idCpy, true );
var idLwCs = charIDToTypeID( "LwCs" );
desc17.putBoolean( idLwCs, true );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc17.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc17, DialogModes.NO );

//colse active document without saving changes
//activeDocument.close(SaveOptions.DONOTSAVECHANGES);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2021 Sep 22, 2021

Copy link to clipboard

Copied

If you are not changing the name why are you duplicating the document? Just ues your export action 

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 22, 2021 Sep 22, 2021

Copy link to clipboard

Copied

Next time use </> icon when posting the code:

doc = app.activeDocument, dPath = doc.path
dname = doc.name.replace(/\.[^\.]+$/, '')
dplctn = doc.duplicate(dname + '_review')

fWidth = 1500, fHeight = 1000

if (doc.height > doc.width) dplctn.resizeImage(null, UnitValue(fHeight,'px'), null, ResampleMethod.BICUBIC)
else dplctn.resizeImage(UnitValue(fWidth,'px'), null, null, ResampleMethod.BICUBIC)

idsave = charIDToTypeID('save'), desc1 = new ActionDescriptor()
idAs = charIDToTypeID('As  '), desc2 = new ActionDescriptor()
idPhteight = charIDToTypeID('Pht8'), desc1.putObject(idAs, idPhteight, desc2)
idIn = charIDToTypeID('In  '), desc1.putPath(idIn, File(dPath + '/' + dplctn.name + '.psb'))
executeAction(idsave, desc1), dplctn.close(SaveOptions.DONOTSAVECHANGES)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 23, 2021 Sep 23, 2021

Copy link to clipboard

Copied

LATEST

Thanks a lot Kukurykus. 

 

JJMack, export action will not export .psb file. I need it to make smaller (6 times) resolution file for a review purposes. We are working with 4-10gb files and it takes too long to open them just to review it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Aug 03, 2018 Aug 03, 2018

Copy link to clipboard

Copied

displayDialogs = DialogModes.ALL;

// do not clamp Ctrl+Alt when you run the script

// otherwise the dialog does not appear

activeDocument.duplicate();

displayDialogs = DialogModes.ERROR;

doAction("Action 1", "Set 1"); // launching your first action

doAction("Action 2", "Set 1"); // launching your second action

doAction("Action 1", "Set 2"); // launching other of your actions

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 03, 2018 Aug 03, 2018

Copy link to clipboard

Copied

He wants to duplicate active document to new file, so not to another document. It's because he doesn't want script saved new document, but it was already saved when later you play actions on it.

If you duplicate document you'll get only another unsaved document (also without extension as it's not related to any file).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Aug 03, 2018 Aug 03, 2018

Copy link to clipboard

Copied

Maybe so?

var doc = activeDocument;

var name = doc.fullName;

doc.duplicate(doc.name);

var doc1 = activeDocument;

doc.close(SaveOptions.DONOTSAVECHANGES);

activeDocument = doc1;

executeAction(stringIDToTypeID("save"), undefined, DialogModes.ALL);

//doAction("Action 1", "Set 1");

 

//doAction("Action 2", "Set 1");

 

//doAction("Action 1", "Set 2");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 03, 2018 Aug 03, 2018

Copy link to clipboard

Copied

Still not 🙂 Now there's no chance to user input, and what is most important it saves duplicated document while he wanted avoid saving. I know he wants it to be done too complicated way, but that is what he wants untill he will change his mind 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Aug 03, 2018 Aug 03, 2018

Copy link to clipboard

Copied

He sets the name in the saveAs dialog

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 03, 2018 Aug 03, 2018

Copy link to clipboard

Copied

You're right, it's where he can do it. The only problem is he didn't want file was saved by a script, however prompted or not.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Aug 03, 2018 Aug 03, 2018

Copy link to clipboard

Copied

activeDocument.fullName.copy(NEW_FULL_NAME);

??? 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 03, 2018 Aug 03, 2018

Copy link to clipboard

Copied

It's what I exactly did in my script 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2021 Sep 22, 2021

Copy link to clipboard

Copied

...

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines