Skip to main content
miloes329
Known Participant
December 1, 2022
Answered

why does find object only can change the first one?

  • December 1, 2022
  • 3 replies
  • 1118 views

I try a simple script

but I don't know why just only change the first one

anyone can tell me what's the reason and I tried wrong?

thanks a lot

 

 

app.findObjectPreferences = null;
app.changeObjectPreferences = null;
app.findObjectPreferences.appliedObjectStyles = "Japan_one";
app.changeObjectPreferences.anchorXoffset = 210;
app.changeObject();

 

 

app.findObjectPreferences = null;
app.changeObjectPreferences = null;
app.findObjectPreferences.appliedObjectStyles = "Japan_two";
app.changeObjectPreferences.anchorXoffset = 230;
app.changeObject();

This topic has been closed for replies.
Correct answer m1b

I had try to set the objectstyle before, but it doesn't work......

var myDoc = app.activeDocument;

myDoc.objectStyles.itemByName("Japan_one").anchoredObjectSettings.anchorXoffset = 210;

 

I checked the "Japan_one" style changed the value to 210.......

but nothing happen..............so I don't know how to make it work......

 


Oh yes. What you did is correct, but there is an extra part: because in your documents the Japan_one object style has been overridden on the objects (see the + symbol in the Object Styles panel). So your script needs to clear those overrides. There is a method of TextFrame that does it. See this script:

function main() {

    var docs = app.documents,
        counter = 0;

    // change all open documents
    for (var d = 0; d < docs.length; d++) {

        var doc = docs[d];
        var myStyle = doc.objectStyles.itemByName("Japan_one");

        if (!myStyle.isValid)
            continue;

        // first set correct value in the object style
        myStyle.anchoredObjectSettings.anchorXoffset = "210 mm";

        app.findObjectPreferences = null;
        app.changeObjectPreferences = null;
        app.findObjectPreferences.appliedObjectStyles = "Japan_one";

        var found = doc.findObject();
        counter += found.length;
        for (var i = 0; i < found.length; i++)
            // then clear overrides for each found object
            found[i].clearObjectStyleOverrides();
    }

    alert('' + counter + ' objects changed.');
};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Fix Object Style');

 

 I hope you can see what's happening there. But remember, clearing overrides means that if other changes were made, to diverge from the object style, then those changes will be lost. (That might be a good thing, it depends.)

- Mark

3 replies

Community Expert
December 1, 2022

Works for me. Are you sure there are multiple pageitems with these object styles applied. Did you try to run this find/change manually? 

-Manan

-Manan
miloes329
miloes329Author
Known Participant
December 1, 2022

hi @Manan Joshi , I try to run this find/change manually its work....

Community Expert
December 1, 2022

@miloes329 please share a sample document for us to test.

-Manan

-Manan
m1b
Community Expert
Community Expert
December 1, 2022

Oh and just checking that you know that if an object isn't anchored then anchorXoffset won't do anything to it.

- Mark

m1b
Community Expert
Community Expert
December 1, 2022

Hi @miloes329, I ran your code and it worked fine for me. Try adding an alert that shows how many it changed:

app.findObjectPreferences = null;
app.changeObjectPreferences = null;
app.findObjectPreferences.appliedObjectStyles = "Japan_one";
app.changeObjectPreferences.anchorXoffset = 210;
var changedObjects = app.changeObject();
alert('' + changedObjects.length + ' objects changed.');

Did you get the expected number of objects?

- Mark

m1b
Community Expert
Community Expert
December 1, 2022

A good test to do would be to:

1. run your script and take notice of which object was changed

2. delete that object

3. run script again — did it change the next object? or nothing?

- Mark

miloes329
miloes329Author
Known Participant
December 1, 2022

hi @m1b , it just show me "Result: undefined"

I try it individual is work in the first page

another page was nothing happen (I had total 20 pages, every page had this two anchor object)

 

I changed Japan_one, it was happened on page 1

change Japan_two, also happened on page 1

but combine Japan_one and Japan_two, just the Japan_one happened on page 1

 

I had no idea how to make it work on whole document and with 2 objecstyle........