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

saveAs: overwrite files or ask what to do?

Community Expert ,
May 05, 2015 May 05, 2015

this is one of those situations where i feel like i'm missing something obvious, but with as little knowledge as i have there's a lot to be potentially missed. anyway, enough self-deprecation...

this script came from a discussion i found ages ago that fit what i needed to do at the time. i don't remember the exact discussion but Larry G.S. rounded it off and posted this:

**********************************************************

 

Place_JPG_to_AI.jsx

DESCRIPTION

This script gets files specified by the user from the 

selected folder and batch processes them and saves them 

as AIs in the user desired destination with the same 

file name.

Modified from Adobe supplied scripts and

scripts by Carlos Canto on Illustrator Scripting

by Larry G. Schneider

 

**********************************************************/ 

 

// uncomment to suppress Illustrator warning dialogs 

app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; 

 

var destFolder, sourceFolder, files, fileType; 

 

// Select the source folder. 

sourceFolder = Folder.selectDialog( 'Select the folder containing M.E.T. JPEG files', '~' ); 

 

// If a valid folder is selected 

if ( sourceFolder != null ) 

     files = new Array(); 

      

     // Get all files with JPEG extension

     files = sourceFolder.getFiles("*.jpg"); 

      

     if ( files.length > 0 ) 

     { 

          // Get the destination to save the files 

          destFolder = Folder.selectDialog( 'Select the folder where you want to save the AI files.', '~' ); 

           

          for ( i = 0; i < files.length; i++ ) 

          { 

               var idoc = app.open(File("/G/Design & Publishing/Elements/Master Templates/Re-Engineering Templates/Illustrator/MET Template 106.82x60.ai"));  // open template document. Change the file path for different templates

              

               var ilayer = idoc.layers["Image"];

 

               var iplaced = ilayer.placedItems.add();  // add image place holder 

              

               iplaced.file = (files);  // place file 

                

                var PosXY=new Array(2);  // ensure position is 0,0

                PosXY[0]=0; 

                PosXY[1]=0;

                iplaced.position = PosXY;

                iplaced.embed();

                

               ilayer.dimPlacedImages = false;   // do not dim images       

               ilayer.locked = true;  // lock layer 

               ilayer.printable = true;

                

               var docName = (files.name.split('.'))[0];   // take the first part of the placed file name for the new document 

      

               var destFile = new File(destFolder + "/" +  docName + ".ai");   // make a new file in the dest folder 

                

               var options = new IllustratorSaveOptions();   // new save options 

               options.compatibility = Compatibility.ILLUSTRATOR15;   // save as AICS5. May want to update if we go to CC

               options.pdfCompatible = true; // needs to be set to view links in InDesign

                

               // Export as AI 

               idoc.saveAs(destFile,  options);  // save the file in the dest folder 

      

               idoc.close(SaveOptions.DONOTSAVECHANGES);   // close the file without saving 

          } 

          alert( 'Files are saved as AI in ' + destFolder ); 

     } 

     else 

     { 

          alert( 'No matching files found.' ); 

     } 

 

app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS 

i edited little bits for our purposes but it's basically the same. user selects a folder with jpegs, a dest folder, and the script places the jpegs on my template and saves them with the original filename. however, occasionally the user will need to do this several times into the same dest folder, and won't necessarily know what jpegs they've already done. so they've ended up overwriting files that might already have been worked on (names can't change after the fact for some very boring and irritating reasons).

what they'd like, in that case, is for the script to alert them when files with that filename already exist in the dest folder (but run as normal otherwise). i thought just turning DISPLAYALERTS back on might do this (since that's what saveAs does when you go through it manually) but it didn't seem to make any difference. i can't find clues in the reference, but then i'm not entirely sure what i'm looking for.

any help or pointers at any level of detail welcome.

TOPICS
Scripting
2.1K
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

Guide , May 05, 2015 May 05, 2015

switch out null for 0.

Translate
Adobe
Guide ,
May 05, 2015 May 05, 2015

test = destfolder.getFiles(docName + ".ai");

if(test != null){alert("file exists");}

not tested as I am not near a computer

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 ,
May 05, 2015 May 05, 2015

g'day Qwerty. that seems to trigger the alert whether there's a file by that name in the folder or not.

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 ,
May 05, 2015 May 05, 2015

getFiles is an array, right? might it need to loop through them?

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
Guide ,
May 05, 2015 May 05, 2015

getFiles is an array, but the docName + ".ai" is a filter, so it should only find that file.

needs to happen before your "var destFile = ... " line.

I'm back at my PC now so I'll have a play.

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
Guide ,
May 05, 2015 May 05, 2015

switch out null for 0.

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 ,
May 06, 2015 May 06, 2015

