Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. // Shortcuts
  2. var doc = app.activeDocument;
  3. var selection = doc.selection;
  4.  
  5. function main() {
  6.  
  7. var outputFolder = Folder.selectDialog( 'Select folder for extracted objects.', '~' );
  8. if(!outputFolder) {
  9. alert('You have to select a folder to save the files to!\nExiting.');
  10. return;
  11. }
  12.  
  13. // Variables to store issues
  14. var errors = [];
  15.  
  16. // Loop through all selected items
  17. var maxIterations = selection.length;
  18. while(maxIterations--) {
  19. var obj = selection[0];
  20.  
  21. var succeeded = false;
  22. // Only SymbolItems can be extracted
  23. if(obj instanceof SymbolItem) {
  24. succeeded = extractObject(obj, outputFolder);
  25. }
  26. else {
  27. obj.selected = false;
  28. }
  29.  
  30. if(!succeeded) {
  31. if(!confirm('Last symbol extraction was unsuccessful, do you want to continue?\n' +
  32. '(The program might crash if you do so)')) {
  33. break;
  34. }
  35. }
  36. }
  37.  
  38. }
  39.  
  40.  
  41. function extractObject(obj, folder) {
  42.  
  43. var newDocument = app.documents.add(DocumentColorSpace.RGB);
  44. var newLayer;
  45.  
  46. // Use first layer if there is one, otherwise, create a new one
  47. if(newDocument.layers) {
  48. newLayer = newDocument.layers[0];
  49. }
  50. else {
  51. newLayer = newDocument.layers.add();
  52. }
  53.  
  54. // Move the selected object to the layer in the new document
  55. obj.move(newLayer, ElementPlacement.PLACEATBEGINNING);
  56.  
  57. // Make sure the object is selected in its new document
  58. obj.selected = true;
  59.  
  60. // Resize the artboard based on the object
  61. try {
  62. newDocument.fitArtboardToSelectedArt(0);
  63. }
  64. catch(e) {
  65. }
  66.  
  67. // Save the file
  68. var outputFilename = folder + '/' + obj.symbol.name + '.ai';
  69. try {
  70. newDocument.saveAs(new File(outputFilename));
  71. }
  72. catch(e) {
  73. alert('Saving of file "' + outputFilename + '" failed\n' +
  74. 'Make sure a folder named "output" exists on your desktop!');
  75. return false;
  76. }
  77.  
  78. // Change focus to the original document
  79. try {
  80. doc.activate();
  81. // This causes Illustrator to crash, for no apparent reason.
  82. // newDocument.close();
  83. }
  84. catch(e) {
  85. alert('Focusing original document failed\n' +
  86. 'This program has only been tested in Illustrator CS6');
  87. return false;
  88. }
  89.  
  90. return true;
  91.  
  92. }
  93.  
  94.  
  95. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement