31 Replies Latest reply on Jul 21, 2010 10:07 PM by sstanleyau

    scripts and frame tags?

    Roger Sawhill Level 1

      This is a general question that, if possible, I'll be looking for details at a later date.

       

      Is it possible to use Frame Tags to identify a layout object so that a script can accurately select it no matter where it is on a layout? Or is there another function that can do this?

       

      Filemaker allows you to name objects for this purpose and when I saw the Tag Frame option I started wondering if it could be used for that purpose.

       

      Thanks

      Roger

        • 1. Re: scripts and frame tags?
          [Jongware] Most Valuable Participant

          Absolutely. Look up "label" in your favourite scripting language.

           

          Up to CS4 (I think) almost every single object in ID can have a single label. Starting with CS5 (again, I think) you can add any kind of 'key-value' pair to almost every single ID object.

          You can easily apply and interrogate labels from within a script, but also from with the User Interface -- the "Script Label" panel shows the label for a selected item.

           

          The great thing about labels is that you can even use the same label for a set of different objects, and then perform operations on every item with that label!

          • 2. Re: scripts and frame tags?
            [Jongware] Most Valuable Participant

            (The Tag Frame option is for XML tagging of files. It's kinda sort of the same thing as a user defined label, but then in a pure XML context.)

            • 3. Re: scripts and frame tags?
              Roger Sawhill Level 1

              I thought so :-)

               

              Thanks for the info, will circle back to using this in a few weeks after everything else on this job has been put to bed. I'll probably be back with questions once I get there.

               

              Thanks again

              • 4. Re: scripts and frame tags?
                Roger Sawhill Level 1

                Jongware, another question if you will - can the script see the contents of a text box?

                 

                Some background --- We are building an automated product using a variable data plugin for Indesign (DesignMerge). One of my earlier questions in the forums was about editing color swatch RGB values so that we can change the color scheme of the product with a script. At the edge of the product we have production data being brought in from our database (job number, client data, shipping address, etc), one of those items is the ColorCode.

                 

                So, if this ColorCode is in its own text block and that text box has been given a unique script label, can the script be built to "see" the text in this text block and use this data to decide which color scheme to trigger? I'm thinking the answer to this would be 'yes'.

                 

                 

                My other question is, since what we are talking about here is far outside my abilities ("damn it Jim, I'm a designer not a programmer!"), are you or are there guys on the forum that do contracts for this type of work and what is the proper procedure for soliciting such work? Any thoughts on what this type of thing might cost - ballpark? I have no clue.

                 

                Thanks

                Roger

                • 5. Re: scripts and frame tags?
                  [Jongware] Most Valuable Participant
                  [C]an the script be built to "see" the text in this text block and use this data to decide which color scheme to trigger? I'm thinking the answer to this would be 'yes'.

                   

                  Yes

                  Every object containing a reference to InDesign text can immediately access its contents as a simple Javascript string. If your frame has the label "mycolor", you can read its text using

                   

                  alert (app.activeDocument.textFrames.item("mycolor").contents);

                   

                  My other question is [..], are you or are there guys on the forum that do contracts for this type of work and what is the proper procedure for soliciting such work? Any thoughts on what this type of thing might cost - ballpark? I have no clue.

                   

                  Not me personally, but there are a few pro scripters that do exactly this kind of thing: writing custom scripts for highly individual requirements. As to the cost: I have no clue either...

                   

                  Wait a couple of days for one of the guys to pop up in this thrad, or send you a personal message. If it takes too long, post a new thread where you describe what the script ought to do, with a title like "Scripter Needed". Be sure to state both platform (Mac, Windows) and targeted InDesign version -- there are a some important differences amongst these.

                  • 6. Re: scripts and frame tags?
                    Muppet Mark-QAl63s Level 4

                    Yes a script could read the text contents of a labelled text frame then make decisions based on this. So you could avoid the user interaction of a dialog altogether… Here is a snippet that may get you started just using a switch on the contents…

                     

                    #target indesign
                    
                    function colorSwap() {
                         if (app.documents.length == 0) {
                              alert('Please have an "InDesign" document open before running this script.');
                              return;
                         }
                         var docRef = app.activeDocument;
                         with (docRef) {
                              for (var i = 0; i < textFrames.length; i++) {
                                   if (textFrames[i].label == 'Foo') {
                                        switch(textFrames[i].contents) {
                                             case
                                             'Red' :
                                             alert("It's Red…");
                                             break;
                                             case
                                             'Green' :
                                             alert("It's Green…");
                                             break;
                                             default :
                                             alert("It's Black…"); 
                                        }
                                   }
                              }
                         }
                    }
                    
                    colorSwap();
                    

                     

                    You need to make enough case's to cover your list of colour schemes and replace the 'alert()' commands with the syntax of how you wish to proceed in each case. In this the label it's looking for is 'Foo' and the cases it's looking for are contents being 'Red' or 'Green' for anything else its black.

                    • 7. Re: scripts and frame tags?
                      Roger Sawhill Level 1

                      Muppet Mark


                      If I read you right, you are saying that I'd need to label each frame manually that has a color I wish to change? Or would the script look for each frame with those color parameters and change the values?

                       

                      Either way, I'm thinking this might be the harder way to approach this. Our layout has a couple hundred objects, yet it only has 5 custom color swatches that need to be edited -- edit the swatch values, and all objects with the related colors are also edited (including tints). This would lead to only needing to have 5 case statements each with 10 color options. Does this make sense?

                       

                      Roger

                      • 8. Re: scripts and frame tags?
                        Roger Sawhill Level 1

                        Thanks Jongware, will give that a try once I am ready to roll in that direction.

                        • 9. Re: scripts and frame tags?
                          Muppet Mark-QAl63s Level 4

                          So, if this ColorCode is in its own text block and that text box has been given a unique script label, can the script be built to "see" the text in this text block and use this data to decide which color scheme to trigger? I'm thinking the answer to this would be 'yes'.

                          Roger, if your document already contains the objects coloured with your 10 RGB colours then you just require a 5 way switch each with 10 commands to change colour values… I have a test document that contains 2 colours 'Test 1' & 'Test 2' there is only 1 text frame with a label which is set to 'ColorCode' the cases for this to change are I change the text contents of this labelled text frame to '0001', '0002', '0003', '0004' or '0005' then run the script. This should be easy enough for you to duplicate the extra lines required change the color names to suit and set the 'ColorCode' switch conditions to your text strings… Which I think your plug-in is going to supply before?

                           

                          #target indesign
                          
                          function colorSwap() {
                               if (app.documents.length == 0) {
                                    alert('Please have an "InDesign" document open before running this script.');
                                    return;
                               }
                               var docRef = app.activeDocument;
                               with (docRef) {
                                    for (var i = 0; i < textFrames.length; i++) {
                                         if (textFrames[i].label == 'ColorCode') {
                                              switch(textFrames[i].contents) {
                                                   case
                                                   '0001' :
                                                   colors.item('Test 1').colorValue = [10,100,150];
                                                   colors.item('Test 2').colorValue = [120,190,160];
                                                   break;
                                                   case
                                                   '0002' :
                                                   colors.item('Test 1').colorValue = [30,180,170];
                                                   colors.item('Test 2').colorValue = [240,170,180];
                                                   case
                                                   '0003' :
                                                   colors.item('Test 1').colorValue = [50,160,190];
                                                   colors.item('Test 2').colorValue = [160,150,200];
                                                   break;
                                                   case
                                                   '0004' :
                                                   colors.item('Test 1').colorValue = [70,140,210];
                                                   colors.item('Test 2').colorValue = [180,130,220];
                                                   break;
                                                   case
                                                   '0005' :
                                                   colors.item('Test 1').colorValue = [90,120,230];
                                                   colors.item('Test 2').colorValue = [200,110,240]; 
                                              }
                                         }
                                    }
                               }
                          }
                          
                          colorSwap();
                          

                          • 10. Re: scripts and frame tags?
                            Roger Sawhill Level 1

                            Muppet Mark -- YOU ROCK

                             

                            I can't wait until I get all this other stuff on my desk finished so I can play with this code!

                             

                            Many, many, many thanks

                             

                            btw, do you do this his lind of thind on a contractual basis? I might have need to hire someone to take care of some scripting that I am too green to do or just don't have the time to do.

                             

                            cheers

                            Roger

                            • 11. Re: scripts and frame tags?
                              Muppet Mark-QAl63s Level 4

                              Now that is funny… NOT even close to being good enough… Just do bits to aid my own work flows here & there. There are forum members that Im sure do do contract work and I don't think that there are any forum rules that don't allow you to post a request…

                              • 12. Re: scripts and frame tags?
                                Roger Sawhill Level 1

                                well if you aren't even good enough, then my knowledge is a mere spec of lint adrift in the cosmos (which I readily aknowledge).

                                 

                                As Clint Eastwood once said "a man's got to know his limitations"!

                                 

                                That said, I don't need Einstein or Hawking, any "run of the mill physicist" will do - and so far you are solving these problems with ease. Sure you're not interested?

                                • 13. Re: scripts and frame tags?
                                  Roger Sawhill Level 1

                                  Hi Muppet Mark

                                   

                                  finally got to work with your script on this color change and it works flawlessly - many thanks.

                                   

                                  However, I have one more request on this. Due to poor planning on my customers part, at the beginning of the project we were told everything was to be produced using CMYK - but in the end, the vendor demanded RGB. My problem is that my master automation file - and the 897 InDesign support files - are in CMYK. They all use the same 5 CMYK spot colors.

                                   

                                  So the question is, can your script be modified to convert the 5 spot colors to RGB values (any RGB values will do) and then move into doing what the original script was designed to do?

                                   

                                  Any help would be great as I don't look forward to converting 900 InDesign files.

                                   

                                  thanks

                                  Roger

                                  • 14. Re: scripts and frame tags?
                                    Muppet Mark-QAl63s Level 4

                                    If I am understanding you correctly then you just need to change the space of each of you colours then supply the new values.

                                    So each of your case statements should look more like this…

                                     

                                    case'0001' :
                                    colors.item('Test 1').space = ColorSpace.rgb;
                                    colors.item('Test 1').colorValue = [10,100,150];
                                    colors.item('Test 2').space = ColorSpace.rgb;
                                    colors.item('Test 2').colorValue = [120,190,160];
                                    break;
                                    

                                    • 15. Re: scripts and frame tags?
                                      Roger Sawhill Level 1

                                      Many thanks.You're a life saver.

                                       

                                      It's amazing the potential hell one minor decision 3 months ago can have on a situation now.

                                      • 16. Re: scripts and frame tags?
                                        Roger Sawhill Level 1

                                        Hi again

                                         

                                        I've been backward engineering some javascript to learn how to build some things. I've decided that Applescript is just too wierd a language and I should be focusing on JS (and cross platform). I am looking into taking some JS classes in the last quarter of the year at the design school I teach at.

                                         

                                        Anyway I've been looking everywhere but can't seemt to figure out 2 things in JS:

                                         

                                        1) using Script Labels, how do I get the text value out of a labeled Text box as a variable so that I can use it in other functions

                                            (i.e. getting the value from the text box to use as the filename when saving) ;

                                         

                                        2) how do I use JS to select an object based on its Script Label so that I can then point another action to that selected object(s)

                                            (i.e. selecting an object so that you can run another script that looks for selected objects or to delete the object(s) )

                                         

                                         

                                        By the way, if my questions start annoying you, please just tell me. It's great to have some one around you can ask questions of, but I don't want you to think I am taking advantage of you time or talents.

                                         

                                         

                                        thanks again

                                        • 17. Re: scripts and frame tags?
                                          Muppet Mark-QAl63s Level 4

                                          Roger, I started scripting with AppleScript as I don't come from a programming background but design. The language I felt was much easier to pick up. JavaScript has taken a fair bit longer to start getting my head around but I am making progress all be it slowly. Here is an example on how you get the script label of the first text frame as a variable…

                                           

                                          #target indesign
                                          
                                          var docRef = app.activeDocument;
                                               with (docRef) {
                                               // Variable from label text contents
                                               var foo = textFrames[0].label;
                                               alert(foo);
                                               // Target an object and issue a command 
                                               textFrames[0].remove();
                                          }
                                          

                                           

                                          It is just a property of an object. What I would say is avoid selection while it is the case when using the GUI to work with objects. In script you just set them as target then either change their properties or issue a command. In the above you will see that the text frame has been removed but there was NO need to make any selection in order to do it. Script Labels are IMO a great way to work Im currently working on a pretty large catalogue job and the whole thing is labelled up. I would dread to thing how long this job would take if this were NOT the case. The only thing I need do with ID that I did not in Quark is tell users NOT to touch/edit them as they have access in the GUI. In Quark they were safe as only script could access it. Have fun with the learning invest in a thick mouse mat there's lots of head whacking involved…

                                          • 18. Re: scripts and frame tags?
                                            Roger Sawhill Level 1

                                            Hi Mark, finally getting back to this code, been doing more Applescript the last few days.

                                             

                                            ok, so after writing a long post telling you I was completely stumped, something clicked and I figured out how to do what I needed. It is a hybrid of this code and the code you sent me the  other day.

                                             

                                            var docRef = app.activeDocument;
                                            with (docRef) {
                                                for (var i = 0; i < textFrames.length; i++) {
                                                    if (textFrames[i].label == 'ordnum') {
                                                        var $ordnum = textFrames[i].contents;
                                                        alert($ordnum);
                                                    }
                                                }
                                            }

                                             

                                            what was killing me was how ID looked at [i]. I didn't realize that it was a numerical value representing the textframes order in the stack.

                                             

                                            What I still can't figure out is the line in bold up above, I know it's got to be cycling through the document looking for the first object with the correct label, but I don't understand the structure of it. Do you mind explaining it's 3 sections so that I can use it with a semblence of a clue?

                                             

                                             

                                            the other question I have is, do you know how to turn ON or off a specific named layer's visibility? I have scoured many a reference guide and the net and can't find anything except that which relates to setting properties when a new layer is created via the script, but nothing regarding a layer that already exists.

                                             

                                            many thanks

                                            • 19. Re: scripts and frame tags?
                                              Peter Kahrel Adobe Community Professional & MVP

                                              Roger,

                                               

                                              In for (var i = 0; i < textFrames.length; i++)
                                              - the first part (var i = 0) initialises the counter i;
                                              - the second part (i < textFrames.length) tells the loop when to stop. In this case, at the last text frame;
                                              - the third part tells the loop how to increment. i++ is short for i = i + 1 and means "do every item". You could also write i = i + 2, which skips every other frame.

                                               

                                              Run this script in the ESTK:

                                               

                                              for (var i = 0; i < 10; i++)

                                                  $.writeln (i);

                                               

                                              It'l print the numbers 0 to 9 in the console.

                                               

                                              Marks's code is expensive in that at every iteration of the loop the collection of text frames is built up. In a short document that doesn't matter, but in longer documents that can slow down a script dramatically. You can speed up the script by building turning the collection of frames into an array. In that way, the collection is built just once. That does mean that the script would look a bit different, you can't use "with" the way Mark did 9and which is a bit unusual in InDesign circles:

                                               

                                              var myFrames = app.activeDocument.textFrames.everyItem().getElements();


                                              // another small point is that it's deemed better to define variables outside of loops.
                                              // If you define them inside a loop, you create in effect a new variable at every iteration

                                              // (and there's no need to prefix variable names with $)


                                              var ordnum;
                                              for (var i = 0; i < myFrames.length; i++) {
                                                 if (myFrames[i].label == 'ordnum') {
                                                 ordnum = textFrames[i].contents;
                                                       alert(ordnum);
                                                   }
                                              }

                                               

                                              As to the visibility of layers, you set visible to true or false:

                                               

                                              myLayer.visible = false;
                                              myLayer.visible = true;

                                               

                                              You can find these things in the ESTK's object-model viewer or, even better, in Jongware's HTMLled help files (http://www.jongware.com/idjshelp.html).

                                               

                                              Finally, at http://oreilly.com/catalog/9780596802523/ you'll find an introduction to JavaScript in InDesign. It covers CS3 and CS4, but CS5 as well -- largely. For your purposes, the description of labels in last section on text frames is out of date, but an update will be out later this month.

                                               

                                              Peter

                                              • 20. Re: scripts and frame tags?
                                                Peter Kahrel Adobe Community Professional & MVP

                                                Mark,

                                                 

                                                >The only thing I need do with ID that I did not in Quark is tell users NOT to touch/edit them as they have access in the GUI. In Quark they were safe as only script could access it.

                                                 

                                                In InDesign you can set "invisible" labels. This sets a label:

                                                 

                                                myFrame.insertLabel ("model", "DB4");

                                                 

                                                You read the label as follows:

                                                 

                                                myModel = myFrame.extractLabel ("model");

                                                 

                                                These labels are invisible in the interface and can be used anywhere wehre you can use .label.

                                                 

                                                Peter

                                                • 21. Re: scripts and frame tags?
                                                  PankChat Level 1

                                                  >Finally, at http://oreilly.com/catalog/9780596802523/ you'll find an introduction to JavaScript in InDesign. It covers CS3  and CS4, but CS5 as well -- >largely.

                                                   

                                                  Thanks for the info Peter. I was just going thru the website you mentioned above and seems that it is quite basic book for JS. Though I am sure I will learn a lot from it also :-).

                                                   

                                                  Anyways, I personally started my JS for Indesign of my own, with available (pdf) scripting guides for CS3/CS4 and so on and of course like many learnt a lot from the forums. But I still have to find a book wth comprehensive details (with samples) for InDesign JS. Truely speaking I never looked for that yet.

                                                   

                                                  Your Books on Regex also seems to be promising but can you or for that matter anyone have suggestions for handful books on this.

                                                   

                                                  TIA,

                                                  --PC

                                                   

                                                  PS: I would rather prefer pdfs instead of hardcopies.

                                                  • 22. Re: scripts and frame tags?
                                                    Muppet Mark-QAl63s Level 4

                                                    Peter, thanks for this tip I did NOT know this. I may well look into moving my labels in the near future… Oh and the 'with' thing I lifted from the ID CS2 PDF intro to Javascript (The only reference I had at the time). I did ask about this before now in the Photoshop Scripting forum as I had NOT seen any examples like this before by those guys… I couldn't work out if it were good or bad thing? Wether this suited one app's object model over another that's all still very much at the 'you live n learn' stage…

                                                    • 23. Re: scripts and frame tags?
                                                      Peter Kahrel Adobe Community Professional & MVP

                                                      PC,

                                                       

                                                      >Your Books on Regex also seems to be promising but can you or for that matter anyone have suggestions for handful books on this.

                                                       

                                                      There isn't very much on GREP in InDesign. The main GREP title is print only (Friedl, "Mastering regular expressions", O'Reilly). My O'Reilly title is pretty exhaustive I think.

                                                       

                                                      Peter

                                                      • 24. Re: scripts and frame tags?
                                                        PankChat Level 1

                                                        Thanks again.

                                                         

                                                        Any suggestions for comprehensive book (for beginner to master) on InDesign JS? Or one need to master of its own :-)

                                                         

                                                        Thanks again,

                                                        --PC

                                                        • 25. Re: scripts and frame tags?
                                                          PankChat Level 1

                                                          Well Peter. Please do not bothered http://forums.adobe.com/thread/556039 tells me alot.

                                                           

                                                          Well planning son to grab one.

                                                           

                                                          Thanks

                                                          --PC

                                                          • 26. Re: scripts and frame tags?
                                                            PankChat Level 1

                                                            Ooooooooooooooops

                                                             

                                                            Planning soon..................... :-)

                                                            • 27. Re: scripts and frame tags?
                                                              Roger Sawhill Level 1

                                                              Many thanks Peter, your response helped me undestand many things and your new code has me scratching my head again :-)  I am used to writing loops and building counters in Filemaker, so this is a very diferent approach but it makes more sense than i did yesterday.

                                                               

                                                              I am starting to think I should stick with one language for a while - bouncing back and forth is hard on the brain - and I seem to be having more success in engineering Applescripts.

                                                               

                                                              For that reason, I am trying to reverse engineer some InDesign javascript i got online (as I want to marry it with several other functions I have built in Applescript). I have managed to disect most of the script but there are two terms/functions for which I can't find any reference:

                                                               

                                                              -- getFrameCenter

                                                               

                                                              -- getTextCenter

                                                               

                                                              I know what they are supposed to do, but I am not sure how they do it. Do you know of any equivalent Applescript actions?

                                                               

                                                              My other question, is there a way in Applescript to trigger/run a separate complex javascript that is in the InDesign Scripts palette? The doscript function seems like a possible solution, but, as I understand it, do script requires that i more or less build an applescript line for each javascript line --  it would be tough considering the length of the script (again, assuming I understand doscript correctly).

                                                               

                                                              Again, thanks for everyone's help -- the more I learn the more I find that I don't know -- one step at a time.

                                                               

                                                              cheers

                                                              Roger

                                                              • 28. Re: scripts and frame tags?
                                                                sstanleyau Level 4

                                                                It's hard to say what those functions do without seeing their code.

                                                                 

                                                                As for do script, you can pass it a whole slab of js -- you just need to deal with quoting -- or pass an alias to a js (or AS) script.

                                                                • 29. Re: scripts and frame tags?
                                                                  Roger Sawhill Level 1

                                                                  sstanleyau, my bad, he was calling out another chunk of JS he had built that was further down in the process, I just hadn't realized it.

                                                                   

                                                                  The quoting you mention in the doscript is exactly my worry, as I fear I will muck it up out of ignorance.

                                                                   

                                                                  Now, your concept of passing an alias I have not heard of but am very intrigued -- I would much rather just use this code instead of trying to rebuild  something similar in AS. if I have an AS, how do I tell it to hand off to a JS (or another AS for that matter)? Can you have it return to the "parent" script after completion?

                                                                   

                                                                  thanks in advance


                                                                  Roger

                                                                  • 30. Re: scripts and frame tags?
                                                                    sstanleyau Level 4

                                                                    Just call it and your script will continue when it's done:

                                                                     

                                                                    tell application "Adobe InDesign CS4"

                                                                    do script alias "Macintosh HD:folder:js file" language javascript

                                                                    end tell

                                                                    1 person found this helpful
                                                                    • 31. Re: scripts and frame tags?
                                                                      Roger Sawhill Level 1

                                                                      call it as a file! DUH.   All this time I've been trying to call it from within InDesigns's Scripting palette.


                                                                      Sooooo much simpler. Sometimes you just have to step back to actually see the forest.

                                                                       

                                                                      Thanks