works great. thanks Qwerty!

next on the list might be a dialogue from which the user can choose to continue (and overwrite) or stop. never done that before, so i might well be back when i inevitably get stuck.

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 ,
May 06, 2015 May 06, 2015

/**********************************************************

 

Place_JPG_to_AI.jsx

DESCRIPTION

This script gets files specified by the user from the 

selected folder and batch processes them and saves them 

as AIs in the user desired destination with the same 

file name.

Modified from Adobe supplied scripts and

scripts by Carlos Canto on Illustrator Scripting

by Larry G. Schneider 072911

 

**********************************************************/ 

 

// uncomment to suppress Illustrator warning dialogs 

function jpegMET(){

app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; 

 

var destFolder, sourceFolder, files, fileType; 

 

// Select the source folder. 

sourceFolder = Folder.selectDialog( 'Select the folder containing M.E.T. JPEG files', '~' ); 

 

// If a valid folder is selected 

if ( sourceFolder != null ) 

     files = new Array(); 

      

     // Get all files with JPEG extension

     files = sourceFolder.getFiles("*.jpg"); 

      

     if ( files.length > 0 ) 

     { 

          // Get the destination to save the files 

          destFolder = Folder.selectDialog( 'Select the folder where you want to save the AI files.', '~' ); 

           

          for ( i = 0; i < files.length; i++ ) 

          { 

               var idoc = app.open(File("/G/Design & Publishing/Elements/Master Templates/Re-Engineering Templates/Illustrator/MET Template 106.82x60.ai"));  // open template document. Change the file path for different templates

              

               var ilayer = idoc.layers["Image"];

 

               var iplaced = ilayer.placedItems.add();  // add image place holder 

              

               iplaced.file = (files);  // place file 

                

                var PosXY=new Array(2);  // ensure position is 0,0

                PosXY[0]=0; 

                PosXY[1]=0;

                iplaced.position = PosXY;

                iplaced.embed();

                

               ilayer.dimPlacedImages = false;   // do not dim images       

               ilayer.locked = true;  // lock layer 

               ilayer.printable = true;

                

               var docName = (files.name.split('.'))[0];   // take the first part of the placed file name for the new document 

              

               var test = destFolder.getFiles(docName + ".ai");

               if(test != 0){

                   var overWrite = confirm("A file with this name already exists. Continue and overwrite?");

                   if (overWrite){

                        var destFile = new File(destFolder + "/" +  docName + ".ai");   // make a new file in the dest folder 

                        }else{

                   return;

                   }

               }else{

                   var destFile = new File(destFolder + "/" +  docName + ".ai");   // make a new file in the dest folder 

                   }

               var options = new IllustratorSaveOptions();   // new save options 

               options.compatibility = Compatibility.ILLUSTRATOR15;   // save as AICS5. May want to update if we go to CC

               options.pdfCompatible = true; // needs to be set to view links in InDesign

                

               // Export as AI 

               idoc.saveAs(destFile,  options);  // save the file in the dest folder 

      

               idoc.close(SaveOptions.DONOTSAVECHANGES);   // close the file without saving 

          } 

          alert( 'Files are saved as AI in ' + destFolder ); 

     } 

     else 

     { 

          alert( 'No matching files found' ); 

     } 

}

 

app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS 

}

jpegMET ()

this seems to work... one little blip though. most files are named in the format 'AIM_0000' (straight from the user's camera), but one named 'AIM_0000 0' for whatever reason didn't display the message... can't see why it'd skip it. it still overwrites though.

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
Guide ,
May 06, 2015 May 06, 2015

‌

‌nnot sure it will fix the bug.

Bit you might be better replacing split with replace.

var docName = (files.name.replace(".jpg",".ai"));

safety against a file file name with a dot in it. Not 100% fail safe as it may silt have the whole .jpg string in file name, but safer.

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 ,
May 06, 2015 May 06, 2015

didn't affect the bug i'm afraid, but i have incorporated it anyway. thanks for the advice.

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 ,
May 06, 2015 May 06, 2015

seems to be any filename with a space in it... does getFiles not like spaces?

hmmm. but it wouldn't work in the first place if that was the case i guess.

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
Guide ,
May 06, 2015 May 06, 2015

Try this...

var docName = decodeURI(files.name.replace(".jpg",".ai"));


I'mm plucking at straws but it may be due to the %20 in place of the spaces that's causing the issue.

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
Guide ,
May 06, 2015 May 06, 2015

I was just doing a test.

I am confident that this is the issue.

the above should work.

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 ,
May 07, 2015 May 07, 2015

ah! yes, that's it. thank you 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
Guide ,
May 07, 2015 May 07, 2015
LATEST

‌glad I could help

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