7 Replies Latest reply on Nov 25, 2011 7:12 PM by JJ0330

    VBScript - Place asset from library.

    JJ0330 Level 1

      Hi, we currently do something in AppleScript which takes and element from and indesign library (.indl) something like:

      set placedAsset to place asset asset "ElementName" of library "FF1.indl" on myDocument

      select placedAsset

      move to {LocationX, LocationY}

       

      Does anyone know how to do in vbscript?

       

      Thanks,

      Joe

        • 1. Re: VBScript - Place asset from library.
          JJ0330 Level 1

          Made some progress.  Not as much VBScript help out there as AppleScript:(  Anyway, maybe this will help someone else.  Still got to figure how to move to x,y location if anyone knows?

           

          Here is what I got to work for placing asset: 

           

          Thanks, Joe

           

           

           

                 Dim  jLib = myInDesign.Libraries.Item("FF1.indl")

                 Dim  jAsset = jLib.assets.item("Daily_News")

                  jAsset.placeAsset(myDocument)

          • 2. Re: VBScript - Place asset from library.
            John Hawkinson Level 5

            Asset.placeAsset() returns an array of placed items. So loop over the array and move each page item to its desired location.

            Remember that library assets have a "home" location that they default to.

            • 3. Re: VBScript - Place asset from library.
              JJ0330 Level 1

              Thanks for responding.  This is how I did in vbscript where I placed the asset on page then group all items in asset then moved the whole asset to specific location.  Can this be done in vb?  Or can I set the default location of asset to always be 0,0?

               

                           tell mydocument

                                 select placedAsset

                                

                                 set myGroupTest to make group with data {group items:every item of selection}

                                 tell myGroupTest

                                       move to {0, 0}

                                       ungroup

                                 end tell

              • 4. Re: VBScript - Place asset from library.
                John Hawkinson Level 5

                Wow, your VBscript looks remarkably like Applescript. :-)

                 

                I can't really help you with VB, I'm sorry. I thought your question was about InDesign's DOM, and that you would be up for translating it into VB. If that's not the case, I strongly suggest you dump VB and use Javascript instead. You'll get much better help on the forums and I think will be less frustrated overall.

                 

                Or can I set the default location of asset to always be 0,0?

                Sure. Just manually place the asset at 0,0 and select it, then right-click the library item and choose Update. All done. Or were you asking something else?

                • 5. Re: VBScript - Place asset from library.
                  JJ0330 Level 1

                  I know JavaScript might be easier to get help but we're doing some other stuff with our sql databases and file moving on our network so actually am doing it in vb.net. Finally got it with looking at Javascripts and some of the vbscript samples in InDesign.  Ended up being a couple lines of code but took me 10 hours to do  Actually works pretty cool.  Thanks for the replies.

                   

                              Dim jLib = myInDesign.Libraries.Item("FF1.indl")   

                              Dim jAsset = jLib.assets.item("Daily_News")

                              jAsset.placeAsset(myDocument)    'Puts asset on page

                              Dim myObjectsToSelect(0)

                              Dim mySpread1 = myInDesign.ActiveWindow.ActiveSpread   

                              Dim myPageItem = mySpread1.PageItems.Item(1)    'Asset in Library has everything grouped

                              myObjectsToSelect(0) = myPageItem

                              Dim XY() As String = {"0in", "0in"}   

                              myInDesign.ActiveDocument.Select(myObjectsToSelect, idSelectionOptions.idReplaceWith)  'Selects group

                              myPageItem.Move(XY)     'Moves to correct location

                              myDocument.ImportXML("\\app1\customers\970020\input.xml")

                              myInDesign.ActiveDocument.Export(idExportFormat.idPDFType, "\\app1\customers\970020.pdf", False)

                  • 6. Re: VBScript - Place asset from library.
                    John Hawkinson Level 5

                    Joe:

                     

                      I'm glad you got it working, but your solution is not correct .

                     

                    The right way to solve this problem is to place the asset and move the returned page items. What you're doing is throwing away the reference to the returned pageitems and finding them anew on the assumption that they are the top pageitem on the page. Then you are selecting them -- scripts should never touch the selection if they can at all avoid it! Then you are moving them, but your move should not be dependent on the selection at all, so the selection is entirely unnecessary! And as you've noted, this only works if the library asset is grouped. Which is not considered best-practice (though sometimes it can be helpful, sometimes not).

                     

                    (Maybe you have another reason for leaving the placed asset selected after you run the script, so the user can interact with it? If so, OK, but only to that small segment.).

                     

                    I'm still not a VBScript programmer, but let's run through this. Remember what I said before:

                     

                    Asset.placeAsset() returns an array of placed items. So loop over the array and move each page item to its desired location.

                    So:

                     

                    Dim jLib = myInDesign.Libraries.Item("FF1.indl")    
                    Dim jAsset = jLib.assets.item("Daily_News")
                    

                     

                    So far so good. But next you should have an array of pageitems returned.

                     

                    Dim myPlacedAssets() = jAsset.placeAsset(myDocument)
                    

                     

                    then you should loop over them and move them. Or if you like, group them, move them, and ungroup them:

                    Dim myGroup = myDocument.Groups.Add(myPlacedAssets)
                    myGroup.Move(XY)
                    myGroup.Ungroup()
                    

                     

                    That probably won't work as written because I'm not a VB person, but that's the correct way to handle this consistent with the InDesign Document Object Model. I suppose it's possible that it won't work in VB because of strict typing issues, but I'm sure there's a way to do it properly....

                    • 7. Re: VBScript - Place asset from library.
                      JJ0330 Level 1

                      Hey, I tried to do but NG.

                       

                      Dim  myPlacedAssets() = jAsset.placeAsset(myDocument)

                       

                      Error 1 Variable cannot be initialized with non-array type 'Object'. C:\Users\administrator.GNTRAD\Documents\Visual Studio 2008\Projects\WatchFolderTest\WatchFolder\Form1.vb 98 17 WatchFolderTest

                       

                      The original code that I found did loop through all page items to group but figured it would be easier to group first and move group.  I think I had some similiar issues with grouping of a placed asset in AppleScript too.  Maybe because the asset has like 100+ objects like rectangles, text boxes, images and so on.  The original code from sample scripts had where you can select rectangles, ovals, polygons, text, groups, etc... so never saw a way to select all objects types.  Will keep plugging away because I am sure that you are correct and there is a easier way to do it but if needed could probably do that example and build an array of all items then move as one group.

                       

                      Thanks, Joe