Highlighted

How to use a script to run another script from File > Scripts?

Engaged ,
Aug 24, 2020

Copy link to clipboard

Copied

Hi guys. I was looking at this post asking for a toolbar/panel for scripts and thought I'd try my hand at it, but realized shortly in that for a user like this, it could be much more convenient to type in the Menu Item equivalent that appears in File > Scripts for each entry instead of manual drag/dropping of every .jsx file into the panel. In order to do this I'd have to be able to run the items within File > Scripts dynamically from within my own script.

 

I figure the solution would be to create an action which uses Insert Menu Item and dynamically construct the .aia text, load and execute this action then unload it, and following some resources from Silly-V, moluapple and others I'd thought this would be the solution:

 

runMenuItem("sayHello");

function runMenuItem(name) {
  var act = getActionText(name); // get raw .aia content
  var tmp = File(Folder.desktop + "/set10.aia"); // create temp file
  // open and write
  tmp.open("w");
  tmp.write(act);
  tmp.close(); // close temp file
  app.loadAction(tmp); // load then execute the action
  app.doScript("runMenuItem", "temp", false);
  // unload and remove
  app.unloadAction("temp", "");
  tmp.remove();
}

// First construct the action text
function getActionText(name) {
  // Preload necessary functions
  function u16to8(cd) {
    function toHex2(num) {
      var out = "0" + num.toString(16);
      return out.slice(-2);
    }
    var out =
      cd < 0x80
        ? toHex2(cd)
        : (cd < 0x800
            ? toHex2(((cd >> 6) & 0x1f) | 0xc0)
            : toHex2((cd >> 12) | 0xe0) + toHex2(((cd >> 6) & 0x3f) | 0x80)) +
          toHex2((cd & 0x3f) | 0x80);
    return out;
  }

  var str = "";
  // Convert target name to hexadecimal
  for (var i = 0; i < name.length; i++) str += u16to8(name.charCodeAt(i));
  // return raw .aia content
  return (
    "/version 3" +
    "/name [ 4" +
    "	74656d70" +
    "]" +
    "/isOpen 1" +
    "/actionCount 1" +
    "/action-1 {" +
    "	/name [ 11" +
    "		72756e4d656e754974656d" +
    "	]" +
    "	/keyIndex 0" +
    "	/colorIndex 0" +
    "	/isOpen 1" +
    "	/eventCount 1" +
    "	/event-1 {" +
    "		/useRulersIn1stQuadrant 0" +
    "		/internalName (adobe_commandManager)" +
    "		/localizedName [ 16" +
    "			416363657373204d656e75204974656d" +
    "		]" +
    "		/isOpen 0" +
    "		/isOn 1" +
    "		/hasDialog 0" +
    "		/parameterCount 2" +
    "		/parameter-1 {" +
    "			/key 1769238125" +
    "			/showInPalette -1" +
    "			/type (ustring)" +
    "			/value [ 17" +
    "				5f30303030303144443137363033463930" +
    "			]" +
    "		}" +
    "		/parameter-2 {" +
    "			/key 1818455661" +
    "			/showInPalette -1" +
    "			/type (ustring)" +
    "			/value [ " +
    str.length / 2 +
    "				" +
    str +
    "			]" +
    "		}" +
    "	}" +
    "}" +
    ""
  );
}

 

Sorry it still has whitespace, I find it easier to look at in my editor but unsure how it will wrap here in the forum.

 

The above snippet doesn't work. I get an error dialog saying "The object 'sayHello' is not currently available," even though there exists a File > Scripts > sayHello menu item that prompts an alert reading "Hello". In the least it doesn't silently fail and does recognize the dynamic text "sayHello" being injected. Can any one spot what's wrong here? I thought all I needed to do was convert the string to hexadecimal and replace the value with the hex length divided by two.

TOPICS
Scripting

Views

57

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to use a script to run another script from File > Scripts?

Engaged ,
Aug 24, 2020

Copy link to clipboard

Copied

Hi guys. I was looking at this post asking for a toolbar/panel for scripts and thought I'd try my hand at it, but realized shortly in that for a user like this, it could be much more convenient to type in the Menu Item equivalent that appears in File > Scripts for each entry instead of manual drag/dropping of every .jsx file into the panel. In order to do this I'd have to be able to run the items within File > Scripts dynamically from within my own script.

 

I figure the solution would be to create an action which uses Insert Menu Item and dynamically construct the .aia text, load and execute this action then unload it, and following some resources from Silly-V, moluapple and others I'd thought this would be the solution:

 

runMenuItem("sayHello");

function runMenuItem(name) {
  var act = getActionText(name); // get raw .aia content
  var tmp = File(Folder.desktop + "/set10.aia"); // create temp file
  // open and write
  tmp.open("w");
  tmp.write(act);
  tmp.close(); // close temp file
  app.loadAction(tmp); // load then execute the action
  app.doScript("runMenuItem", "temp", false);
  // unload and remove
  app.unloadAction("temp", "");
  tmp.remove();
}

