Skip to content

Instantly share code, notes, and snippets.

@joonaspaakko
Last active December 30, 2023 10:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save joonaspaakko/df2f9e31bdb365a6e5df to your computer and use it in GitHub Desktop.
Save joonaspaakko/df2f9e31bdb365a6e5df to your computer and use it in GitHub Desktop.
Finds all Illustrator files from the input folder + its subfolders and converts them to an older version.
// https://gist.github.com/joonaspaakko/df2f9e31bdb365a6e5df
// Finds all .ai files from the input folder + its subfolders and converts them to the version given below in a variable called "targetVersion"
// Tested in Illustrator cc 2014 (Mac)
// Didn't bother to do a speed test with my macbook air...
#target illustrator
// If set to false, a new file will be written next to the original file.
// The new file will have (legacyFile) in the name.
// Files with (legacyFile) in the file name are always ignored.
var overwrite = true, // boolean
// Accepted values:
// 8, 9, 10, 11 (cs), 12 (cs2), 13 (cs3), 14 (cs4), 15 (cs5), 16 (cs6), 17 (cc)
targetVersion = 13;
if ( app.documents.length > 0 ) {
alert("ERROR: \n Close all documents before running this script." );
}
// Run the script
else {
var files,
folder = Folder.selectDialog("Input folder...");
// If folder variable return null, user most likely canceled the dialog or
// the input folder and it subfolders don't contain any .ai files.
if ( folder != null ) {
// returns an array of file paths in the selected folder.
files = GetFiles( folder );
// This is where things actually start happening...
process( files );
}
}
function process( files ) {
// Loop through the list of .ai files:
// Open > Save > Close > LOOP
for ( i = 0; i < files.length; i++ ) {
// Current file
var file = files[i]
// Open
app.open( file );
// If overwrite is false, create a new file, otherwise use "file" variable;
file = !overwrite ? new File( file.toString().replace(".ai", " (legacyFile).ai") ) : file;
// Save
app.activeDocument.saveAs( file, SaveOptions_ai() )
// Close
app.activeDocument.close( SaveOptions.DONOTSAVECHANGES );
}
// For better of for worse...
alert( "Script is done." );
}
function SaveOptions_ai() {
var saveOptions = new IllustratorSaveOptions();
saveOptions.compatibility = Compatibility[ "ILLUSTRATOR" + targetVersion ];
saveOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
saveOptions.compressed = true; // Version 10 or later
saveOptions.pdfCompatible = true; // Version 10 or later
saveOptions.embedICCProfile = true; // Version 9 or later
saveOptions.embedLinkedFiles = false; // Version 7 or later
return saveOptions
}
function GetFiles( folder ) {
var i, item,
// Array to store the files in...
files = [],
// Get files...
items = folder.getFiles();
// Loop through all files in the given folder
for ( i = 0; i < items.length; i++ ) {
item = items[i];
// Find .ai files
var fileformat = item.name.match(/\.ai$/i),
legacyFile = item.name.indexOf("(legacyFile)") > 0;
// If item is a folder, check the folder for files.
if ( item instanceof Folder ) {
// Combine existing array with files found in the folder
files = files.concat( GetFiles( item ) );
}
// If the item is a file, push it to the array.
else if ( item instanceof File && fileformat && !legacyFile ) {
// Push files to the array
files.push( item );
}
}
return files;
}
@mds
Copy link

mds commented Oct 12, 2015

Just used this. Worked like a charm. Thanks!

@FreeSpirit63
Copy link

Just used this. Worked like a charm. Thanks!

@giripappaiya
Copy link

Aswome script.

If any possible to provide higher version pdf's to lower version illustrator file script.

@joonaspaakko
Copy link
Author

@giripappaiya, something like this, I guess. If you need more PDF save options, you can find them in the javascript reference pdf (starting from page 144), and you put them in the SaveOptions_pdf() function in the code.

@strathmoredesigns
Copy link

I tried this and it worked great. Any chance there's a way to tweak it so that it could be used to save newer EPS files as legacy AI files? (I should also note that I just discovered the concept of Illustrator scripts today and have no idea how to create or modify them :))

@joonaspaakko
Copy link
Author

@strathmoredesigns, should work if you change line 101 to say .eps where it now says .ai.

@strathmoredesigns
Copy link

@joonaspaakko I just tried this and it works! Thank you so much for creating it and walking me through my first Javascript edit :)

@giripappaiya
Copy link

I need script for illustrator 2024 ai files to convert illustrator 2022 script. please help me

@joonaspaakko
Copy link
Author

@giripappaiya, yea, the script stops listing targetVersions after CC, which obviously doesn't refer to every single CC version, just the first one. I don't know if there is a better list, but you can find them in Wikipedia: Illustrator release history. Starting from CS, the targetVersion is in parentheses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment