Creating New Actions - OptrixAU/xltoadobe GitHub Wiki

XlToAdobe allows you to create your own actions.

If you do make one that might be useful to others, we encourage you to submit them back to the project so that more people can use it.

You create new actions by adding Python classes to the directory named after the platform you are supporting. For example, InDesign actions should appear in the indesign folder.

Naming the File and Class

Each file should be named after the Action. In this case, we are going to create an InDesign action to change the paragraph type of a given layer. We will call the action ParagraphStyle.

This will be created in indesign/paragraphstyle.py.

Creating the Class

In the file, we define our class, which is called ActionNameScriptGen.

class ParagraphStyleScriptGen:
    
    def __init__(self, args):
        # Store action parameters here.
        pass    

    def GenerateScript(self, arg2):        
        # Return the created script lines
        return ""

Generating Script

In this case, the steps required are fairly easy.

The Javascript to change the paragraph style of a specific layer (in this case, 'MyLayer') is below...

var lyr = app.activeDocument.layers.item('MyLayer');
lyr.textFrames[0].appliedParagraphStyle=app.activeDocument.paragraphStyles.item('MyParagraphStyle');

First, we store the name of the layer the user wants to interact with in the init code, with self.layername = args;

Finally, in GenerateScript, we return the new Javascript we want to add to the overall script file.

class ParagraphStyleScriptGen:
    
    def __init__(self, args):
        self.layername = args;
        pass    

    def GenerateScript(self, arg2):        
        Script = "var lyr = app.activeDocument.layers.item('" + self.layername + "');\n"
        Script += "lyr.textFrames[0].appliedParagraphStyle=app.activeDocument.paragraphStyles.item('" + arg2 + "');\n"
        return Script