4 Replies Latest reply on Oct 25, 2018 6:43 PM by Robert Kyle

    BeforeOpen--Detect if Copy or Original Was Selected

    Tony722 Level 1

      Hi,

       

      Added an EventListener on beforeOpen and need to be able to tell if a document is being opened as a copy or not.

       

      beforeOpen provides a DocumentEvent object which has:

      .Target (which is Application)

      .Parent (which is Application)

      and .CurrentTarget (which is also Application)

      .FullName is the Document.FullName which is the same in both cases (copy or original)

       

      Application.ActiveDocument is null (provided that no other documents are open)

       

      Any ideas where else I could go to figure out what the user did? I want to be handling this in the "beforeOpen" stage as I want to be able to prevent opening the document (PreventDefault) under certain conditions--but only if they're opening the original, if they're opening a copy it's fine.

       

      Thanks!

       

      Tony

        • 1. Re: BeforeOpen--Detect if Copy or Original Was Selected
          Robert Kyle Level 2

          I don't know of a way to do this with the beforeOpen event. But if you try afterOpen, you'll see that it fires twice: the first time the target is a Document object; the second time it is a LayoutWindow. If the user has opened a copy, the title of the window (target.name) will contain the word "untitled".

           

          So maybe the best you can do is spot the problem in the afterOpen stage, which is still before the user has a chance to do anything. If you determine that he/she has opened the original, you could close it and issue a new Open() command using the OPEN_COPY.

           

          Hope this helps.

           

          Bob

          • 2. Re: BeforeOpen--Detect if Copy or Original Was Selected
            Robert Kyle Level 2

            Upon further review, maybe there is a way:

             

            Your beforeOpen handler stores target.fullName in a variable. Then it uses preventDefault(). Always. Then it uses the open() method with the OPEN_COPY option to open target.fullName in an Untitled window.

             

            So the users who do things the right way are treated as if they did it the wrong way. Something weird about that, but I  don't think they would ever notice.

             

            Bob

            • 3. Re: BeforeOpen--Detect if Copy or Original Was Selected
              Tony722 Level 1

              Thanks, that's a creative idea!


              My plan was to give them a dialog box giving the option of opening a copy or not opening the file at all.

               

              I could still give them this option even if they selected open a copy already. They would sometimes already have selected open as copy and it would say, "would you like to open a copy?" It would be annoying, but probably workable, it's a small workgroup.

               

              I don't know how to set a variable that persists (using VBScript), but I can read write and read a temporary file to flag when they opted to open as copy.

               

              Are you aware of any better way than via the file system to persist a value between event handlers in InDesign's VBScript environment?

               

              Tony

              • 4. Re: BeforeOpen--Detect if Copy or Original Was Selected
                Robert Kyle Level 2

                If it were me, I'd probably stick with the afterOpen event. Then if I see something other than an Untitled window, I'd ask "Do you want to open a copy?"  If the answer is Yes, then I close the document and reopen it with the OPEN_COPY option. I wouldn't want to annoy the folks who selected Copy in the first place.

                 

                Unfortunately, it's been a long time since I've worked in VB, so I really can't answer your question about initializing a persistent variable. In javascript, it's just a matter of running the script in #targetengine session and declaring the variable on the top level. If you go with my idea for a beforeOpen handler, though, the fullName doesn't have to be persistent.

                 

                I will mention that there have been times when I've needed to pass info back and forth from a javascript to a VB script (written by someone else). In those cases, we've used system variables, which can be created and read by both scripts. That may be overkill here.

                 

                Bob