Highlighted

Script to insert an anchor text frame(located in library .indl) into a text frame by find/change grep?

Explorer ,
May 08, 2019

Copy link to clipboard

Copied

hello, i'm very new with InDesign and script. I was wonder if i can insert an anchor text frame(located in library .indl) into a text frame by find/change grep with script.

This is all i have. Anyone can help me with this? Really thanks.

function change1 (myLib){ 

          var libAssets = myLib.assets.everyItem().getElements(), libAsset, assetName, placedAsset = app.activeDocument; 

              while (libAsset = libAssets.pop()){

                   assetName = libAsset.name;

                   myLib.assets.item("button1").placeAsset(app.activeDocument);break;

                    //libAsset.placeAsset(app.documents.item(0))[0];

              }

}

function replaceText (input, output) 

     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing; 

     app.findTextPreferences.findWhat = input; 

     app.changeTextPreferences.changeTo = output; 

     app.activeDocument.changeText();

     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;  

function main() {

    var myLib=app.libraries.itemByName ("anhtu.indl"); //my Library

if (app.layoutWindows.length == 0) return;

if (app.selection.length != 1 || !app.selection[0].hasOwnProperty('changeGrep')) {

alert('Please select only one textframe or text range!');

return;

}

var changeObject = app.selection[0];

if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;

app.scriptPreferences.version = 7.5;

    replaceText ("hello", change1(myLib));  

}

main();

TOPICS
Scripting

Views

511

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Script to insert an anchor text frame(located in library .indl) into a text frame by find/change grep?

Explorer ,
May 08, 2019

Copy link to clipboard

Copied

hello, i'm very new with InDesign and script. I was wonder if i can insert an anchor text frame(located in library .indl) into a text frame by find/change grep with script.

This is all i have. Anyone can help me with this? Really thanks.

function change1 (myLib){ 

          var libAssets = myLib.assets.everyItem().getElements(), libAsset, assetName, placedAsset = app.activeDocument; 

              while (libAsset = libAssets.pop()){

                   assetName = libAsset.name;

                   myLib.assets.item("button1").placeAsset(app.activeDocument);break;

                    //libAsset.placeAsset(app.documents.item(0))[0];

              }

}

function replaceText (input, output) 

     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing; 

     app.findTextPreferences.findWhat = input; 

     app.changeTextPreferences.changeTo = output; 

     app.activeDocument.changeText();

     app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;  

function main() {

    var myLib=app.libraries.itemByName ("anhtu.indl"); //my Library

if (app.layoutWindows.length == 0) return;

if (app.selection.length != 1 || !app.selection[0].hasOwnProperty('changeGrep')) {

alert('Please select only one textframe or text range!');

return;

}

var changeObject = app.selection[0];

if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;

app.scriptPreferences.version = 7.5;

    replaceText ("hello", change1(myLib));  

}

main();

TOPICS
Scripting

Views

512

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 08, 2019 0
Adobe Community Professional ,
May 09, 2019

Copy link to clipboard

Copied

What is working?

What is not working for you?

Try to place the asset to an insertion point in the text.

FWIW: That cannot work if the asset consists of several page items that are not grouped.

So best strategy is: First place the asset to the document, then group all its components and as a final step anchor the group.

Anchoring of page items can be done through the anchoredObjectSettings of the page item you want to anchor with method insertAnchoredObject():

Adobe InDesign CS6 (8.0) Object Model JS: AnchoredObjectSetting

Regards,
Uwe

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 09, 2019 2
Explorer ,
May 12, 2019

Copy link to clipboard

Copied

hello, it's me again, thanks you for your help.

