Copy link to clipboard
Copied
Does anyone know of a way within AI CS4 to export layers to separate .ai files? Or third party plug-in to do the same? Preferably retaining the layer name as the new file name.
I have a layered .ai file with 40 layers of logo versions that I need to export to separate files and I'd like to avoid the obvious "save as" and delete layers route.
Thanks in advance.
Copy link to clipboard
Copied
Try this. Cut and paste into a text editor and save as a plain text file with a .jsx extension. You can either place it into your AICS4>Presets>Scripts folder and restart AI or just run it from File>Scripts>Other Scripts.
//////////////Start
var doc = app.activeDocument;
if (documents.length > 0){
// Create the illusrtratorSaveOptions object to set the AI options
var saveOpts = new IllustratorSaveOptions();
// Setting IllustratorSaveOptions properties.
saveOpts.embedLinkedFiles = true;
saveOpts.fontSubsetThreshold = 0.0
saveOpts.pdfCompatible = true
if (doc.saved==false) doc.save();
for (i=0; i<doc.layers.length; i++)
if (doc.layers.locked == false) doc.layers.visible = false;
fullDocName = doc.fullName;
var param = doc.name.split('.');
realDocName = param[0];
for (i=0; i<doc.layers.length; i++){
if (i-1<0) doc.layers.visible = true;
else {
doc.layers[i-1].visible = false;
doc.layers.visible = true;
}
if (doc.layers.locked == false) {
docName = realDocName+doc.layers.name+".ai";
var saveName = new File ( doc.path + "/" + docName );
doc.saveAs( saveName, saveOpts );
}
}
doc.close(SaveOptions.DONOTSAVECHANGES);
doc = null;
app.open (fullDocName);
}
/////////End
Copy link to clipboard
Copied
That worked great. Thank you so much. The only tweak I made was to eliminate the "realDocName+" from the "docName=" line so that the exported file name was unique to the layer name and not a composite of the target file and resulting file.
The Larry G. Schneider Fan Club is being launched as I type.
Danke.
Copy link to clipboard
Copied
Art&Logik wrote: "The Larry G. Schneider Fan Club is being launched as I type."
Sign me up!
Copy link to clipboard
Copied
Actually, I just discovered that the resulting files still had all of the original layers in it. Is there code to delete the non-visible layers and retain only the visible one?
Thanks for any help.
Copy link to clipboard
Copied
Are you working on a Mac or a PC? If a Mac I have another script which will do that. I'll need a little while to figure out how to translate it to JS.
Copy link to clipboard
Copied
Mac. Mucho Gracias.
Copy link to clipboard
Copied
Since in AI scripting you can't delete a hidden layer, this reverses the visibility of the layers and then deletes the visible layers. It then makes all layers visible and saves the file.
--------Start
--get a sourceFolder that holds the files to process
set sourceFolder to (choose folder with prompt "Choose a folder with files to process:") as text
-- get a list of files of the files to process in the sourceFolder
tell application "Finder" to set workingFiles to (every file of folder sourceFolder) as alias list
repeat with aFile in workingFiles
tell application "Adobe Illustrator"
open aFile
set currentFile to front document
tell application "Adobe Illustrator"
set myLayers to every layer of currentFile
repeat with i from 1 to count of items in myLayers
set myLayer to item i of myLayers
set locked of myLayer to false
if visible of myLayer is true then
set visible of myLayer to false
else if visible of myLayer is false then
set visible of myLayer to true
end if
end repeat
delete (every layer of currentFile whose visible is true)
end tell
tell application "Adobe Illustrator"
set visible of (every layer of currentFile) to true
end tell
close currentFile with saving
end tell
end repeat
--------End
Copy link to clipboard
Copied
Again, my thanks. I'll give 'er a go.
Copy link to clipboard
Copied
Were you guys ever able to figure out how to delete the hidden layers?
Is there a way to "flatten the document" with scipt?
I can't get it to work and it's killing me.
Thanks!
Copy link to clipboard
Copied
I couldn't get a script to work, so I recorded an action to flatten the layers and batch processed the files. Worked great. Good luck.
Copy link to clipboard
Copied
Did you find a way to go around the prompt about discarding hidden layers? I couldn't get rid of this dialog and the action became useless, I still need to click ok each time a document is flattened.
Copy link to clipboard
Copied
i wasn't able to get the second script to work (the one that should remove the layers that don't belong in the new file).
i don't know much about scripting, but the first file doesn't look anything like the second..
and this is the error it gives.
may i get help on this script to save only the layer information per the new file it's creating?
@tpf1952.. it worked for you?
thank you if anyone can help!!!!!
Copy link to clipboard
Copied
The script you show in the right hand window is an AppleScript that will only work on a Mac. It should be placed in the Apple Script editor. You've placed it in the ESTK which is only used for ExtendScript (Adobe's implementation of Java Script).
Copy link to clipboard
Copied
i'm sorry. i will be slow to figure this out probably. lol. thank you for responding.
i am on a mac.
i opened the apple script program? and it will not allow me to save this file.
Copy link to clipboard
Copied
Just do the same thing you just did but now hit the Compile button ( the hammer), then try to save.
Copy link to clipboard
Copied
I never heard of scripts until now. Are they hard to do? I was wanting to do exactly what this script did, but instead of saving as to an .ai, I wanted to export as a psd at:
300dpi
flat image
anti aliasing checked
imbed ICC profile UNchecked
Is this possible to do?
If you could do this or show me where to learn that would be awesome.
Thank you thank you thank you.
Justin
Copy link to clipboard
Copied
I haven't laughed this hard in quite some time. I no longer get frustrated with Adobe's mysterious choices in arbitrarily blanking fundamental capability. It's just entertaining now. Someone asks for a way to export layers as separate files and gets back a page of code and advice on how to develop scripts? Hysterical!! I've never seen anything so funny. It's going in my next screenplay. By the way, if your mechanic did this to you you'd take her to small claims. (laughter echoes on)
Copy link to clipboard
Copied
if your mechanic did this to you you'd take her to small claims.
Actually, if my mechanic could fix my vehicle with Javascript, I'd take him to lunch and pick his brain.
JET
Copy link to clipboard
Copied
Hello Larry. I'm attempting to use this script in AICC2020 and getting the below error. Looks like this script is 10 years old. Any idea if it should still work or what might be causing my error? Thanks!
Copy link to clipboard
Copied
Hi,
It may possible while switching old threads to new forum, some of the code get corrupted. Here is the correct version of the script
var doc = app.activeDocument;
if (documents.length > 0) {
// Create the illusrtratorSaveOptions object to set the AI options
var saveOpts = new IllustratorSaveOptions();
// Setting IllustratorSaveOptions properties.
saveOpts.embedLinkedFiles = true;
saveOpts.fontSubsetThreshold = 0.0
saveOpts.pdfCompatible = true
if (doc.saved == false) doc.save();
for (i = 0; i < doc.layers.length; i++)
if (doc.layers[i].locked == false) doc.layers[i].visible = false;
fullDocName = doc.fullName;
var param = doc.name.split('.');
realDocName = param[0];
for (i = 0; i < doc.layers.length; i++) {
if (i - 1 < 0) doc.layers[i].visible = true;
else {
doc.layers[i - 1].visible = false;
doc.layers.visible = true;
}
if (doc.layers[i].locked == false) {
docName = realDocName + doc.layers[i].name + ".ai";
var saveName = new File(doc.path + "/" + docName);
doc.saveAs(saveName, saveOpts);
}
}
doc.close(SaveOptions.DONOTSAVECHANGES);
doc = null;
app.open(fullDocName);
}
Let us know if this works for you.
Copy link to clipboard
Copied
This script resolved the error I was having and now works. Thanks!
Copy link to clipboard
Copied
Hi, Charu I ran the script but I don't know where the files are saving?
Copy link to clipboard
Copied
Hi,
The files are storing at the same location where your ai file is stored. For an example: If you store your AI file at your desktop, then all files created by above script will also store at the desktop.
Copy link to clipboard
Copied
Hi, I'm trying this script on Ai2021 to save a character's elements for animation, it runs alright saving a file for each layer but only the top layer's exported file has visible contents, every other file has them all hidden... not sure if I'm doing something wrong (all my document layers are visible and unlocked)