我想在InDesign CS3 vb.net脚本中对文本框架进行分组。它适用于InDesign 2.0,但不适用于InDesign CS3。这是我的代码:
Dim myDoc As InDesign.Document = Nothing
Dim myGroup As InDesign.Group = Nothing
Dim myObjectList(2)
myObjectList.SetValue(myOuterTextFrame, 0)
myObjectList.SetValue(myInnerTextFrame, 1)
myObjectList.SetValue(myContentTextFrame, 2)
myGroup = myDoc.Groups.Add(myObjectList)
获取错误“无法将类型为'System.Object []'的对象强制转换为'InDesign.Objects'。”
答案 0 :(得分:2)
我知道你很久以前就问过这个问题,所以我主要回答未来的搜索。我没有找到一个完全托管的方法来使用.Net Framework来做到这一点并相信我,我已经搜索过它。我已经尝试了一百万个不同的演员阵容,子类化,反思,你的名字。最终最终起作用的是JavaScript。下面是一个方法,它采用InDesign.Document对象和两个或多个表示InDesign项ID的整数。然后它创建一些JavaScript并让InDesign执行它。最后,它返回从这些对象创建的InDesign.Group。
Public Function GroupObjects(ByVal indesignDocument As InDesign.Document, ByVal ParamArray objectIds() As Integer) As InDesign.Group
'Sanity checks
If indesignDocument Is Nothing Then Throw New ArgumentNullException("indesignDocument")
If objectIds Is Nothing OrElse objectIds.Count < 2 Then Throw New ArgumentException("You must pass at least 2 object ids")
'We'll assign a unique label to the group that we create in JavaScript so that we can find it in managed code later
Dim GID = Guid.NewGuid().ToString()
'Create the JavaScript
Dim Buf As New StringBuilder()
Buf.AppendLine("var items = new Array();")
For Each ID In objectIds
Buf.AppendFormat("items.push(app.activeWindow.activePage.pageItems.itemByID({0}));", ID)
Buf.AppendLine()
Next
Buf.AppendLine("var g = app.activeWindow.activePage.groups.add(items);")
Buf.AppendFormat("g.label='{0}';", GID)
Dim IA = indesignDocument.Parent
IA.DoScript(Buf.ToString(), InDesign.idScriptLanguage.idJavascript)
'Loop through all document groups looking for the object with the label created above
For Each G As InDesign.Group In indesignDocument.Groups
If Not String.IsNullOrWhiteSpace(G.Label) AndAlso G.Label = GID Then Return G
Next
Return Nothing
End Function
要在你的代码中使用它,你会说:
Dim MyGroup = GroupObjects(myOuterTextFrame, myInnerTextFrame, myContentTextFrame)
答案 1 :(得分:1)
这个对我有用:
Type type = Type.GetTypeFromProgID("InDesign.Application");
Host = (InDesign.Application)Activator.CreateInstance(type);
InDesign.Objects o = Host.CreateCollection();
答案 2 :(得分:0)
我在InDesign脚本示例中找到了答案 - 霓虹灯示例脚本提供了分组示例