i was able to add a anchor object(it's text frame.Let's call this TX1) into a text frame(Let's call this TX2).

But what i want is, replace a word with TX1 in a paragraph in exact place of that word.

#target indesign

var mSourceIdx = app.selection[0].index;

var sel = app.selection;

myDoc = app.documents[0];

myObjectStyle = myDoc.objectStyles.item("icon");

myLib = app.libraries.itemByName("anhtu.indl");

function main(){

    if (app.layoutWindows.length == 0) return;

if (app.selection.length != 1 || !app.selection[0].hasOwnProperty('changeGrep')) {

alert('Chon phan dieu kien Kyushu muon chuyen doi');

return;

}

var changeObject = app.selection[0];

if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;

var doc = app.documents[0];

var style,text;

var scriptVersion = app.scriptPreferences.version;

app.scriptPreferences.version = 7.5;

var options = app.findChangeGrepOptions.properties;

app.findGrepPreferences = NothingEnum.NOTHING;

app.changeGrepPreferences = NothingEnum.NOTHING;

    //gan bien cho text va paragraph

    var text = sel[0];

    var para = text.parentStory;

    var i=null;

    try {

            for (i = 1; i < 7; i++){

            app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});   

            app.findGrepPreferences = null;

            // Thay icon ■最少催行人員/

            app.findGrepPreferences.findWhat = "■最少催行人員/"; //find this paragraph

            //var myFoundItems = app.activeDocument.findGrep();

            var myFound = myDoc.findGrep (true);

            app.select(myFound[0].insertionPoints[0]); 

            alert(myFound[0].contents+"is found");

             //what i really want to do here is insert asset in library in exact location of cursor or just replace the text with that asset. i'm kind of stuck here.

            app.changeGrepPreferences.properties = ({changeTo:""});

            myLib.assets.item ("催").placeAsset (para.insertionPoints[0])[0];   

            changeObject.changeGrep();

    }

}

        catch (e) {//alert(e + ' at line ' + e.line)

        }

}

function getStyleByString(docOrGroup, string, property) {

if (string == '[No character style]') return docOrGroup[property][0];

if (string == '[No paragraph style]') return docOrGroup[property][0];

if (string == 'NormalParagraphStyle') return docOrGroup[property][1];

stringResult = string.match (/^(.*?[^\\]):(.*)$/);

var styleName = (stringResult) ? stringResult[1] : string;

styleName = styleName.replace (/\\:/g, ':');

remainingString = (stringResult) ? stringResult[2] : '';

var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;

var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);

if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);

return styleOrGroup;

};

main();

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 12, 2019 0
Explorer ,
May 01, 2020

Copy link to clipboard

Copied

Hi @OfficialNeaven!

 

Although this nearly a year old question, I have a solution for you.


User Vinny from StackOverflow had described in this comment how can we insert a copied or cut object using GREP find and replace. Same thing is described at Favorite GREP Expressions You Can Use (under Fun Tricks).

 

Basically, you need to copy or cut an object, do a GREP find and replace. See the following script, where I create a text frame object, cut it and find a footnote reference that is not followed by an anchored object and insert the cut object right after the footnote reference. Note that the following script inserts the cut object multiple times (after each and every footnote reference) in a similar manner like the Change All button in the Find/Replace dialogue.

 

 

// Insert a text frame object
var textFrame = doc.pages[0].textFrames.add();
textFrame.properties = {
	appliedObjectStyle: objStyleSidenotes,
	contents : "Sample Text Frame"
};

// Clear the selection
app.select(null);

// Select the textFrame
doc.select(textFrame);

// Cut the text frame
app.cut();
// Or copy the text frame
// app.copy();

// Reset the GREP preferences
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;
app.findGrepPreferences = null;
app.changeGrepPreferences = null;
app.findChangeGrepOptions.properties = (
	{
		// If needed, you can change some of the following settings to `true`
		includeFootnotes: false,
		includeMasterPages: false,
		includeHiddenLayers: false,
		includeLockedLayersForFind: false,
		includeLockedStoriesForFind: false,
	}
);

// Set the GREP search preferences
app.findGrepPreferences.findWhat = '~F\\K[^~a]';
app.changeGrepPreferences.properties = (
	{
		// This is basically what you are looking for
		changeTo: '~c$0'
	}
);

// Find all matches
app.findGrep();

// Replaces all matches
doc.changeGrep();

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 01, 2020 0