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

JavaScript - save file to folder in parent directory

Community Beginner ,
Jan 21, 2020 Jan 21, 2020

Hello everyone!

 

I managed to frankenstein together a javascript I found online to save out a JPG and PNG from a PSD to the parent directory and folder inside the parent directory.  After a couple of nights of troubleshooting, the script below is doing MOSTLY what I need.  From the working file (the PSD directory), I'm able to save out a JPG to the parent directory but I can't figure out the magic combination to send the PNG to the PARENT->PNG folder.  Any help is appreciated!

 

MY DIRECTORY + FILE SETUP:

  • PARENT (holds the PSD folder, PNG folder and JPG files)
    • PSD (these are my _working files)
    • PNG


THE SCRIPT:

// reference open doc
var doc = app.activeDocument;

// define parent directory
var parentF = new Folder(doc.path.parent);

 

// set save options for JPG
var jpg = new ExportOptionsSaveForWeb();

jpg.interlaced = false;
jpg.quality = 100;
jpg.includeProfile = false;
jpg.format = SaveDocumentType.JPEG; // Document Type

// save png file in parent directory of open doc
activeDocument.exportDocument((doc.path.parent), ExportType.SAVEFORWEB, jpg);

 

//set save options for PNG
var png = new ExportOptionsSaveForWeb();

png.PNG8 = false;
png.transparency = true;
png.interlaced = false;
png.quality = 100;
png.includeProfile = false;
png.format = SaveDocumentType.PNG; // Document Type

// save png file in PNG folder in parent directory

activeDocument.exportDocument((doc.path.parent + '/PNG'), ExportType.SAVEFORWEB, png);

TOPICS
Actions and scripting
12.0K
Translate
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

correct answers 1 Correct answer

Mentor , Jan 22, 2020 Jan 22, 2020

Document.exportDocument (exportIn: File , exportAs: ExportType , options: ExportOptions )  

 

First argument must be File (full path with filename). Folder to export this file must be created before.

 

var png = new ExportOptionsSaveForWeb();

png.PNG8 = false;
png.transparency = true;
png.interlaced = false;
png.quality = 100;
png.includeProfile = false;
png.format = SaveDocumentType.PNG; 

// save png file in PNG folder in parent directory
var exportFolder = Folder ((app.activeDocument.path.parent
...
Translate
Adobe
Mentor ,
Jan 22, 2020 Jan 22, 2020

Document.exportDocument (exportIn: File , exportAs: ExportType , options: ExportOptions )  

 

First argument must be File (full path with filename). Folder to export this file must be created before.

 

var png = new ExportOptionsSaveForWeb();

png.PNG8 = false;
png.transparency = true;
png.interlaced = false;
png.quality = 100;
png.includeProfile = false;
png.format = SaveDocumentType.PNG; 

// save png file in PNG folder in parent directory
var exportFolder = Folder ((app.activeDocument.path.parent) + "/PNG")
if (!exportFolder.exists) exportFolder.create ()
app.activeDocument.exportDocument (File(exportFolder + "/" + app.activeDocument.name + ".png"),ExportType.SAVEFORWEB, png )  

 

 

 

 

Translate
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 Beginner ,
Jan 22, 2020 Jan 22, 2020

This is perfect!  Thank you very much, Dmitry!

Translate
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 ,
Jan 22, 2020 Jan 22, 2020

Hi Dmitry I like your script, however, one thing struck me as potentially requiring modification...

 

I'd add a filename variable to strip the source filename extension so that the result is not a double extension such as .psd.png etc.

 

 

var baseName = app.activeDocument.name.split('.')[0];

//...

app.activeDocument.exportDocument (File(exportFolder + "/" + baseName + ".png"),ExportType.SAVEFORWEB, png )

 

 

 

 

Translate
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 Beginner ,
Jan 23, 2020 Jan 23, 2020
LATEST

Great catch, Stephen.  I noticed one of the files had the psd.png and your update corrected it.  Thanks so much!

Translate
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 ,
Jan 22, 2020 Jan 22, 2020

download and install the plug-in  Image Processor Pro.... It should be easy to do what you want to do using it.

image.png

JJMack
Translate
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 Beginner ,
Jan 22, 2020 Jan 22, 2020

Ah nice.  I'll look into it.  Thanks JJ!

Translate
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