Skip to main content
prema56615405
Inspiring
June 22, 2019
Answered

Run the script for the selected files

  • June 22, 2019
  • 3 replies
  • 4100 views

Below is the script to convert the ai files to pdf which in available in the selected folder. Is there any possibility to run the script only for the selected files in a particular folder.

#target illustrator 

var destFolder, sourceFolder, files, fileType, sourceDoc, pdfSaveOpts; 

// Select the source folder. 

sourceFolder = Folder.selectDialog( 'Select the folder with .ai files you want to convert to PDF'); 

if ( sourceFolder != null ) 

    files = new Array(); 

    fileType = "*.ai"; 

    files = sourceFolder.getFiles(fileType); 

    if ( files.length > 0 ) 

    { 

        destFolder = Folder.selectDialog( 'Select the folder to save the converted PDF files'); 

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

        { 

          sourceDoc = app.open(files); 

          pdfSaveOpts = getPDFOptions(); 

          sourceDoc.saveAs(destFolder, pdfSaveOpts); 

          sourceDoc.close(); 

        } 

        alert( 'Files are saved as PDF'); 

    } 

    else 

    { 

        alert( 'No matching files found' ); 

    } 

}

//Save PDF in Preset option 

function getPDFOptions() { 

    var pdfSaveOpts = new PDFSaveOptions();

    pdfSaveOpts.pDFPreset= 'Smallest File Size';

    return pdfSaveOpts;

}

This topic has been closed for replies.
Correct answer Developer1009

Here is another version of your script where you can select multiple files and script will run on selected files.

#target illustrator

var destFolder, myFile, files, sourceDoc;

files = File.openDialog("Select one or more files?", true);

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

{

myFile = files

var fileName = myFile.name

var extension = fileName.substr(fileName.lastIndexOf('.') + 1, fileName.length);

if (extension == 'ai')

{

sourceDoc = app.open(files);

sourceDoc.close();

alert('Files are saved as PDF');

} else {

alert('No matching files found');

}

}

Hope this will help you. Also, instead of checking the extension of the file you can use the filter option in open DIalog, that only allow selecting ai files. So in that case, checking extension can skip from the script.

Thanks

Charu

3 replies

Inspiring
June 25, 2019

renél80416020

I am so sorry, I did not know about your intention. So sorry.

Yes, this is the way people can learn.

renél80416020
Inspiring
June 25, 2019

charur94718007

Merci pour votre réponse.

Après 38 années d'enseignement c'est difficile de changer...

prema56615405
Inspiring
June 25, 2019

Thank you renél80416020​and charur94718007​.

Actually I am in the basic level of javascript. Don't have deep knowledge in Javascript.

Developer1009Correct answer
Inspiring
June 25, 2019

Here is another version of your script where you can select multiple files and script will run on selected files.

#target illustrator

var destFolder, myFile, files, sourceDoc;

files = File.openDialog("Select one or more files?", true);

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

{

myFile = files

var fileName = myFile.name

var extension = fileName.substr(fileName.lastIndexOf('.') + 1, fileName.length);

if (extension == 'ai')

{

sourceDoc = app.open(files);

sourceDoc.close();

alert('Files are saved as PDF');

} else {

alert('No matching files found');

}

}

Hope this will help you. Also, instead of checking the extension of the file you can use the filter option in open DIalog, that only allow selecting ai files. So in that case, checking extension can skip from the script.

Thanks

Charu

renél80416020
Inspiring
June 25, 2019

Bonjour charur94718007

Je voulais que prema56615405  réfléchisse et trouve de lui même.

C'est la meilleur solution pour apprendre non ?

de LR

CarlosCanto
Braniac
June 22, 2019

use File open dialog instead of Folder select dialog

File.openDlg (prompt: string , filter:any, multiSelect: Boolean ): File

Core JavaScript Classes

Opens the built-in platform-specific file-browsing dialog, in which the user can select an existing file or files, and creates new File objects to represent the selected files.

Differs from the class method openDialog() in that it presets the current folder to this File object’s parent folder and the current file to this object’s associated file.

If the user clicks OK, returns a File or Folder object for the selected file or folder, or an array of objects.

If the user cancels, returns null.

prompt: Data Type: string

A string containing the prompt text, if the dialog allows a prompt.

filter (optional): Data Type: any, Default Value: null

A filter that limits the types of files displayed in the dialog.

In Windows, a filter expression such as "Javascript files:*.jsx;All files:*.*".

In Mac OS, a filter function that takes a File instance and returns true if the file should be included in the display, false if it should not.

multiSelect (optional): Data Type: Boolean , Default Value: false

When true, the user can select multiple files and the return value is an array.

prema56615405
Inspiring
June 24, 2019

I have used the below script to open the multiple files from a folder. It shows below error attached.

#target illustrator

var destFolder, myFile, files, fileType, sourceDoc;

myFile = File.openDialog("prompt", undefined, true);

if(myFile != null)

    files = new Array(); 

    fileType = "*.ai"; 

    files = myFile.getFiles(fileType); 

    if ( files.length > 0 ) 

    { 

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

        { 

          sourceDoc = app.open(files); 

          sourceDoc.close(); 

        } 

        alert( 'Files are saved as PDF'); 

    } 

    else 

    { 

        alert( 'No matching files found' ); 

    } 

}

renél80416020
Inspiring
June 24, 2019

Salut!

essai ce script, tu comprendras peut-être ?

alert(File.openDialog("Sélectionnez un ou plusieurs fichiers ?","All files:*.*", true));

La documentation est dans Javascript Tools guide

LR