Skip to main content
Participant
December 2, 2010
Question

How to create, place, format and paste the clipboard contents into a text frame

  • December 2, 2010
  • 1 reply
  • 1330 views

I am new to scripting and need help. I have an existing Indesign document. I need to be able to create a text frame on my current page, the width of my margins with the top of the text frame at the top margin and 1" in height, then format it to be 1-column, and then paste the clipboard contents into it and tag the text with a particular paragraph style. I am using Indesign CS4 on a mac, if that makes any difference. Thanks for any help.

This topic has been closed for replies.

1 reply

tpk1982
Legend
December 2, 2010

Create a new document with check on "Master Text Frame" in the dialog box. In that dialog box you can enter head margin as well.

GCDuffordAuthor
Participant
December 2, 2010

Thanks, but what I am needing is a script that I can either assign a shortcut key to or at least just run from the scripting window. It is a repetitive thing that happens hundreds of times in the documents I am working on. There is already text lower down on the page, and I am cutting part of that text and placing and reformatting it at the top of the page, which has been left blank for this purpose.

George

tpk1982
Legend
December 2, 2010

May this will help you. It will create an anchored object with a text what you desired, with object style. You should create an object style before with the x and y co ordinates. You can choose either para style or character style.

var the_document = app.documents.item(0);

// Create a list of paragraph styles

var list_of_paragraph_styles = the_document.paragraphStyles.everyItem().name;

// Create a list of character styles

var list_of_character_styles = the_document.characterStyles.everyItem().name;

// Create a list of object styles

var list_of_object_styles = the_document.objectStyles.everyItem().name;

// Make dialog box for selecting the styles

var the_dialog = app.dialogs.add({name:"Create anchored text frames"});

with(the_dialog.dialogColumns.add()){

with(dialogRows.add()){

staticTexts.add({staticLabel:"Make the anchored frames from ..."});

}

with(dialogRows.add()){

staticTexts.add({staticLabel:"This character style:"});

var find_cstyle = dropdowns.add({stringList:list_of_character_styles, selectedIndex:0});

}

with(dialogRows.add()){

staticTexts.add({staticLabel:"Or this paragraph style:"});

var find_pstyle = dropdowns.add({stringList:list_of_paragraph_styles, selectedIndex:0});

}

with(dialogRows.add()){

staticTexts.add({staticLabel:"Leave one dropdown unchanged!"});

}

dialogRows.add();

with(dialogRows.add()){

staticTexts.add({staticLabel:"Delete matches?"});

var delete_refs = dropdowns.add({stringList:["Yes","No"], selectedIndex:0});

}

dialogRows.add();

with(dialogRows.add()){

staticTexts.add({staticLabel:"Anchored text frame settings:"});

}

with(dialogRows.add()){

staticTexts.add({staticLabel:"Object style:"});

var anchor_style = dropdowns.add({stringList:list_of_object_styles, selectedIndex:0});

}

with(dialogRows.add()){

staticTexts.add({staticLabel:"Frame width:"});

var anchor_width = measurementEditboxes.add({editUnits:MeasurementUnits.MILLIMETERS, editValue:72});

}

with(dialogRows.add()){

staticTexts.add({staticLabel:"Frame height:"});

var anchor_height = measurementEditboxes.add({editUnits:MeasurementUnits.MILLIMETERS, editValue:72});

}

}

the_dialog.show();

// Define the selected styles

var real_find_cstyle = the_document.characterStyles.item(find_cstyle.selectedIndex);

var real_find_pstyle = the_document.paragraphStyles.item(find_pstyle.selectedIndex);

var real_anchor_style = the_document.objectStyles.item(anchor_style.selectedIndex);

// Check if a style is selected

if(find_cstyle.selectedIndex != 0 || find_pstyle.selectedIndex != 0) {

// Define whether to search for character styles or paragraph styles

app.findChangeGrepOptions.includeFootnotes = false;

app.findChangeGrepOptions.includeHiddenLayers = false;

app.findChangeGrepOptions.includeLockedLayersForFind = false;

app.findChangeGrepOptions.includeLockedStoriesForFind = false;

app.findChangeGrepOptions.includeMasterPages = false;

if(find_cstyle.selectedIndex != 0) {

app.findGrepPreferences = NothingEnum.nothing;

app.findGrepPreferences.appliedCharacterStyle = real_find_cstyle;

} else {

app.findGrepPreferences = NothingEnum.nothing;

app.findGrepPreferences.appliedParagraphStyle = real_find_pstyle;

app.findGrepPreferences.findWhat = "^";

}

// Search the document

var found_items = the_document.findGrep();

myCounter = found_items.length-1;

do {

// Select and copy the found text

var current_item = found_items[myCounter];

if(find_pstyle.selectedIndex != 0) {

var found_text = current_item.paragraphs.firstItem();

var insertion_character = (found_text.characters.lastItem().index) + 1;

var check_insertion_character = insertion_character + 1;

var alt_insertion_character = (found_text.characters.firstItem().index) - 1;

var the_story = found_text.parentStory;

app.selection = found_text;

if(delete_refs.selectedIndex == 0) {

app.cut();

} else {

app.copy();

}

} else {

var found_text = current_item;

var insertion_character = (found_text.characters.lastItem().index) + 2;

var check_insertion_character = insertion_character;

var alt_insertion_character = (found_text.characters.firstItem().index) - 1;

var the_story = found_text.parentStory;

app.selection = found_text;

if(delete_refs.selectedIndex == 0) {

app.cut();

} else {

app.copy();

}

}

// Make text frame from selection

try {

app.selection = the_story.insertionPoints[check_insertion_character];

app.selection = the_story.insertionPoints[insertion_character];

} catch(err) {

app.selection = the_story.insertionPoints[alt_insertion_character];

}

var the_anchored_frame = app.selection[0].textFrames.add({geometricBounds:["0","0",anchor_height.editContents,anchor_width.editContents],anchoredObjectSettings:{anchoredPosition: AnchorPosition.ANCHORED}});

app.selection = the_anchored_frame.insertionPoints[0];

app.paste();

// Apply the object style now to "force apply" paragraph style set in the object style

if(anchor_style.selectedIndex != 0) {

the_anchored_frame.appliedObjectStyle = real_anchor_style;

}

myCounter--;

} while (myCounter >= 0);

} else {

alert("No styles selected!");

}