Skip to content

Instantly share code, notes, and snippets.

@magnusdahlstrand
Last active September 10, 2023 14:03
Show Gist options
  • Save magnusdahlstrand/5053277 to your computer and use it in GitHub Desktop.
Save magnusdahlstrand/5053277 to your computer and use it in GitHub Desktop.
Script for splitting selected objects from one Illustrator file into multiple Illustrator (.ai) files.

#Extract Selections as Documents

Script for splitting selected objects from one Illustrator file into multiple Illustrator (.ai) files.

##Installation Save the file to /Applications/Adobe Illustrator CS6/Presets/YOUR_LOCALE/Scripts, restart Illustrator.

##Usage Select the objects you want to export. Only objects of type SymbolItems are supported at this moment. Run the script from the script menu (File->Scripts->ExtractSelectionsAsDocuments). You'll get prompted to select an output folder.

Tested with Illustrator CS6 using an .fxg source file exported from Flash CS6.

##License

Copyright (c) 2013 Magnus Dahlstrand

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// Shortcuts
var doc = app.activeDocument;
var selection = doc.selection;
function main() {
var outputFolder = Folder.selectDialog( 'Select folder for extracted objects.', '~' );
if(!outputFolder) {
alert('You have to select a folder to save the files to!\nExiting.');
return;
}
// Variables to store issues
var errors = [];
// Loop through all selected items
var maxIterations = selection.length;
while(maxIterations--) {
var obj = selection[0];
var succeeded = false;
// Only SymbolItems can be extracted
if(obj instanceof SymbolItem) {
succeeded = extractObject(obj, outputFolder);
}
else {
obj.selected = false;
}
if(!succeeded) {
if(!confirm('Last symbol extraction was unsuccessful, do you want to continue?\n' +
'(The program might crash if you do so)')) {
break;
}
}
}
}
function extractObject(obj, folder) {
var newDocument = app.documents.add(DocumentColorSpace.RGB);
var newLayer;
// Use first layer if there is one, otherwise, create a new one
if(newDocument.layers) {
newLayer = newDocument.layers[0];
}
else {
newLayer = newDocument.layers.add();
}
// Move the selected object to the layer in the new document
obj.move(newLayer, ElementPlacement.PLACEATBEGINNING);
// Make sure the object is selected in its new document
obj.selected = true;
// Resize the artboard based on the object
try {
newDocument.fitArtboardToSelectedArt(0);
}
catch(e) {
}
// Save the file
var outputFilename = folder + '/' + obj.symbol.name + '.ai';
try {
newDocument.saveAs(new File(outputFilename));
}
catch(e) {
alert('Saving of file "' + outputFilename + '" failed\n' +
'Make sure a folder named "output" exists on your desktop!');
return false;
}
// Change focus to the original document
try {
doc.activate();
// This causes Illustrator to crash, for no apparent reason.
// newDocument.close();
}
catch(e) {
alert('Focusing original document failed\n' +
'This program has only been tested in Illustrator CS6');
return false;
}
return true;
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment