Skip to main content
Participant
February 5, 2012
Question

script to create directory based on current file name and then save the file in that directory

  • February 5, 2012
  • 1 reply
  • 6261 views

Hi all,

I have a need save all my projects in a directory using the name of the main image I am working on.

Manually these are the steps I used:

  1. Save As.
  2. Copy ( press command-c <== the filename is highlighted by default so all I have to do is )
  3. New Folder ( press the new folder button. Brings up a dialog box)
  4. Paste ( press command-v. <== Pastes the file name into the "New Folder" dialog box.)
  5. Save ( press thre save button. Saves the files into the new folder )
  6. Save As.
  7. Change format to JPG
  8. Save
  9. Change image quality to 12

I want to assign these actions to a key

Recording these steps and playing them back does not give me the results I need, snce the original name is hard-coded in the actionlist that was recorded

What should I be doing instead ?

Thanks in advance !

This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
February 5, 2012

You don’t mention which format the original file is (and figuring that and its current settings out seems too much of a hassle to me) so this would only save the jpgs into the folder (and create one if it does not exist).

If you want to give it a try, paste the following text into a new file in ExtendScript Toolkit (part of Photoshop’s installation, Applications/Utilities/Adobe Utilities/ExtendScript Toolkit CS4 or /Applications/Utilities/Adobe Utilities-CS5/ExtendScript Toolkit CS5) and save it as a jsx-file into Photoshop’s Presets/Scripts-folder.

After restarting Photoshop the Script should be available under File > Scripts and can be assigned a Keyboard Shortcut directly, recorded into an Action, (in CS4 and CS5) be used in a Configurator-Panel or started from ExtendScript Toolkit directly.

// saves jpg into folder of file’s name next to file;

// be advised: this  overwrites existing jpgs of the same name without prompting;

// thanks to xbytor;

// 2012, use it at your own risk;

#target photoshop;

if (app.documents.length > 0) {

var thedoc = app.activeDocument;

// getting the name and location;

var docName = thedoc.name;

if (docName.indexOf(".") != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}

else {var basename = docName};

// getting the location, if unsaved save to desktop;

try {var docPath = thedoc.path}

catch (e) {var docPath = "~/Desktop"};

// create folder if it does not exist;

var folderString = docPath+"/"+basename;

if (Folder(folderString).exists == false) {new Folder(folderString).create()};

// jpg options;

var jpegOptions = new JPEGSaveOptions();

jpegOptions.quality = 12;

jpegOptions.embedColorProfile = true;

jpegOptions.matte = MatteType.NONE;

//save jpg as a copy:

thedoc.saveAs((new File(folderString+"/"+basename+".jpg")),jpegOptions,true);

};

Participating Frequently
October 26, 2016

Hey hey ! your script works great !  Thank you for your work !

The problem is, it's not working when you select multiple files (it creates a folder for every file )

But, I want to move 25 files into just ONE folder.

Files in my case are like this :

NFGFSHTEFD-1.jpg

NFGFSHTEFD-2.jpg

NFGFSHTEFD-3.jpg

NFGFSHTEFD-4.jpg

NFGFSHTEFD-5.jpg

NFGFSHTEFD-6.jpg

NFGFSHTEFD-7.jpg

....

NFGFSHTEFD-25.jpg

Please help me, or point me to the correct path !

Thanks and greetings from Germany

Adrian

natrev
Legend
October 27, 2016

Hi cinevaa77289409,

You need to replace this code

if (docName.indexOf(".") != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]} 

else {var basename = docName};

to this code

var docName = (thedoc.name).replace(/\.[^\.]+$/, '');

if(basename.indexOf("-")!=-1){basename=docName.substring(0,docName.indexOf("-"));}

else{basename=docName;}

this script will move that 25 files into one folder.

Regards,

yajiv