Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement