What I'm trying to do
- Find all instances of paragraphs with a certain paragraph style.
- Cut the paragraphs and create a text frame where each group of paragraphs was.
- Apply an object style to the new text frame.
- Copy the cut text into the new text frame.
- Convert the new text frame to an interactive text field
Why I'm trying to do it
I have a document with a bunch of blank lines printed in it so users can fill in their notes. I accomplish this with carriage returns that have a Ruled paragraph that adds a full width paragraph underline. I want to also create an interactive PDF that has form fields for users to add their notes digitally. My hope is that I can take my current document, run a script on it, and export a PDF ready to go.
What isn't working
Currently, my javascript creates a new text box for each paragraph. But I have multiple lines for print users to add notes, and only need one text field for digital users. So I need a way to select all instances of multiple paragraphs with the same paragraph style.
My code
var myDoc = app.activeDocument; // The open document: the scene of the action
var myParas = app.selection[0].parentStory.paragraphs; // The collection of paragraphs
var myParaCount = app.selection[0].parentStory.paragraphs.length; // The number of paragraphs in the story
// Make a reference to the paragraph style for captions
var paraStyleSelect = app.activeDocument.paragraphStyleGroups.item("Main").paragraphStyles.item("Ruled");
var objectStyleSelect = app.activeDocument.objectStyles.item("Fillable Form");
// Loop to make the change on every paragraph that has the selected style
for (i = myParaCount - 2; i >= 0; i--) {
if (myParas[i].appliedParagraphStyle == paraStyleSelect) {
var myNewTextFrame = myParas[i + 1].insertionPoints[0].textFrames.add(); // Make a new text frame
myNewTextFrame.appliedObjectStyle = objectStyleSelect;
myParas[i].move(LocationOptions.AT_BEGINNING, myNewTextFrame.insertionPoints[0]); // Move the paragraph into the new text frame at the first insertion point
// Convert text box to fillable form text field
app.select(myNewTextFrame);
var convertToTextField = app.menuActions.itemByName("$ID/$$$/Command/TextField"); // Menu command "Convert To Text Field":
// If available:
if (convertToTextField.enabled) {
convertToTextField.invoke();
};
}
}
alert("High five!");