9 Replies Latest reply on Jul 6, 2013 8:27 AM by DaveSofTypefi

    window.bounds gives weird results

    DaveSofTypefi Level 2

      I run without Application Frame and without Application Bar. I see no point to the former and the latter is just a shocking waste of space. For years, I've been using a handful of scripts to manage my window positions. One of them is now misbehaving. The script is so old that it didn't use functions and all its variables were global -- it probably dates all the way back to when I was beta-testing the original ExtendScript in CS.

       

      So I decided to rewrite it in case I'd done something weird that rewriting would expose. Well, I found the weirdness but it wasn't me. With one document window open, I typed this into the JavaScript Console (I'm running with a monitor that is 1920 x 1200):

       

      app.windows[0].bounds = [22,32,1140,863]

       

      and the console duly responded with:

       

      Result: 22,32,1140,863

       

      and visually, I can see that the window is indeed at that location.

       

      But now when I type:

       

      app.windows[0].bounds

       

      into the JS Console, it responds with:

       

      Result: 60,32,1178,863

       

      The x values are right but the y values are off by 38.

       

      Is this a Mac-only issue? I'm running with Mountain Lion on a Retina MacBook Pro. Oh: of course it's Mac only: you can't switch off the Application Frame on Windows, can you?

       

      Dave

        • 1. Re: window.bounds gives weird results
          DaveSofTypefi Level 2

          Tried an experiment: switched the monitor to 1680 x 1050. Got these results:

           

          app.windows[0].bounds = [22,32,700,563]

          Result: 22,32,700,563

          app.windows[0].bounds

          Result: 350,32,1028,563

           

          So now the y values are off by 328.

           

          Hmm. I expect the OS is playing a part in this.

           

          Dave

          • 2. Re: window.bounds gives weird results
            Vamitul Level 4

            Hi Dave!

             

            One quick question: does the window move to it's desired location, and just reports the wrong coords, or actualy moved in the wrong position.

             

            try moving the window to [0,0], then try to make it full screen.

            • 3. Re: window.bounds gives weird results
              DaveSofTypefi Level 2

              Yes, it moves to the right place but reports the wrong values.

               

              The 0,0 idea is promising. Perhaps I can use that to measure the error and then apply that to the results of my other queries.

               

              Thanks,

               

              Dave

              • 4. Re: window.bounds gives weird results
                Vamitul Level 4

                i can't test now, since i'm on windows, but the first result (the 38 px difference on the y axis) made me think of the height of the menubar, but the secon one is more puzzling.

                what results are you getting if you enable the application frame and bar?

                the app.windows[0].bounds should work the same.

                • 5. Re: window.bounds gives weird results
                  DaveSofTypefi Level 2

                  The script is largely pointless if you have the application frame on; the idea of the script is to allow me to quickly get two windows side by side or one document window next to an ESTK window.

                   

                  I won't be able to get back to this for a day or two because of pressures of other work.

                   

                  Thanks for your interest and particularly for the [0,0] idea.

                   

                  Dave

                  • 6. Re: window.bounds gives weird results
                    DaveSofTypefi Level 2

                    It turned out that moving to 0,0 created problems with the x-coordinate, but since those were the only x-coordinate problems I ever saw, I decided to focus only on the y-coordinate. In this version of the script, I move the window to y = 0 to measure the current value of the y error.

                     

                     

                    //DESCRIPTION: Resets active layoutWindow size or uses front layoutWindow to set page default size.
                    
                    
                    /*
                              Rewrite of a venerable old favorite
                              1. If no layoutWindow open offer to delete settings file
                              2. If no settings and layoutWindow offer to make layoutWindow bounds new settings
                              3. If settings and layoutWindow move layoutWindow to settings location unless there's a layoutWindow already there
                                  in which case, move layoutWindow next to that other layoutWindow or move only layoutWindow right by its width
                    */
                    
                    
                    (function() {
                              var settingsFile = getSettingsFile();
                              if (app.layoutWindows.length == 0) {
                                        // no front layoutWindow; offer user chance to delete settings
                                        if (confirm("No layoutWindow is open; would you like to delete the settings file?")) {
                                                  // user said yes; only bother to delete it if it exists
                                                  if (settingsFile.exists) {
                                                            settingsFile.remove();
                                                  }
                                        return; // no layoutWindow, so nothing more to do
                                        }
                              }
                              // there's at least one layoutWindow
                              var yDelta = getDelta(app.layoutWindows[0]);
                              if (settingsFile.exists) {
                                        applySettings(settingsFile, yDelta);
                              } else {
                                        if (confirm("Settings file is missing. Use current front layoutWindow to set default?")) {
                                                  // user said do it
                                                  var bounds = app.layoutWindows[0].bounds;
                                                  var bounds = [
                                                            bounds[0] - yDelta,
                                                            bounds[1],
                                                            bounds[2] - yDelta,
                                                            bounds[3]
                                                  ]
                                                  new File(settingsFile);
                                                  settingsFile.open("w");
                                                  settingsFile.write(bounds);
                                                  settingsFile.close();
                                        }
                              }
                    
                    
                              function getDelta(layoutWindow) {
                                        // get current layoutWindow position and dimensions
                                        var reportedBounds = layoutWindow.bounds;
                                        var wHeight = reportedBounds[2] - reportedBounds[0];
                                        // move to 0, 0
                                        layoutWindow.bounds = [0, reportedBounds[1], wHeight, reportedBounds[3]];
                                        $.sleep(10);
                                        var newBounds = layoutWindow.bounds;
                                        // calculate errors
                                        var yDelta = newBounds[0];
                                        // restore layoutWindow to original position
                                        layoutWindow.bounds = [
                                                  reportedBounds[0] - yDelta, 
                                                  reportedBounds[1],
                                                  reportedBounds[2] - yDelta,
                                                  reportedBounds[3]
                                        ]
                                        $.sleep(10);
                                        return yDelta
                              }
                      
                              function applySettings(settingsFile, yDelta) {
                                        // values written out to the settings file must take delta into account
                                        settingsFile.open("r");
                                        var savedBounds = settingsFile.read();
                                        settingsFile.close();
                      
                                        var myBounds = savedBounds.split(",");
                                        for (var j = myBounds.length - 1; j >= 0; j--) {
                                                  myBounds[j] = Number(myBounds[j]);
                                        }
                      
                                        // Check to see if an existing layoutWindow is already at these bounds;
                                        var layoutWindowsBounds = app.layoutWindows.everyItem().bounds;
                                        // all these values are off by delta
                                        for (var j = layoutWindowsBounds.length - 1; j >= 0; j--) {
                                                  if (layoutWindowsBounds[j][0] - yDelta == myBounds[0]
                                                                      && layoutWindowsBounds[j][1]  == myBounds[1]
                                                                      && layoutWindowsBounds[j][2] - yDelta == myBounds[2]
                                                                      && layoutWindowsBounds[j][3] == myBounds[3]) {
                                                            // layoutWindow right there, so:
                                                            var myWidth = myBounds[3] - myBounds[1];
                                                            myBounds[1] += myWidth;
                                                            myBounds[3] += myWidth;
                                                            break; // only do it once!
                                                  }
                                        }
                                        app.layoutWindows[0].bounds = myBounds; 
                              }
                    
                    
                              function getSettingsFile() {
                                        var scriptFile = getScriptFile();
                                        return File(scriptFile.parent + "/PageDefault" + parseInt(app.version) + ".txt");
                              }
                    
                    
                              function getScriptFile() {
                                        // This function returns the file of the active script, even when running ESTK
                                        try {
                                                  return app.activeScript;
                                        } catch(e) {
                                                  return File(e.fileName);
                                        }
                              }
                    }())
                    
                    
                    • 7. Re: window.bounds gives weird results
                      DaveSofTypefi Level 2

                      I should mention that switching the Application Bar on and off changes the value of yDelta from 38 to 3 in my current set up. This is true whether the Application Bar is docked or not.

                      • 8. Re: window.bounds gives weird results
                        Vamitul Level 4

                        Hi dave.

                        I'm still curious if, using application frame, the problem still exists. (app.windows[0].bounds should return the app frame's position). If so, then the problem resides somwhere deeper, most likely in the os. Still i's a bug that should be reported.

                         

                        Another question: is the yDelta constant? For example it's the same if you move the window to [80,80] and later to [520,250]?

                         

                        Ps1: for settings there is a nice trick (i think i learned it from tomaxxi??): make a settings object. to save them: app.insertLabel("myScriptNameSettings", mySettings.toSource()). To load the settings, just use myStettings=eval(app.extractLabel("myScriptNameSettings"). no need for external file, no problems with permissions etc.

                         

                        ps2: you say you need sometimes to run estk side by side to a document window. I used to do that for easy acces to the console. If so: Re: Indesign command box

                        • 9. Re: window.bounds gives weird results
                          DaveSofTypefi Level 2

                          The application frame makes things worse. It doesn't flip-flop, it wanders across a small area of the screen. Look at this from the JS Console:

                           

                          app.layoutWindows[0].bounds = app.layoutWindows[0].bounds

                          Result: 7,12,1155,872

                          app.layoutWindows[0].bounds = app.layoutWindows[0].bounds

                          Result: 20,2,1183,882

                          app.layoutWindows[0].bounds = app.layoutWindows[0].bounds

                          Result: 5,-8,1183,892

                          app.layoutWindows[0].bounds = app.layoutWindows[0].bounds

                          Result: -10,-18,1183,902

                           

                          Yes, yDelta does vary according to where it starts life. I'm seeing lots of different values, but the script seems to compensate correctly in all cases, including detecting that a window is in the way and moving the front window to the alternative position.

                           

                          I'm aware of the concept of saving settings as labels and frequently do that, but in this case there are many advantages to using an external file -- transferability from one ID release to another -- finetuning the position with a text editor.

                           

                          I like ESTK beside my window so I can debug scripts.

                           

                          Thanks,

                           

                          Dave