• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Building A Custom Menu Dynamically

Participant ,
Dec 07, 2016 Dec 07, 2016

Copy link to clipboard

Copied

I have created a custom menu of commonly-used scripts for our Prepress department. Works great, but I have to manually update the script that builds the menu and distribute a new copy whenever I add or remove a script from the collection. So I'm trying to rewrite this so it will dynamically build the menu based on the files it finds in the specified folder.

I can get the script to build the menu itself, but where I'm hung up is attaching an event listener to each menu item. I think I've figured out the problem, but I don't know how to solve it.

In the following code snippet (which iterates through the items to build the script menu actions and event listeners), note lines 4 and 9 (specifically the comments). I inserted these alerts as a debugging tool, and they have shown me that my array is being changed to "undefined" inside the event listener function.

I'm not sure why this is happening, but I suspect that it why my menu items don't actually trigger anything. (If I swap in the filename of a script instead of this variable it works just fine).

for (i=0; i < prepressScriptNames.length; i++){

    menuItemFromFilename = prepressScriptNames.slice(0,-4);

    scriptToRun = (scriptsFolder + prepressScriptNames);

    alert (typeof(scriptToRun)); // result: string

    myScriptMenuAction = app.scriptMenuActions.add(menuItemFromFilename);

    myEventListener = myScriptMenuAction.eventListeners.add("onInvoke", function()

    {

        try{app.doScript(File(scriptToRun))} catch(e){}

        alert (typeof(scriptToRun)); // result: undefined. WHY????

    }

)

}

Here's the full code as I have it right now:

// #target InDesign

#targetengine "session"

// GLOBAL ARRAYS

usrSMAs = [];

usrMenus = [];

// LOCATE PREPRESS SCRIPTS FOLDER

var startupScriptsFolder = Folder(app.activeScript.path);

var scriptsFolder = startupScriptsFolder.parent + "/Scripts Panel/Prepress\ Tools/";

// REMOVE EXISTING PREPRESS MENU

var myMainMenu = app.menus.item("$ID/Main");

try{

    var myNewMenu = myMainMenu.submenus.item("Prepress Tools");

    myNewMenu.remove();

}catch(myError){}

// BUILD ARRAY FROM FILENAMES IN PREPRESS SCRIPTS FOLDER

function GetScriptFilenames(){ 

  var scriptPath = Folder(scriptsFolder);

  var scriptFiles = scriptPath.getFiles();

  var scriptNames = [];

  for (var i=0; i<scriptFiles.length; i++){ 

      var thisFile = (scriptFiles);

      if (thisFile.name != ".DS_Store") {

          scriptNames.push(decodeURI(thisFile.name));

      }

  } 

  return scriptNames; 

}

var prepressScriptNames = GetScriptFilenames();

// ITERATE THROUGH ARRAY OF SCRIPT NAMES, CREATE MENU ITEMS AND EVENT LISTENERS

var scriptToRun = [];

var menuItemFromFilename = [];

var myScriptMenuAction = [];

var myEventListener = [];

var myMenuItem = [];

for (i=0; i < prepressScriptNames.length; i++){

    menuItemFromFilename = prepressScriptNames.slice(0,-4);

    scriptToRun = (scriptsFolder + prepressScriptNames);

    alert (typeof(scriptToRun)); // result: string

    myScriptMenuAction = app.scriptMenuActions.add(menuItemFromFilename);

    myEventListener = myScriptMenuAction.eventListeners.add("onInvoke", function()

    {

        try{app.doScript(File(scriptToRun))} catch(e){}

        alert (typeof(scriptToRun)); // result: undefined. WHY????

    }

// CREATE MENU AND SUBMENUS

try{

    var menuPrepressTools = app.menus.item("$ID/Main").submenus.item("Prepress Tools");

    menuPrepressTools.title;

    usrMenus.push(menuPrepressTools);

}

catch (e){

    var menuPrepressTools = app.menus.item("$ID/Main").submenus.add("Prepress Tools");

    // usrMenus.push(menuPrepressTools);

}

// CREATE MENU ITEMS

for (i=0; i < prepressScriptNames.length; i++){

    usrSMAs.push(menuPrepressTools.menuItems.add(myScriptMenuAction));

}

Thanks in advance for any advice or nudges in the right direction!

(Full disclosure: I am not a very skilled Javascripter, or coder of any sort. In fact, I'm learning JS by learning to automate a bunch of InDesign things, I'd never touched JS before now!)

TOPICS
Scripting

Views

275

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
community guidelines
no replies

Have something to add?

Join the conversation