Skip to content

Inventsable/Redesigning-Illustrator-Actions-in-Scripting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using actions with scripting can be a pain. Exported actions will look like this:

/version 3
/name [ 11
	436f6c6f725069636b6572
]
/isOpen 1
/actionCount 2
/action-1 {
	/name [ 6
		736574524742
	]
	/keyIndex 7
	/colorIndex 2
	/isOpen 0
	/eventCount 1
	/event-1 {
		/useRulersIn1stQuadrant 0
		/internalName (ai_plugin_setColor)
		/localizedName [ 9
			53657420636f6c6f72
		]
		/isOpen 0
		/isOn 1
		/hasDialog 0
		/parameterCount 6
		/parameter-1 {
			/key 1768186740

Certain values in the above (with square brackets like /name and /localizedName) are Hexadecimal to ASCII:

"436f6c6f725069636b6572"  ==>  "ColorPicker"
"53657420436f6c6f72"      ==>  "Set Color"
"53657420636f6c6f72"      ==>  "Set color"
"46696c6c20636f6c6f72"    ==>  "Fill color"
"52474220636f6c6f72"      ==>  "RGB color"

While others like /key will be Decimal to Hexadecimal to ASCII:

"1768186740"  ==>  "idct"
"1718185068"  ==>  "fill"
"1954115685"  ==>  "type"
"1919247406"  ==>  "red."
"1735550318"  ==>  "gren"
"1651275109"  ==>  "blue"

To run one without cluttering the user's Actions panel:

// We have to manually grab the contents of the file
var tmp = File(Folder.desktop + "/someActionSet.aia");
tmp.encoding = "UTF8";
tmp.open("r");
tmp.read();

// Then load it and close the file:
app.loadAction(tmp);
tmp.close();

// While remembering any hard-coded values like action name and set name:
app.doScript("export300dpiJPEG", "whateverMySetWasNamed", false);

// And when done, unload whatever temporary set we'd made:
app.unloadAction("whateverMySetWasNamed", "");

The above isn't that bad or inconvenient, but it demonstrates the only 3 interactions that scripting and actions have: whether to load, unload, or execute obscurely formatted text. Actions are a core part of the app to any user so you would think scripting them could be a lot more elegant:

// I should be able to load actions easily from a given file:
var mySet = new ActionSet(new File(Folder.desktop + "/someActionSet.aia"));

// And then just run one instantly:
mySet.actions.run("export300dpiJPEG");

// If I want to load them and save this set for the user:
mySet.load(); // it should be this easy

And if each action were similar to PageItem, all their properties would be exposed:

var myExportAction = mySet.actions.getByName("export300dpiJPEG");
myExportAction: {
  "events": [[Event ExportAs], [Event SaveAs]],
  "name": "export300dpiJPEG",
  "parent": [ActionSet SuperUsefuls],
  "typename": "Action",
  "version": 3,
}

Which could allow you to create dynamic actions on the fly:

var myAction = new Action("recolorit");
myAction.events.add(
  new ActionEvent({
    localizedName: "selectAll",
    internalName: "(adobe_selectAll)",
    hasDialog: 0,
  }),
  new ActionEvent({
    localizedName: "paint it red",
    internalName: "(adobe_plugin_setColor)",
    parameters: [
      new ActionParam({ key: "Set color", value: "idct" }),
      new ActionParam({ key: "Fill color", value: "fill" }),
      new ActionParam({ key: "type", value: "RGB color" }),
      new ActionParam({ key: "red.", value: 255 }),
      new ActionParam({ key: "gren", value: 50 }),
      new ActionParam({ key: "blue", value: 0 }),
    ],
  })
);
myAction: {
  "events": [[Event selectAll], [Event paint-it-red]],
  "name": "recolorit",
  "parent": null,
  "typename": "Action",
  "version": 3,
}

Add them into any set and run it:

mySet.actions.add(myAction);
myAction.run();

Then save this new action set to a file:

mySet.saveAs(new File("~/desktop/setColorSet.aia"));

But if we're parsing AIA anyway, we could use JSON instead:

mySet.saveAs(new File("~/desktop/setColorSet.json"));

To become more legible and transparent for all parties involved:

{
  "name": "SuperUsefuls",
  "isOpen": 1,
  "actions": [
    {
      "name": "recolorit",
      "events": [
        {
          "localizedName": "selectAll",
          "internalName": "(adobe_selectAll)",
          "hasDialog": 0
        },
        {
          "localizedName": "paint it red",
          "internalName": "(adobe_plugin_setColor)",
          "parameters": [
            { "key": "Set color", "value": "idct" },
            { "key": "Fill color", "value": "fill" },
            { "key": "type", "value": "RGB color" },
            { "key": "red.", "value": 255 },
            { "key": "gren", "value": 50 },
            { "key": "blue", "value": 0 }
          ]
        }
      ]
    }
  ]
}



And that's the idea.

But maybe it's worth trying to design code instead of always jumping in and improvising, so I'm writing out what feels right.



ActionSet

Properties

Key Type Description
actions Actions Array-like collection of Action objects. Entries can be accessed as an Array while simultaneously having functions like ActionSet.actions.add()
loaded Boolean Whether or not the current set has been loaded
name String The name of the current set
rawtext String Text last used to generate this object on creation or ActionSet.toAIA() call
typename String Returns "ActionSet"

Methods

ActionSet.load()Boolean

Load the current set into memory, equivalent to Application.loadAction. Returns truthiness of whether successful.

ActionSet.run([actionName?])Boolean

Run Action contained in current set by name. Returns truthy if successful, falsey if no Action of this name is found.

ActionSet.saveAs(file)Boolean

Saves current set to designated file. Returns truthiness of whether or not successful.

ActionSet.toAIA()String

Converts this set's content to dynamically generated AIA text.

ActionSet.toJSON()String

Converts this set's content to dynamically generated JSON text.

ActionSet.unloadBoolean

Unloads the current ActionSet into memory, equivalent to Application.unloadAction.


Action

Properties

Key Type Description
events Events Array-like collection of Event objects. Entries can be accessed by index while simultaneously including methods like Action.events.add()
isOpen Boolean Whether unfolded in the Actions panel
name String Name of this action
parent ActionSet | null The set containing this action (if any). This prop is rewritten each time this action is exposed to an ActionSet.actions method like add() or remove()
typename String Returns "Action"
version Number Defaults to 3

Methods

Action.load()Boolean

Loads current action into memory, must be included inside valid ActionSet. Returns boolean of whether successful

Action.remove()Boolean

Removes current action within set (if any). Returns boolean of whether successful

Action.run()Boolean

Executes current action, must be included inside valid ActionSet. Returns boolean of whether successful

Action.unload()Boolean

Unloads current action into memory, must be included inside valid ActionSet. Returns boolean of whether successful

About

What Actions native to scripting could look like

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published