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

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

New Here ,
Feb 04, 2012 Feb 04, 2012

Copy link to clipboard

Copied

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 !

TOPICS
Actions and scripting

Views

5.5K

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 ,
Feb 05, 2012 Feb 05, 2012

Copy link to clipboard

Copied

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);

};

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 Beginner ,
Oct 26, 2016 Oct 26, 2016

Copy link to clipboard

Copied

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

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
Engaged ,
Oct 26, 2016 Oct 26, 2016

Copy link to clipboard

Copied

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

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 Beginner ,
Oct 26, 2016 Oct 26, 2016

Copy link to clipboard

Copied

Hi Natrev,

thank you for your help,

unfortunately it gives me an error on line 11 : "Error 2 : basename undefined"

can't figure it out what's wrong

hope you can help me

thank you !

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
Engaged ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

Hi,

Sorry the that code. Please use this code

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

to this code

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

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

else{var basename=docName}

-yajiv

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 Beginner ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

Hi again Natrev !

it's working but again it creates a folder for every single file, so if I have 25 files, it creates 25 folders...

I just want to move all 25 files into ONE folder based on the name of the file BLABLABLA

but, hey, thank you so much for a quick answer !!!

greetings form Germany !

Adrian

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
Engaged ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

Hi Adrian,

Try this code. its working.. if the file name is NFGFSHTEFD-1.jpg, NFGFSHTEFD-2.jpg....NFGFSHTEFD-25.jpg..

#target photoshop; 

if (app.documents.length > 0) { 

    var thedoc = app.activeDocument; 

    // getting the name and location; 

    var docName = thedoc.name; 

    var basename;

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

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

    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+"/"+docName+".jpg")),jpegOptions,true); 

}; 

- yajiv

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 Beginner ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

You are the man !!!!

Thank you !!! Vielen Dank !!!

can't tell you how much time you save me !!!

Thank you  Natrev !

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
Engaged ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

Hi Adrian,

Can you please mark correct answer for c.pfaffenbichler post. I have only done some slight change based on your requirement.

-yajiv

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 Beginner ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

LATEST

c.pfaffenbichler​ correct answer ! thank you also natrev​ for sorting it out for my requirement !

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