Skip to content

Instantly share code, notes, and snippets.

@KyleTryon
Created April 18, 2023 14:56
Show Gist options
  • Save KyleTryon/02f973e94df315b170918a2132bd4ab6 to your computer and use it in GitHub Desktop.
Save KyleTryon/02f973e94df315b170918a2132bd4ab6 to your computer and use it in GitHub Desktop.
Export shapes from a selection in Adobe Illustrator
// This script was generated with ChatGPT4
// How to use:
// 1. Select multiple objects in Adobe Illustrator
// 2. Go to File > Scripts > Other Script...
// 3. Open this script
// 4. Enter the name for the shapes in the prompt
// 5. Select a destination folder
// Shared by: @TechSquidTV
#target Illustrator
function exportShapesAsSVGs() {
var doc = app.activeDocument;
var selectedItems = doc.selection;
var exportOptions = new ExportOptionsSVG();
exportOptions.artboardClipping = true;
exportOptions.saveMultipleArtboards = false;
var type = ExportType.SVG;
if (selectedItems.length === 0) {
alert("No shapes are selected. Please select the shapes you want to export.");
return;
}
var customName = prompt("Enter a custom name for the shapes:", "Shape");
if (!customName) return;
var destFolder = Folder.selectDialog("Choose the destination folder");
if (!destFolder) return;
for (var i = 0; i < selectedItems.length; i++) {
var currentItem = selectedItems[i];
if (!(currentItem instanceof PathItem || currentItem instanceof CompoundPathItem || currentItem instanceof TextFrame)) {
continue;
}
var itemName = currentItem.name || customName + "_" + (i + 1);
// Create a temporary layer to isolate the current item
var tempLayer = doc.layers.add();
tempLayer.name = "Temp Layer for Export";
// Duplicate the current item to the temporary layer
var tempItem = currentItem.duplicate(tempLayer, ElementPlacement.PLACEATBEGINNING);
// Hide all other layers except the temporary layer
for (var j = 0; j < doc.layers.length; j++) {
if (doc.layers[j] !== tempLayer) {
doc.layers[j].visible = false;
}
}
// Create a temporary artboard for the current item
var artboardRect = tempItem.visibleBounds;
var tempArtboard = doc.artboards.add(artboardRect);
tempArtboard.name = "Temp Artboard for Export";
// Find the index of the temporary artboard
var tempArtboardIndex;
for (var j = 0; j < doc.artboards.length; j++) {
if (doc.artboards[j] === tempArtboard) {
tempArtboardIndex = j;
break;
}
}
// Activate the temporary artboard
doc.artboards.setActiveArtboardIndex(tempArtboardIndex);
// Export the isolated item as an SVG
var file = new File(destFolder.fsName + "/" + itemName + ".svg");
try {
doc.exportFile(file, type, exportOptions);
} catch (e) {
alert("Error exporting " + itemName + ": " + e.message);
}
// Remove the temporary artboard and layer, and restore visibility to the other layers
tempArtboard.remove();
tempLayer.remove();
for (var j = 0; j < doc.layers.length; j++) {
doc.layers[j].visible = true;
}
}
alert("SVG export complete.");
}
exportShapesAsSVGs();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment