Search code examples
javascriptphotoshopextendscript

Photoshop Javascript get selected pathItem?


My dilemma lays in the pathItems (not the file path, but those vector paths in the tab next to the layers).

I want my script to react to which one of several paths is currently selected. Is there any way to get the index or name of the currently selected path in Photoshop through JavaScript (alternatively through something else)?

I have not found any documentation on this and would like to ensure that I haven't just overlooked something obvious.


Solution

  • Here're two functions to get pathItem index and name. If no path is selected getPathIndex() will return -1, getPathName() will throw an error, handle it how you want. Note that if several paths are selected, the function will work only on the last one: I don't think it's possible to iterate several selected paths.

    function getPathIndex()
    {
      var ref = new ActionReference();
      ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("TrgP"));
      ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
      var desc = executeActionGet(ref);
      return desc.getInteger(charIDToTypeID("TrgP"));
    }
    
    function getPathName()
    {
      var ref = new ActionReference();
      ref.putProperty(charIDToTypeID("Prpr"), charIDToTypeID("PthN"));
      ref.putEnumerated(charIDToTypeID('Path'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
      try
      {
        var desc = executeActionGet(ref);
      }
      catch (e)
      {
        // handle error
        return '';
      }
      return desc.getString(charIDToTypeID("PthN"));
    }