6 Replies Latest reply on Oct 16, 2008 11:55 AM by (Buster)

    Working in menu item but not in console... why?

      Hi there,

      I've got a script that puts a menu item in Acrobat which lets me clean leading numbers out of my bookmarks using regular expressions (thanks to George J for his help with that one). The script is as follows:

      app.addMenuItem( {
      cName: "Remove Leading Numbers from Bookmarks",
      cParent: "Document",
      cExec: "removeBookmarkPrefix(bookmarkRoot);"
      });

      function removeBookmarkPrefix(bookmark) {
      // If there are no bookmarks, do nothing
      if (bookmark == null) return;
      // Resursive function to remove bookmark prefix
      // Rename bookmark without prefix
      bookmark.name = bookmark.name.replace(/^\d\S*\s+/, "");
      if (bookmark.children != null) {
      for (var i = 0; i < bookmark.children.length; i++) {
      removeBookmarkPrefix(bookmark.children[i]); }
      }
      }

      It works perfectly -- when I select it from the menu.

      However, I'm now trying to work this code into a batch script. I figured it would be easy enough. I simply removed the portion of the script that creates the menu item:

      app.addMenuItem( {
      cName: "Remove Leading Numbers from Bookmarks",
      cParent: "Document",
      cExec: "removeBookmarkPrefix(bookmarkRoot);"
      });

      ... however, when I run this in the console, it doesn't work.

      Why is that?
        • 1. Re: Working in menu item but not in console... why?
          Patrick Leckey Level 3
          That's because all you're doing is defining a function. Nothing at any point is actually calling that function (which is what the cExec line in the menu item does).

          Directly after the function, try adding the following line and then executing again:

          removeBookmarkPrefix(bookmarkRoot);
          • 2. Re: Working in menu item but not in console... why?
            gkaiseril MVP & Adobe Community Professional
            You can just call the "removeBookmarkPrefix()" function in the batch process using the "Run JavaScript". This assumes that your code has been placed in the JavaScript application folder for Acrobat/Reader. If you restart Acrobat/Reader you shold then be able to run the following code from the console:

            removeBookmarkPrefix(this.bookmarkRoot)

            If you rerun the second block of code, you will just be adding another menu option.

            Your add menu code can be made to be grayed out when a PDF is not opened by using the following code:

            app.addMenuItem( {
            cName: "RemoveBookmarksNoPrefix",
            cUser: "Remove Leading Numbers from Bookmarks",
            cParent: "Document",
            cExec: "removeBookmarkPrefix(this.bookmarkRoot)",
            cEnable: "event.rc = (event.target != null);"
            });
            • 3. Re: Working in menu item but not in console... why?
              Level 1
              Hi PDL,

              I think you were suggesting that I try the following JS:

              function removeBookmarkPrefix(bookmark) {
              // If there are no bookmarks, do nothing
              if (bookmark == null) return;
              // Resursive function to remove bookmark prefix
              // Rename bookmark without prefix
              bookmark.name = bookmark.name.replace(/^\d\S*\s+/, "");
              if (bookmark.children != null) {
              for (var i = 0; i < bookmark.children.length; i++) {
              removeBookmarkPrefix(bookmark.children[i]); }
              }
              }
              removeBookmarkPrefix(bookmarkRoot);

              The response is:
              redeclaration of const removeBookmarkPrefix
              1:Console:Exec
              undefined

              ... and it still doesn't work as required. Hmmm...
              • 4. Re: Working in menu item but not in console... why?
                Level 1
                (By the way, thanks Geo for the gray-out code.)
                • 5. Re: Working in menu item but not in console... why?
                  Patrick Leckey Level 3
                  > I think you were suggesting that I try the following JS:

                  Ah, yes, if you're running it as part of a batch process it wouldn't like that. Sorry, I missed that part - just focused on the "run from the console" bit.

                  Place the removeBookmarkPrefix function in a folder-level javascript file, that way it will be loaded when Acrobat starts. Then your batch script can simply be:

                  removeBookmarkPrefix(this.bookmarkRoot);

                  That way it won't try to redeclare the function in each iteration of the batch process.
                  • 6. Re: Working in menu item but not in console... why?
                    Level 1
                    Thanks - that worked brilliantly!

                    Buster