Skip to main content
prema56615405
Inspiring
Jun 22, 2019 7:10 AM UTC
Answered

Run the script for the selected files

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 is closed to new replies. Start a new post to keep the conversation going.
Developer1009Correct answer
Inspiring
Jun 25, 2019 8:07 AM UTC

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

10 replies

Inspiring
Jun 25, 2019 10:14 AM UTC

renél80416020​

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

Yes, this is the way people can learn.

Translate
renél80416020
Inspiring
Jun 25, 2019 12:38 PM UTC

charur94718007

Merci pour votre réponse.

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

Translate
Inspiring
Jun 25, 2019 1:21 PM UTC

renél80416020​

Wow Are you a teacher?

Translate
Developer1009Correct answer
Inspiring
Jun 25, 2019 8:07 AM UTC

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

Translate
renél80416020
Inspiring
Jun 25, 2019 9:36 AM UTC

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

Translate
CarlosCanto
Community Expert
Community Expert
Jun 22, 2019 6:51 PM UTC

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.

Translate
prema56615405
Inspiring
Jun 24, 2019 8:22 AM UTC

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

    } 

}

Translate
renél80416020
Inspiring
Jun 24, 2019 1:22 PM UTC

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

Translate