// First construct the action text
function getActionText(name) {
  // Preload necessary functions
  function u16to8(cd) {
    function toHex2(num) {
      var out = "0" + num.toString(16);
      return out.slice(-2);
    }
    var out =
      cd < 0x80
        ? toHex2(cd)
        : (cd < 0x800
            ? toHex2(((cd >> 6) & 0x1f) | 0xc0)
            : toHex2((cd >> 12) | 0xe0) + toHex2(((cd >> 6) & 0x3f) | 0x80)) +
          toHex2((cd & 0x3f) | 0x80);
    return out;
  }

  var str = "";
  // Convert target name to hexadecimal
  for (var i = 0; i < name.length; i++) str += u16to8(name.charCodeAt(i));
  // return raw .aia content
  return (
    "/version 3" +
    "/name [ 4" +
    "	74656d70" +
    "]" +
    "/isOpen 1" +
    "/actionCount 1" +
    "/action-1 {" +
    "	/name [ 11" +
    "		72756e4d656e754974656d" +
    "	]" +
    "	/keyIndex 0" +
    "	/colorIndex 0" +
    "	/isOpen 1" +
    "	/eventCount 1" +
    "	/event-1 {" +
    "		/useRulersIn1stQuadrant 0" +
    "		/internalName (adobe_commandManager)" +
    "		/localizedName [ 16" +
    "			416363657373204d656e75204974656d" +
    "		]" +
    "		/isOpen 0" +
    "		/isOn 1" +
    "		/hasDialog 0" +
    "		/parameterCount 2" +
    "		/parameter-1 {" +
    "			/key 1769238125" +
    "			/showInPalette -1" +
    "			/type (ustring)" +
    "			/value [ 17" +
    "				5f30303030303144443137363033463930" +
    "			]" +
    "		}" +
    "		/parameter-2 {" +
    "			/key 1818455661" +
    "			/showInPalette -1" +
    "			/type (ustring)" +
    "			/value [ " +
    str.length / 2 +
    "				" +
    str +
    "			]" +
    "		}" +
    "	}" +
    "}" +
    ""
  );
}

 

Sorry it still has whitespace, I find it easier to look at in my editor but unsure how it will wrap here in the forum.

 

The above snippet doesn't work. I get an error dialog saying "The object 'sayHello' is not currently available," even though there exists a File > Scripts > sayHello menu item that prompts an alert reading "Hello". In the least it doesn't silently fail and does recognize the dynamic text "sayHello" being injected. Can any one spot what's wrong here? I thought all I needed to do was convert the string to hexadecimal and replace the value with the hex length divided by two.

TOPICS
Scripting

Views

58

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 24, 2020 0
Engaged ,
Aug 24, 2020

Copy link to clipboard

Copied

Okay, I did a few tests and noticed that my constructed .aia is identical but sometimes differs from an exported action in one place: event-1 > parameter-1 > value. When I try converting this to string from hexadecimal both the key and value arguments look to be encoded? I'm not sure what this part is. Even when I copy/paste from an exported version into my dynamic one, I still get the object not currently available prompt.

 

In fact, is this not possible? When I try with the exact same text as an exported .aia without modifying or constructing anything, I still get this result. Am I doing something wrong? How could it be that you can't run a particular action from a script if this can exist as .aia and be loaded/executed?

 

Below is the same text as an exported .aia file and still gives the object not currently available error:

 

runMenuItem("sayHello");

function getFakeText() {
  return (
    "/version 3" +
    "/name [ 4" +
    "	74656d70" +
    "]" +
    "/isOpen 1" +
    "/actionCount 1" +
    "/action-1 {" +
    "	/name [ 11" +
    "		72756e4d656e754974656d" +
    "	]" +
    "	/keyIndex 0" +
    "	/colorIndex 0" +
    "	/isOpen 1" +
    "	/eventCount 1" +
    "	/event-1 {" +
    "		/useRulersIn1stQuadrant 0" +
    "		/internalName (adobe_commandManager)" +
    "		/localizedName [ 16" +
    "			416363657373204d656e75204974656d" +
    "		]" +
    "		/isOpen 0" +
    "		/isOn 1" +
    "		/hasDialog 0" +
    "		/parameterCount 2" +
    "		/parameter-1 {" +
    "			/key 1769238125" +
    "			/showInPalette -1" +
    "			/type (ustring)" +
    "			/value [ 17" +
    "				5f30303030303232304133363234383930" +
    "			]" +
    "		}" +
    "		/parameter-2 {" +
    "			/key 1818455661" +
    "			/showInPalette -1" +
    "			/type (ustring)" +
    "			/value [ 8" +
    "				73617948656c6c6f" +
    "			]" +
    "		}" +
    "	}" +
    "}" +
    ""
  );
}

function runMenuItem(name) {
  var act = getFakeText();
  var tmp = File(Folder.desktop + "/set10.aia");
  tmp.open("w");
  tmp.write(act);
  tmp.close();
  app.loadAction(tmp);
  app.doScript("runMenuItem", "temp", false);
  app.unloadAction("temp", "");
  tmp.remove();
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 24, 2020 0
Engaged ,
Aug 24, 2020

Copy link to clipboard

Copied

For the record I can just read the base Presets > [locale] > Scripts directory for Illustrator directly and assuming the Menu Item name is identical to the file without the .jsx extension eval the result of any particular file, but I don't understand why this doesn't work since it felt like the most straightforward solution. I'll probably go this route instead of using Actions because of the typical delay that a create, load, execute and unload takes regardless but would still be curious to know why this happens since I've never needed to try before and haven't run across this issue.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 24, 2020 0