Convert an Action into UXP Code (Equivalent of `XTools`' `ActionFileToJavaScript`)

I would say that it’s (currently) not possible to load an .atn file in UXP.
Why? Well, here are some thoughts and tests:

Loading a style (.asl) file can be done like this:

const entry = //...getting entry
  if(entry) {
    const token = //...getting token
    const desc = {
      _obj: 'set',
      _target: [
        {
          _ref: 'property',
          _property: 'style',
        },
        {
          _ref: 'application',
          _enum: 'ordinal',
          _value: 'targetEnum',
        },
      ],
      to: {
        _path: token,
        _kind: 'local',
      },
      append: true,
    }
    await photoshop.action.batchPlay([desc],{})
  }

This process is also recordable via Alchemist. Loading .atn files works differently though. Simply replacing the ‘style’ property with ‘action’ does not work. Using a ‘set’-descriptor seems to be specific for importing styles only, well at least it can’t be used for loading actions.

As Alchemist records neither File>Open nor loading an .atn file from the actions panel, I tried to look for extendscript code. Some examples show, that app.load(file) could be used to load .atn files. However, the app object from CEP is not available anymore, instead we got a new app object which only has an open() function. As @ddbell mentioned, this doesn’t seem to open .atn files correctly.

In the forum post I linked above, Mikaeru also provides an ActionManager alternative:

var descriptor = new ActionDescriptor ();
descriptor.putPath (app.stringIDToTypeID ("target"), file);
app.executeAction (app.stringIDToTypeID ("open"), descriptor);

Running this in a .jsx file works fine (you have to get the file first) - the .atn file gets loaded and added to the actions panel correctly.

Converting it to UXP/Batchplay, gives the following descriptor:

{
      _obj: 'open',
      _target: { _path: "C:\Users\Simon\Desktop\test\dist\actions.atn"}
}

If you’d want to use a path like that, you’d probably have to escape the backslashes by adding another one ("C:\\Users\\Simon\\Desktop\\test\\dist\\actions.atn"), however we can’t use direct paths in UXP anyway since file access is based on tokens now. This leads to the following test code:

const entry = //... get Entry
if(entry) {
  const token = //... get Token
  const desc = {
    _obj: 'open',
    _target: { _path: token}
  }
  try {
    const res = photoshop.action.batchPlay([desc],{synchronousExecution: true})
    console.log(res)
  } catch (e) {console.log(e)}
}

This does not work however. It results in the following error (which doesn’t make a lot of sense):

Illegal data type. Undefined is not supported for this operation

I tried it with the path also which obviously failed and also tried to change _target to target.
Interestingly, using target with the path gave me an

invalid file token used

error, which was expected and gave me hope that it would work with the token. Unfortunately it doesn’t - the batchPlay just runs through without any errors, but the action file is not loaded.

2 Likes