Copy link to clipboard
Copied
How can I apply a condition (conditional text) to text pasted from the Clipboard?
Below is the code. The problems is that myStartCharacter gets updated when myInDesign.Paste() is executed. How can I keep myStartCharacter from getting updated when the text is pasted from the clipboard?
When myStartCharacter is updated, myStartCharacter becomes equal to myEndCharacter. (So my text range ends up being an insertion point.)
Dim myInDesign As InDesign.Application
myInDesign = CreateObject("InDesign.Application")
Dim myDocument As InDesign.Document = myInDesign.ActiveDocument
If myDocument.Selection.Count > 0 Then
Select Case TypeName(myDocument.Selection.Item(1))
Case "InsertionPoint"
myStartCharacter = myDocument.Selection.Item(1)
myInDesign.Paste()
myEndCharacter = myDocument.Selection.Item(1)
myText = myDocument.Stories.Item(myStartCharacter.ParentStory.Index).Texts.ItemByRange(myStartCharacter, myEndCharacter).Item(1)
myText.ApplyConditions(myDocument.Conditions.Item(1), True)
End Select
End If
Hi Tak Osato,
I have found one tricky solution to your problem.
If you are not much concerned about track changes in the document, then this is the solution for you.
The solution would be like:
Step 1: Turn Off Track changes in all the stories through script.
Step 2: Copy your content as per your wish, the just before pasting the content, turn on Track changes.
Step 3: Paste the content.
Step 4: Find changes of parent Story of your selected insertion points.
Step 5: If change type is inserted Te
...I found a way to do this. Instead of saving the insertion point (which I assume is a reference (pointer)), I decided to save the insertion point index.
Dim myInDesign As InDesign.Application
myInDesign = CreateObject("InDesign.Application")
Dim myDocument As InDesign.Document = myInDesign.ActiveDocument
If myDocument.Selection.Count > 0 Then
Select Case TypeName(myDocument.Selection.Item(1))
Case "InsertionPoint"
' Get the insertion point index instead of the insertion point itself.
' myStartCharacter
Copy link to clipboard
Copied
Hi Tak Osato,
you could add a new temp text frame to the document and paste the text there.
From that frame you could count characters, assign formatting and finally move the formatted text to an insertion point if you like.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Uwe Laubender,
Thank you for your response. I see what you mean (I can see that your method would work). I'll look into the other options provided in this thread and decide if I'll apply your method.
Thanks!
Copy link to clipboard
Copied
It's easier first to apply the condition to the insertion point, then paste the text -- the text will be in the condition.
P.
Copy link to clipboard
Copied
Hi Peter,
Thank you for your response.
I tried the following code, but it didn't work.
Private Sub PasteSelectedText(ByVal withFormatting As Boolean)
Dim myInDesign As InDesign.Application
myInDesign = CreateObject("InDesign.Application")
Dim myDocument As InDesign.Document = myInDesign.ActiveDocument
If myDocument.Selection.Count > 0 Then
Select Case TypeName(myDocument.Selection.Item(1))
Case "InsertionPoint"
myDocument.Selection.Item(1).ApplyConditions(myDocument.Conditions.Item(1), True)
' myInDesign.Paste()
myInDesign.PasteWithoutFormatting()
End Select
End If
End Sub
When I put a break right before pasting, I can see, in InDesign, that the condition is applied to the insertion point. But when the text is pasted (myInDesign.PasteWithoutFormatting()), the condition is cleared (not applied).
I think the code behaves in the same way as it would if you were to do this manually.
If you apply a condition manually to the insertion point in Indesign and then paste, the condition is not applied.
Thanks for the idea, though.
If you have a code that works, I would greatly appreciate it if you could share it.
Thanks,
Tak
Copy link to clipboard
Copied
Hi Tak Osato,
I have found one tricky solution to your problem.
If you are not much concerned about track changes in the document, then this is the solution for you.
The solution would be like:
Step 1: Turn Off Track changes in all the stories through script.
Step 2: Copy your content as per your wish, the just before pasting the content, turn on Track changes.
Step 3: Paste the content.
Step 4: Find changes of parent Story of your selected insertion points.
Step 5: If change type is inserted Text apply conditional text on it.
Refer this code sample for your reference :
////////////////////////////////////////////////////////////////////////////////////
var myDoc = app.documents[0];
app.documents[0].stories.everyItem().trackChanges = false;
app.documents[0].textFrames[1].words[1].select();
app.copy();
app.documents[0].textFrames[0].paragraphs[0].insertionPoints[-1].select();
app.documents[0].stories.everyItem().trackChanges = true;
app.paste();
var allChanges = app.documents[0].stories.everyItem().changes;
for(var i = 0; i < allChanges.length; i++){
if(!myDoc.conditions.item('PastedText').isValid){
var myCondition = myDoc.conditions.add();
myCondition.name = 'PastedText';
}
else{
var myCondition = myDoc.conditions.item('PastedText');
}
if(allChanges[i].changeType == "INSERTED_TEXT"){
allChanges[i].texts[0].appliedConditions = myCondition;
}
}
////////////////////////////////////////////////////////////////////////////////////
Best
Sunil
Copy link to clipboard
Copied
Sunil,
Wow! That's an interesting method to do this. Thanks for your idea!
Copy link to clipboard
Copied
I found a way to do this. Instead of saving the insertion point (which I assume is a reference (pointer)), I decided to save the insertion point index.
Dim myInDesign As InDesign.Application
myInDesign = CreateObject("InDesign.Application")
Dim myDocument As InDesign.Document = myInDesign.ActiveDocument
If myDocument.Selection.Count > 0 Then
Select Case TypeName(myDocument.Selection.Item(1))
Case "InsertionPoint"
' Get the insertion point index instead of the insertion point itself.
' myStartCharacter = myDocument.Selection.Item(1)
myStartCharacterIndex = myDocument.Selection.Item(1).Index
myInDesign.Paste()
myStartCharacter = myDocument.Stories.Item(myDocument.Selection.Item(1).ParentStory.Index).InsertionPoints(myStartCharacterIndex)
myEndCharacter = myDocument.Selection.Item(1)
myText = myDocument.Stories.Item(myStartCharacter.ParentStory.Index).Texts.ItemByRange(myStartCharacter, myEndCharacter).Item(1)
myText.ApplyConditions(myDocument.Conditions.Item(1), True)
End Select
End If
Thank you all for your input.
Tak