31 Replies Latest reply on Apr 8, 2014 7:31 AM by Dave Saunders

    [JS] About Files

      My name is avi. I have some difficulties in my script.
      My problem is i want to create a file and i want to read and write that file.
      For creating a file i am using following code but it is not working. Can anyone please look at it.

      var myFile = new File("~/Desktop/amit.rtf");
      myFile.create();

      Currently i am working on two scripts. I want to create and write a text file using first program and then i need to read that text file using second program. Can anyone please tell me how to create, read and write a file using javascript(InDesign).

      I just want to write a number in that file which i can read using second program. Please help me.

      Thank you,
        • 1. Re: [JS] About Files
          Level 1
          Here's a script I use to create/read from/write to/delete a file:
          //DESCRIPTION: Resets active window size or uses front window to set default size.
          

          // Check for WindowDefault.txt; if present use it to set front window
          // if not, use front window to set it after checking with user.

          myPath = app.activeScript;
          myParts = myPath.toString().split("/");
          myParts[(myParts.length - 1)] =  "WindowDefault" + app.version.slice(0,1) + ".txt";
          myNewPath = myParts.join("/");

          mySettingsFile = File(myNewPath);

          // Before proceeding, check that there is a front window
          // If not, offer user the chance to delete the current settings
          if (app.windows.length < 1) {
          beep();
          if (confirm("No window is open; would you like to delete the settings file?")) {
            // User said yes; check that it exists
            if(mySettingsFile.exists) {
             mySettingsFile.remove();
            }
          }
          } else {

          if (mySettingsFile.open("r")) {

            savedBounds = mySettingsFile.read();
            mySettingsFile.close();

            myBounds = savedBounds.split(",");
            for (i = 0; i<myBounds.length; i++) {
             myBounds[i] = Number(myBounds[i]);
            }
            app.windows[0].bounds = myBounds;
          } else {
            beep();
            if (confirm("Settings file is missing. Use current front window to set default?")) {
             // User said: go to it.
             new File(mySettingsFile);
             mySettingsFile.open("w");
             mySettingsFile.write(app.windows[0].bounds);
             mySettingsFile.close();
            }
          }
          }
          Dave
          • 2. Re: [JS] About Files
            Level 1
            Hmm, I'd use a different approach to the first bit these days:
            myPath = app.activeScript;
            
            mySettingsFile = File((myPath.parent) + "/WindowDefault" + app.version.slice(0,1) + ".txt");
            Dave
            • 3. Re: [JS] About Files
              Hi Avi,

              Perhaps you don't need to read and write to a file.
              You could also use scriptArgs.
              Use app.scriptArgs.set("myNumber", myNumber) in the first script and
              app.scriptArgs,get("myNumber") to get the number in your second script.

              Grtn, Kees
              • 4. Re: [JS] About Files
                Level 1
                Hello KvD

                Thank you KvD.
                This is what i want. This is much easier way to do the thing what i am doing.
                Can you tell me where the this ScriptArgs store the value. It will store the value in system buffer or some other place.
                • 5. Re: [JS] About Files
                  NTC Ann Level 1

                  Hello, Unfortunately, I am not understanding this at all. I don't know scripts. From the previous posts a while ago (which unfortunately don't work any more). I need to do some steps to rename/copy/paste the script so that my work area stays that size instead of going the whole width of the desktop and behind the palettes. But unfortunately, I don't understand what is being said here in these postings re. scripts. 

                   

                  Is there someway to get a script, change it's ending and then just put it somewhere in InDesign in CS4 and it will work. I understand that I also have to do another step of making sure something is not active any more (unchecking some box).

                   

                  Thank you again for all your help.

                  • 6. Re: [JS] About Files
                    Dave Saunders Level 4

                    My windows scripts don't work in Windows ;-( because in Windows you can't turn off the Application Frame and with that on, the script can't change the positioning of the windows.

                     

                    So, before we go any further, can you confirm that you are a Mac user?

                     

                    Dave

                    • 7. Re: [JS] About Files
                      NTC Ann Level 1

                      Yes, Mac. OS 10.5.x (I have to double check at home which OS). Thank you.

                      • 8. Re: [JS] About Files
                        Dave Saunders Level 4

                        In that case, here again is the script I use:

                         

                        //DESCRIPTION: Resets active window size or uses front window to set default size.

                         

                        // Check for WindowDefault.txt; if present use it to set front window
                        // if not, use front window to set it after checking with user.

                         

                        myPath = app.activeScript;
                        myParts = myPath.toString().split("/");
                        myParts[(myParts.length - 1)] =  "WindowDefault" + app.version.slice(0,1) + ".txt";
                        myNewPath = myParts.join("/");

                         

                        mySettingsFile = File(myNewPath);

                         

                        // Before proceeding, check that there is a front window
                        // If not, offer user the chance to delete the current settings
                        if (app.windows.length < 1) {
                        beep();
                        if (confirm("No window is open; would you like to delete the settings file?")) {
                          // User said yes; check that it exists
                          if(mySettingsFile.exists) {
                           mySettingsFile.remove();
                          }
                        }
                        } else {

                         

                        if (mySettingsFile.open("r")) {

                          savedBounds = mySettingsFile.read();
                          mySettingsFile.close();

                          myBounds = savedBounds.split(",");
                          for (i = 0; i<myBounds.length; i++) {
                           myBounds[i] = Number(myBounds[i]);
                          }
                          app.windows[0].bounds = myBounds;
                        } else {
                          beep();
                          if (confirm("Settings file is missing. Use current front window to set default?")) {
                           // User said: go to it.
                           new File(mySettingsFile);
                           mySettingsFile.open("w");
                           mySettingsFile.write(app.windows[0].bounds);
                           mySettingsFile.close();
                          }
                        }
                        }

                         

                        Instructions:

                         

                        1. Copy and paste the script into a new ExtendScript Toolkit document.

                         

                        • To find ExtendScript Toolkit, look in Applications/Utilities/Adobe Utilities/ExtendScript Toolkit CS4/
                        • Fire it up and it will present you with an empty document

                         

                        2. Save the script to the Desktop (temporarily) with the name "Reset Window Position.jsx".

                        3. Fire up InDesign CS4.

                        4. Open the Scripts panel.

                         

                        • If it is not active, use Window/Automation to activate it

                         

                        5. Right-click the folder named User and choose Reveal in Finder.

                        6. Create a folder inside that folder named "Windows"

                        7. Drag the script into that folder.

                        8. Use Edit/Keyboard Shortcuts and assign the shortcut Ctrl-W to this script (or any shortcut of your choice).

                        9. Make sure that Application Frame is not active (also on the Window menu).

                        10. Open a document and get its window the way you want it.

                        11. Run the script for the first time. It will ask if you want to use the front window to set your preference. Say yes.

                         

                        That's it. Now, you can resize your front window by using the shortcut at any time. To change the preferences, you can open the text file it creates in that same Windows folder and twiddle with the numbers. Or, you can run the script with no document open to delete the preferences (it asks you first) and then repeat steps 10 and 11.

                         

                        Dave

                        • 9. Re: [JS] About Files
                          NTC Ann Level 1

                          Thank you. I will try it tonight.

                          • 10. Re: [JS] About Files
                            NTC Ann Level 1

                            Hi. I made a folder called Windows and then didn't know if I was

                            supposed to drag that inside the scripts folder so I did.


                            Then I tried to find the Edit/Keyboard Shortcuts which I couldn't find.


                            I tried to open a document but it opened to the whole desktop again.


                            What am I doing wrong.


                            Application Frame is not active.


                             

                            6. Create a folder inside that folder named "Windows"

                            7. Drag the script into that folder.
                            8. Use Edit/Keyboard Shortcuts and assign the shortcut Ctrl-W to this script (or any shortcut of your choice).
                            9. Make sure that Application Fr
                            ame is not active (also on the Window menu).
                            10. Open a document and get its window the way you want it.
                            11. Run the script for the first time. It will ask if you want to use the front window to set your preference. Say yes.
                            • 11. Re: [JS] About Files
                              NTC Ann Level 1

                              Hi again.


                              I found edit/shortcuts and went to windows.


                              Reset Window Position does not show up


                              ??

                              • 12. Re: [JS] About Files
                                NTC Ann Level 1

                                Hi again. Also I opened windows twice and opened scripting and opened etc... till I got to Windows Reset Position.

                                I clicked on it and it resent the window... saved... reopened... window back to entire desktop width.

                                Thanks for help.

                                • 13. Re: [JS] About Files
                                  Dave Saunders Level 4

                                  Yes, the Windows folder should be inside the Scripts Panel folder.

                                   

                                  When you go to Edit/Keyboard Shortcuts you need to look for the Scripts section and then give the script a shortcut. If you have all the pre-installed scripts still in your Scripts panel you'll have to scroll down a long way to find it.

                                   

                                  The script does nothing until you run it. The use of a shortcut is optional but if you don't have one it takes just as long to find the script in the panel as it does to fix the window manually.

                                   

                                  Dave

                                  • 14. Re: [JS] About Files
                                    NTC Ann Level 1

                                    OK I found scripts for keyboard shortcuts, I found reset window position.jsx.

                                    Under new shortcut I put in Control/W

                                    I clicked on OK

                                    I got cannot modify the default set.

                                    Create a new set based on the default set?

                                    Now what? Thanks.

                                    • 15. Re: [JS] About Files
                                      Dave Saunders Level 4

                                      Take a deep breath.

                                       

                                      Open a document.

                                       

                                      Manually adjust the window to the size you want to be your default.

                                       

                                      Run the script.

                                       

                                      The script will ask you if you want the new size to be your default. Say yes (or OK or whatever the choice is).

                                       

                                      Now, whenever you run the script it will make the front InDesign window that size.

                                       

                                      Dave

                                      • 16. Re: [JS] About Files
                                        NTC Ann Level 1

                                        Hi again. I let it create a new set based on the default set.


                                        I opened the window and did control/w and it resized the window.


                                        I saved and closed and reopened, the window was back to being the entire width of desktop.

                                        • 17. Re: [JS] About Files
                                          NTC Ann Level 1

                                          Maybe I am asking the wrong ?


                                          How do I run the script? Should I be doing something diff?? Thanks again.

                                          • 18. Re: [JS] About Files
                                            NTC Ann Level 1

                                            Hi again. I found runs script but the make it default never came up. It just

                                            makes the window bigger or to size. I must be doing something wrong.

                                            • 19. Re: [JS] About Files
                                              NTC Ann Level 1

                                              Well, I think I messed things up. I had the scripting window open and

                                              there was the reset window one and a default one. I deleted the default one and now

                                              the scripting doesn't work at all... the window stays the entire size of the desktop.

                                              Is there a way to undo the damage? Why can't CS4 just fix the problem? Thanks again.

                                              • 20. Re: [JS] About Files
                                                Dave Saunders Level 4

                                                There is nothing the script can do to change the size that InDesign uses to open a window.

                                                 

                                                What the script does is put you one keystroke away from getting the window to be the size you want it.

                                                 

                                                That's what it does. That's all that it does.

                                                 

                                                You just have to set up the defaults (as you did) and then run the script each time you create a new window.

                                                 

                                                Once you save a document and reopen it, it will open at the new size, but new windows will be at whatever size InDesign decides to make them.

                                                 

                                                Dave

                                                • 21. Re: [JS] About Files
                                                  NTC Ann Level 1

                                                  Why won't it open at the new size when you save and reopen the doc? Why does it keep

                                                  opening the full size of desktop every time? Thanks again.

                                                  • 22. Re: [JS] About Files
                                                    NTC Ann Level 1

                                                    PS. I never got the window where it said, make the new size the default.

                                                    When I click on run script it just brings the window back and forth every

                                                    time? Thanks.

                                                    • 23. Re: [JS] About Files
                                                      NTC Ann Level 1

                                                      I give up. Is there some inexpesive way to buy CS3. I went from

                                                      CS2 to CS4?

                                                      • 24. Re: [JS] About Files
                                                        Dr. Paul Proteus Level 1

                                                        I share Ann's frustration a little, though I was able to get the script going with the detailed instructs -- many thanks to Dave, this was bugging me to no end each time I opened something, at least now I can resize with a keystroke.

                                                         

                                                        Why doesn't Adobe just let us set our own default screen-opening size in the Preferences?

                                                        • 25. Re: [JS] About Files
                                                          DBLjan Level 2

                                                          Hey there,

                                                           

                                                          i have to bump this thread again out of oblivion:

                                                           

                                                          Was anyone able to house this script in startup stripts with an event-handler after open? I read several threads about APID, afterOpen and var myDoc = app.documents[0]; but i'm not able to get it done, due lack of JS-skills.

                                                           

                                                          Has anyone done this/can provide a clue to achieving this?

                                                           

                                                          Here is what didn't worked so far:

                                                           

                                                           

                                                          #target indesign

                                                          #targetengine "session"

                                                           

                                                          main();

                                                                    

                                                          function main()

                                                          {

                                                          var myEventListener = app.eventListeners.add("afterOpen", ReziseWindow, false);

                                                          }

                                                                    

                                                          function ReziseWindow()

                                                          {

                                                          var myDoc = app.documents[0];

                                                          myPath = app.activeScript;

                                                          myParts = myPath.toString().split("/");

                                                          myParts[(myParts.length - 1)] =  "WindowDefault" + app.version.slice(0,1) + ".txt";

                                                          myNewPath = myParts.join("/");

                                                          mySettingsFile = File(myNewPath);

                                                           

                                                           

                                                          if (app.windows.length < 1) {

                                                          beep();

                                                           

                                                           

                                                          if (confirm("Kein Fenster geöffnet; Soll die Einstellungsdatei gelöscht werden?")) {

                                                            if(mySettingsFile.exists) {

                                                             mySettingsFile.remove();

                                                            }

                                                          }

                                                          } else {

                                                          if (mySettingsFile.open("r")) {

                                                            savedBounds = mySettingsFile.read();

                                                            mySettingsFile.close();

                                                            myBounds = savedBounds.split(",");

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

                                                             myBounds[i] = Number(myBounds[i]);

                                                            }

                                                            app.windows[0].bounds = myBounds;

                                                          } else {

                                                            beep();

                                                            if (confirm("Einstellungsdatei nicht vorhanden; Aktives Fenster nutzen um Position zu speichern?")) {

                                                             new File(mySettingsFile);

                                                             mySettingsFile.open("w");

                                                             mySettingsFile.write(app.windows[0].bounds);

                                                             mySettingsFile.close();

                                                            }

                                                          }

                                                          }

                                                          }

                                                          • 26. Re: [JS] About Files
                                                            Dave Saunders Level 4

                                                            I think Startup is too early for this script to do anything. There's no window for it to operate on at that point.

                                                             

                                                            You might be able to make it responsive to an onOpen event, although even that might be too early because the window comes after the event.

                                                             

                                                            I've never felt the urge to do this kind of thing because I actually use a pair of instances of this script each with a shortcut to toggle between full and half screen. I even have another instance that will push the document on to my second monitor, so having any of them operate automatically is of marginal value.

                                                             

                                                            Dave

                                                            • 27. Re: [JS] About Files
                                                              DBLjan Level 2

                                                              I thought scripts that listen to any handlers should be start up scripts. As you see

                                                              var myEventListener = app.eventListeners.add("afterOpen", ReziseWindow, false); the script should only run if any document is opened.

                                                               

                                                              I thought about your idea of having multiple variations of the script, but i only need one window-position to fit my workspace near my pallettes, so it should be applied to any opened window after open.

                                                               

                                                              But, curse me for not having any skills in JS, i cant get it to work.

                                                              • 28. Re: [JS] About Files
                                                                Dave Saunders Level 4

                                                                I've written so few event-handling scripts that I'm not much use here, but I suspect that your problem could well be that afterOpen happens after the document opens but before the window is drawn.

                                                                 

                                                                Dave

                                                                • 29. Re: [JS] About Files
                                                                  DBLjan Level 2

                                                                  I don't really know where I read this today, but I did some research today on topics regarding the "afterOpen"-handler not working before CS4 and fireing befor the window is drawn. Kasyan often suggested to use an eventhandler from APIDToolAssistant and provided a script for this (RememberDocumentsPosition.spln), but somehow it doesnt work.

                                                                   

                                                                  In a later thread he acknowledge that the InD-handler isnt broke, but has to be used with var myDoc = app.documents[0];

                                                                  But since I can script java, i dont know where to house this in your given script (which, to give you kudos here, is great the way it is)

                                                                  • 30. Re: [JS] About Files
                                                                    Dave Saunders Level 4

                                                                    Thanks for the compliment.

                                                                     

                                                                    re: var myDoc = app.documents[0];

                                                                     

                                                                    The first statement of the ResizeWindow function is precisely that.

                                                                     

                                                                    I'll try some experiments to see if I can get a handle on this, but don't hold your breath!

                                                                     

                                                                    Dave

                                                                    • 31. Re: [JS] About Files
                                                                      Dave Saunders Level 4

                                                                      I started a new topic which you may have seen, but just in case, this seems to work (I was wrong about there not being a window to work with):

                                                                       

                                                                      #target indesign
                                                                      #targetengine 'dave'
                                                                      
                                                                      
                                                                      (function(){
                                                                                var myEventListener = app.eventListeners.add("afterOpen", testFunction, false);
                                                                      
                                                                      
                                                                                function testFunction() {
                                                                                          app.layoutWindows[0].bounds = [50, 32, 1143, 944];
                                                                                }
                                                                      }())
                                                                      
                                                                      

                                                                       

                                                                      Dave