VBScript Working with layers
Copy link to clipboard
Copied
Hello everybody!
I would like to use a script to work whith layers in InDesign CS3 but I can't find any reference for VBS.
The only one I have is "Set myLayer = myDocument.Layers.Add" to add a new layer. But, how to delete one or import one from another file, i.e. ?
Who can help me? Many thanks in advance!
Eliane
Copy link to clipboard
Copied
Yes, documentation for the latest versions would be nice. For now, see the Layer Methods in the InDesign Scripting Guide for CS2 and a separate document abourt changes in CS3 (nothing to do with Layers, but a must-read).
http://www.adobe.com/products/indesign/pdfs/InDesign_Scripting_Guide.pdf
You could use an Object Browser that shows all available properties and methods (already shown in earlier posts):
http://www.teusdejong.nl/indesign/IDBrowser.zip
There is no import method for layers. To copy a layer from another document, I use a simple method. The script tells to open both target and source document, turn on 'remember layers when copying', make only layers to copy visible. Cycle through all speads with the following: go to source doc/spread, selectall objects, copy, paste in place to target doc/spread.
Tony
Copy link to clipboard
Copied
Thanks for the links!
I already found a scripting documentation for CS3 under http://wwwimages.adobe.com/www.adobe.com/products/indesign/scripting/pdfs/InDesignCS3_ScriptingGuide... but it's not so complete.
Furthermore in Introduction to scripting (http://wwwimages.adobe.com/www.adobe.com/products/indesign/scripting/pdfs/Adobe_Intro_to_Scripting1....) there is an explanation how to view the VBS object library in any Microsoft Office Application. But the Object Browser you mentioned may be more convinient to use.
Well, I now have some to study and will try to find out the correct syntax!
Copy link to clipboard
Copied
With the correct tools it doesn't take too long to find what you are looking for. Finally, to delete a layer use: myDocument.ActiveLayer.Delete.
Tony, I will follow your tip for the "import" of a layer through the copy procedure. Many thanks for your quick help!!!
Eliane
Copy link to clipboard
Copied
Please disregard this message (att. too large), see the next one.
Message was edited by: Tony Tuneson
Copy link to clipboard
Copied
You need the Scripting Reference, not only the Guide.
I can't find it anymore on the Net quickly, so I made link for you:
https://www.clickanad.com/download/InDesign_Script_Model.zip
Filesize: 7,31 MB. This file will be downloadable until Wednesday, 22 April 2009
Good luck
T
Copy link to clipboard
Copied
Hi Tony,
sorry, but I'm still working on the script to copy a layer from another document. I guess I'm almost done, but I still can't find the proper syntax to select all objects of the layer... Could you help me? Sorry, I'm quite new in wrinting scripts for InDesign!
Many thanks in advance.
Eliane
Copy link to clipboard
Copied
This is what woked for me; I can see some things that I would do differently now, but it works.
It's in VB6, not too different from your VBS, I hope.
Sub CopyLayers(IDSource As InDesign.Document, IDTarget As InDesign.Document)
Dim i As Long
Dim j As Long
Dim PageName As String
Dim theLayoutWindow As InDesign.LayoutWindow
Dim mySpread As InDesign.Spread
Dim SelCounter As Long
Dim LayerFound As Boolean
Dim myLayer As InDesign.Layer
Dim itemCount As Long
For i = 0 To LayerList.ListCount - 1
For j = 1 To IDSource.Layers.Count
If LayerList.List(i) = IDSource.Layers.Item(j).Name Then
IDSource.Layers.Item(j).Visible = LayerList.Selected(i)
If LayerList.Selected(i) Then
LayerFound = True
End If
End If
Next j
Next i
myIndesign.ClipboardPreferences.PasteRemembersLayers = True
If Not LayerFound Then
MsgBox "No Layer selected. Exiting."
Exit Sub
End If
For j = 1 To IDTarget.Layers.Count
IDTarget.Layers.Item(j).Visible = False
DoEvents
Sleep (50)
DoEvents
Next j
For i = 0 To LayerList.ListCount - 1
LayerFound = False
For j = 1 To IDTarget.Layers.Count
If LayerList.List(i) = IDTarget.Layers.Item(j).Name Then
If LayerList.Selected(i) = True Then
IDTarget.Layers.Item(j).Visible = True
DoEvents
Sleep (50)
DoEvents
End If
LayerFound = True
End If
Next j
If (Not LayerFound) And LayerList.Selected(i) Then
Set myLayer = IDTarget.Layers.Add
myLayer.Name = LayerList.List(i)
End If
Next i
For i = IDSource.Spreads.Count To 1 Step -1
DoEvents
If CopyImagesBTN.Caption = "Start" Then
MsgBox "User aborted."
Exit For
End If
Set mySpread = IDSource.Spreads.Item(i)
Set theLayoutWindow = myIndesign.LayoutWindows.Item(1)
myIndesign.ActiveDocument = IDTarget
ZoomTo IDTarget.Spreads.Item(i)
myIndesign.ActiveDocument = IDSource
ZoomTo IDSource.Spreads.Item(i)
IDSource.Select InDesign.idSelectAll.idAll, idReplaceWith
If IDSource.Selection.Count > 0 Then
IDSource.Select InDesign.idSelectAll.idAll, idReplaceWith
myIndesign.Copy
myIndesign.ActiveDocument = IDTarget
Set mySpread = IDTarget.Spreads.Item(i)
IDTarget.Select idSelectAll.idAll, idReplaceWith
For itemCount = IDTarget.Selection.Count To 1 Step -1
IDTarget.Selection.Item(itemCount).Locked = False
IDTarget.Selection.Item(itemCount).Delete
Next itemCount
myIndesign.PasteInPlace
IDTarget.Select idNothingEnum.idNothing
End If
Next i
End Sub
Copy link to clipboard
Copied
Hi Tony,
many thanks for your script! Unfortunately the VBScript syntax is slightly different from Visual Basic. I used to write in VBA for Microsoft Word and now started with VBScript, but I never wrote in Visual Basic. Since my script is almost finished and is a part of a bigger one, I would like to have it completely in VBScript.
The command I'm looking for corresponds to your "IDSource.Select InDesign.idSelectAll.idAll, idRepplaceWith". I think, the best thing is to post a new thread asking for the syntax to select all objects in VBScript (which I will do in a few minutes).
Again many thanks for your help!
Eliane
Copy link to clipboard
Copied
Eliane_Yanou wrote:
many thanks for your script! Unfortunately the VBScript syntax is slightly different from Visual Basic. I used to write in VBA for Microsoft Word and now started with VBScript, but I never wrote in Visual Basic. Since my script is almost finished and is a part of a bigger one, I would like to have it completely in VBScript.
syntax is exactly the same the one difference I found is with For...To...Next - in VB6 (not VB.Net) you need to add variable name after Next - in VBS you should leave Next alone
robin
--
www.adobescripts.co.uk
Copy link to clipboard
Copied
Hi Robin,
I also already found out that in VBS you should leave Next alone in the For...To...Next loop. But this is not the only thing as "select all object" didn't work neither... But we are discussing this point in another thread.
Anyhow, I will let you now when I find the solution!
Eliane
Copy link to clipboard
Copied
Tony Tuneson wrote:
To copy a layer from another document, I use a simple method. The script tells to open both target and source document, turn on 'remember layers when copying', make only layers to copy visible. Cycle through all speads with the following: go to source doc/spread, selectall objects, copy, paste in place to target doc/spread.
but you will lose links between TextFrames in Story ...
robin
--
www.adobescripts.co.uk
Copy link to clipboard
Copied
OK, I will have to check that when I will be finished with the first part...
Tanks!
Eliane
Copy link to clipboard
Copied
you have nothing to check I'm not guessing
you will lose your threading - if Story is on different spreads
as workaround - you need to mark (by Label) all TextFrames in Story and recreate linked order
robin
www.adobescripts.co.uk
Copy link to clipboard
Copied
OK, I must admit that I don't know InDesign very well since I'm not creating the documents myself...
What I meant is that I will have to check how the docs are made and will have to find a compromise depending on the structure of the documents.
But you gave me very valuables tips how to try to find a solution. Thanks!
Eliane
Copy link to clipboard
Copied
That's a fact. If the doc is not too large, you could set all pages as a big spread first, and do one big select copy/paste.
Message was edited by: Tony Tuneson; removed "Or label the linked textframes and relink them after copying."
Copy link to clipboard
Copied
Hi,
I wan to count how many layers have in a indesign document.
What does the code to make this? I need in javascript. Could you help me please?
I write this code, but is not correct.
var n=app.activeWindow.Layers.count;
alert("Layers is " + n);
Thanks.
Copy link to clipboard
Copied
var myDoc = app.activeDocument;
var myLayersCount = myDoc.layers.length;
var myMessage = (myLayersCount == 1)?"There is 1 layer in the document.":"There are " + myLayersCount + " layers in the document."
alert(myMessage);

