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

Run the script for the selected files

Explorer ,
Jun 22, 2019 Jun 22, 2019

Copy link to clipboard

Copied

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;

}

TOPICS
Scripting

Views

3.2K

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

correct answers 1 Correct answer

Contributor , Jun 25, 2019 Jun 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 {

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 22, 2019 Jun 22, 2019

Copy link to clipboard

Copied

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.

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
Explorer ,
Jun 24, 2019 Jun 24, 2019

Copy link to clipboard

Copied

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

    } 

}

Screenshot 2019-06-24 at 10.15.42.png

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
Advocate ,
Jun 24, 2019 Jun 24, 2019

Copy link to clipboard

Copied

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

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
Contributor ,
Jun 25, 2019 Jun 25, 2019

Copy link to clipboard

Copied

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

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
Advocate ,
Jun 25, 2019 Jun 25, 2019

Copy link to clipboard

Copied

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

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
Contributor ,
Jun 25, 2019 Jun 25, 2019

Copy link to clipboard

Copied

renél80416020

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

Yes, this is the way people can learn.

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
Advocate ,
Jun 25, 2019 Jun 25, 2019

Copy link to clipboard

Copied

charur94718007

Merci pour votre réponse.

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

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
Contributor ,
Jun 25, 2019 Jun 25, 2019

Copy link to clipboard

Copied

renél80416020

Wow Are you a teacher?

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
Explorer ,
Jun 25, 2019 Jun 25, 2019

Copy link to clipboard

Copied

Thank you renél80416020​and charur94718007​.

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

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
Contributor ,
Jun 25, 2019 Jun 25, 2019

Copy link to clipboard

Copied

LATEST

No worries, you will learn a lot on forums as we have good teachers available here

Thanks all experts

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