How to use a script to run another script from File > Scripts?
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.
