How to apply transformation to selection in Illustrator by scripting

别来无恙 提交于 2021-02-10 18:40:59

问题


I managed to select every thing I want in Illustrator with a ExtendScript Toolkit javascript code: lots of things (text, path, symbols, ...) in several layers. Now I want to resize them the same way, then move them.

I know how to apply a transformation to one object by code, but I want to avoid looping on each element because it would be very very long and transformation would be applied from the anchor point of each element, so my drawings wouldn't be cohesive.

So, I am looking for a way to do like in Illustrator UI: right click > transform > scale. Is there any UI command like this I could access from code (javascript)?


回答1:


There are at least three ways to do this:

  1. Record AI Actions that perform required transformations, and then play these Actions (by DoScript) from your script
  2. Group selected objects and apply required transformations to the group as @Dane proposed. You need to backup the Layer object property to allow objects to be restored in original layers, as shown in VBA example below:

    For i = Index_Lo To Index_Hi
        Call Layers_Backup.Add(Item:=SelectedItems(i).Layer, Key:=Format(i))
        Call SelectedItems(i).Move(Temp_Group, AiElementPlacement.aiPlaceAtEnd)
    Next i
    
    Call Temp_Group.Resize(scaleX:=120, scaleY:=120, changeLineWidths:=120)
    
    For i = Index_Lo To Index_Hi
        Call SelectedItems(i).Move(Layers_Backup(Format(i)), AiElementPlacement.aiPlaceAtEnd)
    Next i
    
  3. Call Windows API functions (like PostMessage (..., WM_COMMAND, ..., ...), SendDlgItemMessage, FindWindowEx etc) to show, fill and execute required AI transformations dialog boxes

IMHO, item #1 is the easiest to implement while item#2 is the most reliable




回答2:


So I dont know if you can get away with not looping in some form or other. that said, if you dont mind putting your selection in to a group, it might be faster to loop through your selection adding to a group which might be faster than looping and through the selection and scale and moving each element. With the group object you can then do groupObject.scale() and .translate(). Here is a snippet i took from a script of mine.

#target "illustrator"
var aiApp = app.activeDocument;
var aSelection = aiApp.selection;
var aGroup = app.activeDocument.groupItems.add();

for (i=0; i < aSelection.length; i++){
aSelection[i].move( aGroup, ElementPlacement.INSIDE); 
aSelection[i].move( aGroup, ElementPlacement.PLACEATEND);  
 }
//moves up 700 points and scales by 200
aGroup.translate(0,700)
aGroup.resize(200, 200, true , true, true, true, 200)


来源:https://stackoverflow.com/questions/47758185/how-to-apply-transformation-to-selection-in-illustrator-by-scripting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!