2 Replies Latest reply on May 15, 2013 6:54 AM by abjelly

    Object required 'sel'

    abjelly Level 1

      Hello,

       

      I am trying to recreate the following script in vbScript: http://forums.adobe.com/message/4173641

       

      The below code is what i hjave so far, but at the moment i get an error described in the title of this discussion. It is happening in the for loop at method "moveObjects".

       

      Any light on this?

       

      Thank you.

       

      EDIT: even when i do it like "set sel = currDoc.Selection()" it gives me an empty error??

       

      set appRef = CreateObject("Illustrator.Application")
      
      ''''CREATE HOST DOCUMENT
      set docPreset = CreateObject("Illustrator.DocumentPreset")
      With docPreset
         .DocumentTitle = "tmpPortfolio"
         .DocumentColorSpace = 2
         .DocumentRasterResolution = 3
         .DocumentUnits = 8
      
         .NumArtboards = 12
         .ArtboardRowsOrCols = 4
         .Width = 841.89
         .Height = 595.28
      End With
      
      appRef.Documents.AddDocument "AiDocumentColorSpace.aiDocumentCMYKColor", docPreset
      
      dim tmpDocumentLayerState
      tmpDocumentLayerState = unlockHideLayers(appRef.Documents("tmpPortfolio"))
      ''''CREATE HOST DOCUMENT
      
      
      ''''TRANSFER ARTBOARDS
      dim arrFiles(0)
      arrFiles(0) = "C:\scripts\testfiles\testing\LW - BOT3.ai"
      set currDoc = appRef.Open(arrFiles(0))
      
      dim currDocumentTitle
      dim currNumArtboards
      dim currDocumentLayerState
      dim ABsInfo()
      
      currDocumentTitle = currDoc.Name
      currNumArtboards = currDoc.ArtBoards.Count
      currDocumentLayerState = unlockHideLayers(currDoc)
      ReDim ABsInfo(currNumArtboards - 1, 1)
      
      currDoc.Activate()
      
      For i = 1 to currNumArtboards
         ABsInfo(i - 1, 0) = currDoc.artboards(i).artboardRect
         ABsInfo(i - 1, 1) = currDoc.artboards(i).name
      
         currDoc.selection = nothing
         currDoc.artboards.setActiveArtboardIndex(i - 1)
         currDoc.selectObjectsOnActiveArtboard
      
         dim sel
         sel = currDoc.selection
      
         moveObjects sel, appRef.Documents("tmpPortfolio")
      Next
      ''''TRANSFER ARTBOARDS
      
      
      ''''FUNCTIONS
      Function unlockHideLayers(doc)
         dim layerCount
         layerCount = doc.Layers.Count
         dim layerState()
         redim layerState(layerCount - 1, 1)
         For i = 1 to layerCount 
            set iLayer = doc.Layers(i)
            layerState(i - 1, 0) = iLayer.visible
            layerState(i - 1, 1) = iLayer.locked
            iLayer.visible = true
            iLayer.locked = false
         Next
         unlockHideLayers = layerState
      End Function
      
      Sub moveObjects(sel, destDoc)
         msgbox("")
         for i = 1 to sel.length
            'dim newItem
            'newItem = sel(i).duplicate destDoc.Layers(sel(i).layer.name), "ElementPlacement.PLACEATEND"
         next
      End Sub
      ''''FUNCTIONS
      
        • 1. Re: Object required 'sel'
          CarlosCanto Adobe Community Professional & MVP

          I haven't tried but try this

           

          change this

          currDoc.Activate()

           

          to this

          currDoc.Activate

           

           

          to get the selection, this is correct

          sel = currDoc.selection

           

           

          and to loop through items in the selection array, in vb use

              sel = currDoc.Selection
              For j = LBound(sel) To UBound(sel)
          
                  'your code
                  sel(j).Selected = False
              Next
          

           


          1 person found this helpful
          • 2. Re: Object required 'sel'
            abjelly Level 1

            Ok i got my script working

             

            In case somebody may find this interesting, here it is:

             

            Based for a small part on http://forums.adobe.com/message/4173641

             

            The purpose for this script is to create a portfolio for a collection of artboards(in my case with barcodes).

             

            First you give the number of artboards, number of cols and page format. So your destination ai file has a empty start portfolio

            Next you let the script read in a csv file with the cols: idstyle;imgoutline;imgabnumber;imgarticle;imgbarcode

            All the rest is automated, it will now open each ai file one by one and

            - duplicate all the artboards next to each other beneath your start setup, one row per ai file.

            - match the description and barcode at the artboard, given the artboard number

             

            ps my artboards have a fixed size 600x600

             

            set appRef = CreateObject("Illustrator.Application")
            appref.UserInteractionLevel = -1
            
            dim numArtboards, artboardRowsOrCols, presetWidth, presetHeight, errMessage
            numArtboards = 12
            artboardRowsOrCols = 4
            presetWidth = 841.89
            presetHeight = 595.28
            errMessage = ""
            
            ''''CREATE HOST DOCUMENT
            set docPreset = CreateObject("Illustrator.DocumentPreset")
            With docPreset
               .DocumentTitle = "tmpPortfolio"
               .DocumentColorSpace = 2
               .DocumentRasterResolution = 3
               .DocumentUnits = 8
            
               .NumArtboards = numArtboards
               .ArtboardRowsOrCols = artboardRowsOrCols
               .Width = presetWidth
               .Height = presetHeight
            End With
            
            appRef.Documents.AddDocument "AiDocumentColorSpace.aiDocumentCMYKColor", docPreset
            
            dim tmpDocumentLayerState
            tmpDocumentLayerState = unlockUnhideLayers(appRef.Documents("tmpPortfolio"))
            ''''CREATE HOST DOCUMENT
            
            
            ''''READ SOURCE
            dim fs, objTextFile, arrStr
            set ArrReaderLines = CreateObject("System.Collections.ArrayList")
            set ArrReaderLinesStyles = CreateObject("System.Collections.ArrayList")
            
            set fs=CreateObject("Scripting.FileSystemObject")
            set objTextFile = fs.OpenTextFile("C:\Book1.csv")
            Do while NOT objTextFile.AtEndOfStream
               arrStr = split(objTextFile.ReadLine,";")
            
               If(not ArrReaderLinesStyles.Contains(CStr(arrStr(0)))) Then
                  If(IsNumeric(arrStr(0))) Then
                     ArrReaderLinesStyles.Add(CStr(arrStr(0)))   
                  End If
               End If
            
               dim obj
               set obj = New ReaderLines
               obj.propIdStyle = arrStr(0)
               obj.propImgOutline = arrStr(1)
               obj.propImgAbNumber = arrStr(2)
               obj.propImgArticle = arrStr(3)
               obj.propImgBarcode = arrStr(4)
               ArrReaderLines.Add obj
               set obj = Nothing
            Loop
            objTextFile.Close
            set objTextFile = Nothing
            set fs = Nothing
            ''''READ SOURCE
            
            
            ''''ARTBOARDS
            dim destDocRowNumber, destDocColNumber
            destDocRowNumber = 0
            destDocColNumber = 0
            
            set charStyleA = appRef.Documents("tmpPortfolio").CharacterStyles.Add("tmpCharStyleA")
            With charStyleA.CharacterAttributes
               .Size = 36
               .TextFont = appRef.TextFonts.getByName("MyriadPro-Regular")
            End With
            set charStyleB = appRef.Documents("tmpPortfolio").CharacterStyles.Add("tmpCharStyleB")
            With charStyleB.CharacterAttributes
               .Size = 88
               .TextFont = appRef.TextFonts.getByName("CodeEAN13")
            End With
            set parStyle = appRef.Documents("tmpPortfolio").ParagraphStyles.Add("tmpParStyle")
            With parStyle.ParagraphAttributes
               .Justification = 2
            End With
            
            for each itrIdStyle in ArrReaderLinesStyles
               dim currDocLayerState
               dim currDocTitle
               dim currDocNumArtboards
               for each itrObj in ArrReaderLines
                  if(CStr(itrIdStyle) = CStr(itrObj.propIdStyle)) then
                     set currDoc = appRef.Open(itrObj.propImgOutline)
                     currDocLayerState = unlockUnhideLayers(currDoc)
                 currDocTitle = currDoc.Name
                 currDocNumArtboards = currDoc.ArtBoards.Count
            
                 destDocRowNumber = destDocRowNumber + 1
                     destDocColNumber = 0
            
                     exit for
                  end if
               next          
            
               For i = 1 to currDocNumArtboards
                  currDoc.Activate()
                  currDocABname = currDoc.artboards(i).name
                  currDocABrect = currDoc.artboards(i).artboardRect
                  currDoc.selection = null
                  currDoc.artboards.setActiveArtboardIndex(i - 1)
                  currDoc.selectObjectsOnActiveArtboard
                  currDoc.Copy
            
            
                  appRef.Documents("tmpPortfolio").activate
                  set destDocAB = appRef.Documents("tmpPortfolio").artboards.add(currDocABrect)
                  Select case Len(destDocRowNumber)
                     case 1
                        destDocAB.name = "00" & destDocRowNumber & "_" & currDocABname
                     case 2
                        destDocAB.name = "0" & destDocRowNumber & "_" & currDocABname
                     case 3
                        destDocAB.name = destDocRowNumber & "_" & currDocABname
                  End Select
            
                  dim leftMeas, topMeas, rightMeas, bottomMeas, spaceHeight, txtHeightA, txtHeightB
                  spaceHeight = 20
                  txtHeightA = 50
                  txtHeightB = 100
            
                  ''''set start point
                  destDocABrect = destDocAB.artboardRect
                  leftMeas = destDocABrect(0)
                  topMeas = destDocABrect(1)
                  rightMeas = destDocABrect(2)
                  bottomMeas = destDocABrect(3)
                  destDocABrect(0) = 0 'LEFT
                  destDocABrect(1) = 0 'TOP
                  destDocABrect(2) = destDocABrect(2) - leftMeas 'RIGHT
                  destDocABrect(3) = destDocABrect(3) - topMeas 'BOTTOM
                  appRef.Documents("tmpPortfolio").artboards(appRef.Documents("tmpPortfolio").artboards.count).artboardRect = destDocABrect
            
                  ''''re-place
                  destDocABrect = appRef.Documents("tmpPortfolio").artboards(appRef.Documents("tmpPortfolio").artboards.count).artboardRect
                  bottomMeas = destDocABrect(3) - ((numArtboards / artboardRowsOrCols) * presetHeight) - (destDocRowNumber * (600 + spaceHeight + txtHeightA + txtHeightB))
                  topMeas = destDocABrect(1) - ((numArtboards / artboardRowsOrCols) * presetHeight) - (destDocRowNumber * (600 + spaceHeight + txtHeightA + txtHeightB))
                  rightMeas = destDocABrect(2) + (destDocColNumber * (600 + spaceHeight))
                  leftMeas = destDocABrect(0) + (destDocColNumber * (600 + spaceHeight))
                  destDocABrect(3) = bottomMeas 'BOTTOM
                  destDocABrect(1) = topMeas 'TOP
                  destDocABrect(2) = rightMeas 'RIGHT
                  destDocABrect(0) = leftMeas 'LEFT
                  appRef.Documents("tmpPortfolio").artboards(appRef.Documents("tmpPortfolio").artboards.count).artboardRect = destDocABrect
            
            
                  ''''add text
                  destDocABrect = appRef.Documents("tmpPortfolio").artboards(appRef.Documents("tmpPortfolio").artboards.count).artboardRect
            
                  set rectRefA = appRef.Documents("tmpPortfolio").PathItems.Rectangle(destDocABrect(3), destDocABrect(0), 600, txtHeightA) 'y, x, w, h
                  set txtFrameA = appRef.Documents("tmpPortfolio").textFrames.AreaText(rectRefA)
                  set rectRefB = appRef.Documents("tmpPortfolio").PathItems.Rectangle(destDocABrect(3) - txtHeightA, destDocABrect(0), 600, txtHeightB) 'y, x, w, h
                  set txtFrameB = appRef.Documents("tmpPortfolio").textFrames.AreaText(rectRefB)
                  for each itrObj in ArrReaderLines
                     if(CStr(itrIdStyle) = CStr(itrObj.propIdStyle)) then
                        if(CInt(i) = CInt(itrObj.propImgAbNumber)) then
                           txtFrameA.contents = CStr(itrObj.propImgArticle)       
                           charStyleA.ApplyTo txtFrameA.TextRange
                           parStyle.ApplyTo txtFrameA.TextRange
                           set txtFrameA = Nothing
                             set rectRefA = Nothing
            
                             if(not CStr(itrObj.propImgArticle) = "OUTLINE") then
                                if(validChecksum(CStr(itrObj.propImgArticle), CStr(itrObj.propImgBarcode)) = True) then
                                   dim nettBarcode
                         nettBarcode = CStr(itrObj.propImgBarcode)
                         dim rawBarcode
                         rawBarcode = Left(nettBarcode, 1)
                         For bar = 2 to 13
                            if(bar < 8) then
                               rawBarcode = rawBarcode & FirstSix(Left(nettBarcode, 1), bar, Mid(nettBarcode, bar, 1))
                               if(bar = 7) then
                                  rawBarcode = rawBarcode & "*"
                               end if
                            elseif(bar > 7) then
                               rawBarcode = rawBarcode & LastSix(Mid(nettBarcode, bar, 1))
                            end if
                         Next
                         rawBarcode = rawBarcode & "+"
                                   txtFrameB.contents = rawBarcode
            
                                   charStyleB.ApplyTo txtFrameB.TextRange
                         parStyle.ApplyTo txtFrameB.TextRange
                                else
                                   txtFrameB.contents = errMessage
            
                                   charStyleA.ApplyTo txtFrameB.TextRange
                         parStyle.ApplyTo txtFrameB.TextRange
                                end if
                             else
                                txtFrameB.contents = ""
                             end if
                             set txtFrameB = Nothing
                             set rectRefB = Nothing
                        end if
                     end if   
                  next
            
                  bottomMeas = destDocABrect(3) - txtHeightA - txtHeightB
                  destDocABrect(3) = bottomMeas 'BOTTOM
                  appRef.Documents("tmpPortfolio").artboards(appRef.Documents("tmpPortfolio").artboards.count).artboardRect = destDocABrect
            
            
                  ''''paste selection
                  appRef.Documents("tmpPortfolio").Paste
                  selectedObjects = appRef.Documents("tmpPortfolio").Selection
                  For j = 0 to UBound(selectedObjects)
                     set topobject = selectedObjects(j)
                     topobject.top = topMeas - 300 + (topobject.height / 2) 'TOP
                     topobject.left = leftMeas + 300 - (topobject.width / 2) 'LEFT
                     set topobject = Nothing
                  Next
            
                  destDocColNumber = destDocColNumber + 1
            
                  set selectedObjects = Nothing
                  set currDocABname = Nothing
                  set currDocABrect = Nothing
                  set destDocABrect = Nothing
                  set destDocAB = Nothing
               Next
               set currDocLayerState = Nothing
               currDoc.Close(2)
               set currDocLayerState = Nothing
               set currDoc = Nothing
            next
            
            lockHideLayers appRef.Documents("tmpPortfolio"), tmpDocumentLayerState
            
            set parStyle = Nothing
            set charStyleA = Nothing
            set charStyleB = Nothing
            set ArrReaderLines = Nothing
            set ArrReaderLinesStyles = Nothing
            set docPreset = Nothing
            set appRef = Nothing
            ''''ARTBOARDS
            
            
            ''''FUNCTIONS
            Function unlockUnhideLayers(doc)
               dim layerCount
               layerCount = doc.Layers.Count
               dim layerState()
               redim layerState(layerCount - 1, 1)
               For i = 1 to layerCount 
                  set iLayer = doc.Layers(i)
                  layerState(i - 1, 0) = iLayer.visible
                  layerState(i - 1, 1) = iLayer.locked
                  iLayer.visible = true
                  iLayer.locked = false
                  set iLayer = Nothing
               Next
               unlockUnhideLayers = layerState
            End Function
            Sub lockHideLayers(docOut, layerStateOut)
               dim layerCount
               layerCount = docOut.Layers.Count
               For i = 1 to layerCount
                  set iLayer = docOut.Layers(i)
                  iLayer.visible = layerStateOut(i - 1, 0)
                  iLayer.locked = layerStateOut(i - 1, 1)
               Next
            End Sub
            Function validChecksum(nettArticle, nettBarcode)
               '12 = 1, 11 = 2, 10 = 3 etc
               dim oddEven, odds, evens
               odds = 0
               evens = 0
               for ind = 12 to 1 step - 1
                  oddEven = ind Mod 2
                  if(oddEven = 0) then
                     odds = odds + Mid(nettBarcode, ind, 1)
                  else
                     evens = evens + Mid(nettBarcode, ind, 1)
                  end if
               next
            
               dim resultA, resultB, resultC
               resultA = (3 * odds) + evens
               resultB = (3 * odds) + evens
               oddEven = resultB mod 10
               Do Until oddEven = 0
                  resultB = resultB + 1
                  oddEven = resultB mod 10
               Loop
               resultC = resultB - resultA
               if(CInt(resultC) = CInt(Mid(nettBarcode, 13, 1))) then
                  validChecksum = True
               else
                  errMessage = "NaB - " & nettBarcode & " CSum=" & resultC
                  validChecksum = False
               end if
            End Function
            Function FirstSix(prefixDigit, suffixDigitIndex, suffixDigit)
               dim useTable
               useTable = "A"
               Select Case prefixDigit
                  Case 1
                     Select Case suffixDigitIndex : Case 4, 6, 7 : useTable = "B" :  End Select
                  Case 2
                     Select Case suffixDigitIndex : Case 4, 5, 7 : useTable = "B" :  End Select
                  Case 3
                     Select Case suffixDigitIndex : Case 4, 5, 6 : useTable = "B" :  End Select
                  Case 4
                     Select Case suffixDigitIndex : Case 3, 6, 7 : useTable = "B" :  End Select
                  Case 5
                     Select Case suffixDigitIndex : Case 3, 4, 7 : useTable = "B" :  End Select
                  Case 6
                     Select Case suffixDigitIndex : Case 3, 4, 5 : useTable = "B" :  End Select
                  Case 7
                     Select Case suffixDigitIndex : Case 3, 5, 7 : useTable = "B" :  End Select
                  Case 8
                     Select Case suffixDigitIndex : Case 3, 5, 6 : useTable = "B" :  End Select
                  Case 9
                     Select Case suffixDigitIndex : Case 3, 4, 6 : useTable = "B" :  End Select
               End Select
            
               dim useChar
               useChar = "NaN"
               Select Case suffixDigit
                  Case 0
                     Select Case useTable : Case "A" : useChar = "A" : Case "B" : useChar = "K" : End Select
                  Case 1
                     Select Case useTable : Case "A" : useChar = "B" : Case "B" : useChar = "L" : End Select
                  Case 2
                     Select Case useTable : Case "A" : useChar = "C" : Case "B" : useChar = "M" : End Select
                  Case 3
                     Select Case useTable : Case "A" : useChar = "D" : Case "B" : useChar = "N" : End Select
                  Case 4
                     Select Case useTable : Case "A" : useChar = "E" : Case "B" : useChar = "O" : End Select
                  Case 5
                     Select Case useTable : Case "A" : useChar = "F" : Case "B" : useChar = "P" : End Select
                  Case 6
                     Select Case useTable : Case "A" : useChar = "G" : Case "B" : useChar = "Q" : End Select
                  Case 7
                     Select Case useTable : Case "A" : useChar = "H" : Case "B" : useChar = "R" : End Select
                  Case 8
                     Select Case useTable : Case "A" : useChar = "I" : Case "B" : useChar = "S" : End Select
                  Case 9
                     Select Case useTable : Case "A" : useChar = "J" : Case "B" : useChar = "T" : End Select
               End Select
               FirstSix = useChar
            End Function
            Function LastSix(suffixDigit)
               dim useChar
               useChar = "NaN"
               Select Case suffixDigit
                     Case 0
                        useChar = "a"
                     Case 1
                        useChar = "b"
                     Case 2
                        useChar = "c"
                     Case 3
                        useChar = "d"
                     Case 4
                        useChar = "e"
                     Case 5
                        useChar = "f"
                     Case 6
                        useChar = "g"
                     Case 7
                        useChar = "h"
                     Case 8
                        useChar = "i"
                     Case 9
                        useChar = "j"
               End Select
               LastSix = useChar
            End Function
            ''''FUNCTIONS
            
            ''''INLINE CLASS
            Class ReaderLines
               Public propIdStyle
               Public propImgOutline
               Public propImgAbNumber
               Public propImgArticle
               Public propImgBarcode
            End Class
            ''''INLINE CLASS