14 Replies Latest reply: Mar 25, 2013 10:59 AM by bejenbe RSS

    Manipulate Indesign cliente from a Java Application

    Rafael Paz2 Community Member

      Hello, I need to manipulate Indesign client(open/close documents, create menus, tables, set colors etc... ) but I need to do that from a Java application. Is it possible? I was searching on google about it, even in Adobe's site, but the only way I found was thought Javascript, VbScript or AppleScript.

       

       

      Thanks in advance!

       

      Rafael

        • 1. Re: Manipulate Indesign cliente from a Java Application
          hugli2 Community Member

          I can control inDesign from Python or shell scripts on osx via `osascript` This is roughly how it works.

           

          1. My 'master' program runs shell command `osascript -e "tell Indesign blahblah end tell" /path/to/temporary.jsx`  
          2. `osascript` tells Indesign to execute "/path/to/temporary.jsx" as javascript
          3. If all goes well, the value of last expression  in .jsx is returned by `osascript` (serialised JSON works great for this)

          Sorry I'm vague on real life code, but this is my home machine so it's not in my reach.

           

          I realise it's an ugly hack. Main cons follow:

          • quotes have to be escaped couple of times so the result is messy and prone to errors (luckily it's one-off code that sits hidden behind main interface)
          • debugging this chain is nightmare (especially with my nigh-zero knowledge of applescript)
          • so far it's mac only (I suspect same thing can be achieved on Windows machine with VBS)
          • one cannot avoid writing lots of temporary files to the system. (luckily for me, it's only deployed on machines under my control, so the temp files are written to /tmp which is mounted on ramdisk)

           

          The only pro I can think of is that it works. Whole comlexicity is wrapped in one python module, which I only see when squashing the bugs.

           

          Marcel

          • 2. Re: Manipulate Indesign cliente from a Java Application
            hugli2 Community Member

            OK so here's what I use to run JSX file from shell

            osascript  -e 'tell application "Adobe InDesign CS5" to do script alias "%s" language javascript ''' 
            

            where %s is path to the script.

            There's probably a way to run an extendscript statement instead of a file, but my applescript skills are too limited for that. Besides it uses all sorts of quotation marks already.

            Imagine escaping all quotes in your javascript and than escaping the whole thing before you call it from your Java, Python whatever app.

             

            I've no idea what the visual basic equivalent for windows should look like, but I'm sure there's some VB guru around willing to help

             

             

            (Slightly related) syntax sugar:

            If you put this to your InDesign startup script:

            #targetengine "session"

            Application.prototype.shell = function (shell_script){                                                                                                               var apple_script = 'do shell script "' + shell_script + '"'     var output = app.doScript (apple_script, ScriptLanguage.APPLESCRIPT_LANGUAGE)     return output     };

             

            You will have `app.shell()` method available in any script executed in "session" targetengine. Now you can return your Java app favour and call it comfortably from Indesign

            • 3. Re: Manipulate Indesign cliente from a Java Application
              Rafael Paz2 Community Member

              Hey Marcel,

               

              I was searching Jacob and now I'm able to manipulate Indesign from my Java application. Here's a piece of code that show how I can do that:

               

                   //Call Indesign application

                    ActiveXComponent axIndesing = new ActiveXComponent("Indesign.Application");

               

                     //* Create a document

                      Dispatch documents = axIndesing.getProperty("Documents").toDispatch();

                      Dispatch document = Dispatch.get(documents, "Add").toDispatch();

               

                      // Create the document preferences

                      Dispatch documentPreferences = Dispatch.get(document, "DocumentPreferences").toDispatch();

                      Dispatch.put(documentPreferences, "pageHeight", "800pt");

                      Dispatch.put(documentPreferences, "pageWidth", "600pt");

                      //Dispatch.put(documentPreferences, "pageOrientation", "PageOrientation.landscape");

                      Dispatch.put(documentPreferences, "pagesPerDocument", 1);

               

              I'm using the Adobe Indesign CS6 Tutorial to be able to implement that code. It's not big deal.

               

              but thanks anyway, Dude for help me out!

               

              obs: Just have to add the Jacob library to you java application

               

              Rafael Paz

              • 4. Re: Manipulate Indesign cliente from a Java Application
                hugli2 Community Member

                I don't know much about Java, but that code seems to be Windows only. Anyway, I wish you good luck teaching Java & InDesign talking to each other.

                 

                 

                Just out of curiosity, what do you mean by "searching Jacob"?

                • 5. Re: Manipulate Indesign cliente from a Java Application
                  Rafael Paz2 Community Member

                  Yeah, thats correct, its for windows only. It's because our server is windows server. About "searching Jacob" sorry about that, I forgot to complete the setence: I  mean, I was searching on google about Jacob library. Jacob is a library for Java, in other words is a Bridge where you can comunicate COM objects with an application.

                   

                  thanks man!

                  • 6. Re: Manipulate Indesign cliente from a Java Application
                    Dmitry Aleksandrov Community Member

                    And don't forget it's for 32 Bit Systems only.

                    There is also Jawin Library, but the same problem - 32 Bit.

                    We had some experience with Jawin.

                    I wouldn't recommend it to anyone.

                     

                    Now we use ID Server and SOAP Interface.

                    • 7. Re: Manipulate Indesign cliente from a Java Application
                      Dirk Becker Community Member

                      My first attempt was an ExtendScript socket polled from within InDesign, and used to bounce scripts back and forth via eval() and/or Json. I found out the hard way though, that at least on OSX the last thing that ExtendScript will do is an attempt to write to a broken socket, whenever I prematurely close the other side e.g. by debugger kill. Besides CS5 idle events apparently stop working when InDesign is in background.

                       

                      In a CS4 production environment I'm using a custom plugin for idle notifications, it also helps to pre-filter selection events to our purposes.

                       

                      My latest experimental environment is Java on OSX which provides AppleScript as scripting engine (via javax.script), no need to use the shell or temporary files. I've been using it for a few months. With some glue code here and a few more wrapper objects there, it is fun to have a true IDE with Java code completion, javadoc help and syntax checking on InDesign scripts ...

                       

                      Of course this setup comes at the cost of a slight speed penalty; I still have not found a good approach for methods that return variants (different types on different occasions) and objects that change their type on the fly are still a pain.

                       

                      Thanks for the pointers to Jacob and Jawin, are there any experiences with j-Interop?

                       

                      Dirk

                      • 8. Re: Manipulate Indesign cliente from a Java Application
                        Rafael Paz2 Community Member

                        Hello Dirk,

                         

                        Well, I dont have experience with j-Interop, but if you wanna manipulate Indesign from a application(in my case Java) I suggest you JACOB. It's not a big deal, but you have to check Adobe Indesign CS6 Tutorial when you're coding. So you check how it's implemented in JavaScript and just transform this code in Java, for exemple:

                         

                        To create a document in Javascrip t(Adobe Indesign CS6 Tutorial):

                        var myDocument = app.documents.add();

                         

                        Now is just use JACOB and tranform this code like this:

                        ActiveXComponent axIndesing = new ActiveXComponent("Indesign.Application");

                         

                        Dispatch documents = axIndesing.getProperty("Documents").toDispatch();

                        Dispatch document = Dispatch.get(documents, "Add").toDispatch();

                         

                        Very simple, man!

                         

                        Rafael Paz

                        • 9. Re: Manipulate Indesign cliente from a Java Application
                          Dmitry Aleksandrov Community Member

                          Very simple functionality like one from Tutorial will work.

                          But when you start to do some complex things it's not very reliable.

                          IMHO. If it works for you, that's fine.

                          • 10. Re: Manipulate Indesign cliente from a Java Application
                            Rafael Paz2 Community Member

                            Hello, Dmitry !

                             

                            I'm trying this stuff with JACOB because I didint find anything else to do that. Do you have some cool alternative to JACOB? I know that are Jarwin and Com4j, but I heard this tools are not good stuff

                            • 11. Re: Manipulate Indesign cliente from a Java Application
                              Dmitry Aleksandrov Community Member

                              We have not found cool alternative

                              As I remember I've found that some methods or properties were not implemented in stub generated with Jawin.

                              So we changed to InDesign Server. With SOAP you can relative easy start any script on Server.

                              But if Jacob is OK for your tasks, it's nice.

                              • 12. Re: Manipulate Indesign cliente from a Java Application
                                Cenchen Community Member

                                Hi Rafael,

                                 

                                     I want to call ".jsx" file(Adobe javascript) through JAVA... Is it possible?

                                • 13. Re: Manipulate Indesign cliente from a Java Application
                                  Rafael Paz2 Community Member

                                  Hello Cenchen,

                                   

                                  I'll have to do it also, but I dont know how to do that yet. I was working on a Indesign project, but I was moved to another stuff, so i dont know when I'll come back to work with Indesign.

                                  If you find something cool, dont forget to share with us.

                                   

                                  thanks

                                   

                                  Rafael

                                  • 14. Re: Manipulate Indesign cliente from a Java Application
                                    bejenbe Community Member

                                    Cenchen, there is sample Java code in Adobe's InDesign Server SDK to send a SOAP request to an InDesign Server process started up to serve SOAP requests on a particular port number. Part of what you send in the SOAP request as a parameter is a path to a .JSX file to execute (either from the requesting side, or on the server side). You can get back a JavaScript result from that script in your Java that issues the SOAP request.  Is what all you mean by wanting to call JavaScript from Java? Or is this something that you need to work also in InDesign desktop, not only the server verions?

                                     

                                    (Perhaps you have already discovered these answers since your posting.)

                                     

                                    There is also a load balancing scheme at least in CS6 using CORBA to make requests to the server.