Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Script works except when run as an action

Community Beginner ,
Oct 09, 2024 Oct 09, 2024

I modified some code found online to faciliate exporting illustrator files to PSDs while retaining layers. I use the layers of illustrator like photoshop groups, and want each sub-layer to be its own separate layer once exported as a PSD. Unfortunately it seems the only way to ensure that works is to group each sub-layer by itself. I'm working with LaTeX equations and need each element to be separate, and depending on the complexity there can be many sub-layers, so manually grouping each one was tedious.

 

You can find the illustrator test document and .jsx code here to show what I mean. When I drag the .jsx file onto the document, it works, grouping each sub-layer by itself and renaming them to their previous name, and if I put it in illustrators script folder (C:\Program Files\Adobe\Adobe Illustrator 2024\Presets\en_US\Scripts) it shows up after restarting illustrator and I can run it from the File>Scripts menu just fine. However, I wanted to make an action with a hotkey so I could just press F3 and have it execute that script. My understanding is that you make an action, and use Insert Menu Item to choose the script put in the Scripts directory, however I try executing the action and it doesn't give the same result, usually just renaming the topmost layer.

 

Is there something about the code or actions I'm missing as to why it works everywhere else except as an action? Any help is appreciated, thanks!

 

var doc = app.activeDocument;
doc.selection = null;
var thisLayer;
var layerName;
for(var i = 0; i < doc.layers.length; i++){
    thisLayer = doc.layers[i];
	
    var thisPageItem;
    for(var j = 0; j < thisLayer.pageItems.length; j++){
        thisPageItem = thisLayer.pageItems[j];
        if(thisPageItem.typename != "GroupItem"){
			
            thisPageItem.selected = true;
			layerName = thisPageItem.name;
			app.executeMenuCommand("group");
			thisPageItem.parent.name = layerName;
			app.activeDocument.selection = null; //deselect
        }
    }
 
    doc.selection = null;
}

 

The illustrator test document and code file are here.

TOPICS
Scripting
248
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Oct 09, 2024 Oct 09, 2024

I am unfamiliar with running scripts from actions, but my theory is that because the action is running the script there is a problem with illustrator updating. I tried this on a whim and it seemed to work.

Add app.redraw() after the item being selected. = true. This will force illustrator to redraw and it now notices the item is selected and therefore the group command will work and the renaming will work.

Translate
Adobe
Participant ,
Oct 09, 2024 Oct 09, 2024

I am unfamiliar with running scripts from actions, but my theory is that because the action is running the script there is a problem with illustrator updating. I tried this on a whim and it seemed to work.

Add app.redraw() after the item being selected. = true. This will force illustrator to redraw and it now notices the item is selected and therefore the group command will work and the renaming will work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 09, 2024 Oct 09, 2024
LATEST

I tried it with that additional line and it works now, thank you for your help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines