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;
}
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 {
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.
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' );
}
}
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
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
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
Copy link to clipboard
Copied
I am so sorry, I did not know about your intention. So sorry.
Yes, this is the way people can learn.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Wow Are you a teacher?
Copy link to clipboard
Copied
Thank you renél80416020and charur94718007.
Actually I am in the basic level of javascript. Don't have deep knowledge in Javascript.
Copy link to clipboard
Copied
No worries, you will learn a lot on forums as we have good teachers available here
Thanks all experts