Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Select anchored object and apply object style

Participant ,
Feb 20, 2018 Feb 20, 2018

Hi friends, I'm needing a script to select an anchored object with ".ai" extension and apply an object style. Thanks for the help of your friends.

Capturar.JPG

TOPICS
Scripting
2.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 20, 2018 Feb 20, 2018

First you need to get a list of all anchored objects in all stories of the document. Then you need to test the file path for the link reference. If the file path ends with ".ai" then apply the object style. Here is a sample written in AppleScript. Hope this helps.

set styleName to "Photo"

tell application "Adobe InDesign CC 2018"

  set docRef to document 1

  set styleRef to object style styleName of docRef

  set myList to every spline item of every text frame of docRef

  repeat with i from 1 to length of myList

  set thisFrame to item i of myList

  try

  set thisImage to image 1 of thisFrame

  set linkRef to item link of thisImage

  set thisTest to file path of linkRef

  if thisTest ends with ".ai" then

  set applied object style of thisFrame to styleRef

  end if

  end try

  end repeat

end tell

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2018 Feb 20, 2018

Hi,

you would go for the allGraphics array of the document. Loop that and test for e.g. ai as suffix in itemLink.name and also test for parent.parent.constructor.name == "Character" for anchored. Mabe also for imageTypeName "Adobe PDF".

// ExtendScript (JavaScript)

// Detect an anchored frame where the graphic contents is an ai-file

// Apply an object style to it.

( function()

{

  

    if(app.documents.length == 0){ return };

  

    var objectStyleName = "ObjectStyleName"; // Edit and insert your object styles's name here

  

    var doc = app.documents[0];

    var objectStyle = doc.objectStyles.itemByName( objectStyleName );

    if( !objectStyle.isValid ){ return };

  

    var allGraphicsArray = doc.allGraphics;

    var allGraphicsArrayLength = allGraphicsArray.length;

    for(var n=0;n<allGraphicsArrayLength;n++)

    {

        if( allGraphicsArray.parent.parent.constructor.name != "Character" ){ continue };

        if( allGraphicsArray.imageTypeName != "Adobe PDF" ){ continue };

      

        if( allGraphicsArray.itemLink.name.match(/\.ai$/i) )

        {

            allGraphicsArray.parent.appliedObjectStyle = objectStyle ;

        };

    }

}() )

Edit line 10 and insert your object style's name between the "".

Regards,
Uwe

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 21, 2018 Feb 21, 2018

Wordless to thank my friend, it worked perfectly.

But I would like to ask for other help, I forgot to mention that there are other ".AI" files in the archive.

So, how would the script be applied only to text frames with the paragraph style "QUESTION"?
Very grateful for your willingness to help, big hug, and once again, thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2018 Feb 21, 2018

flaviogomes_script  wrote

… So, how would the script be applied only to text frames with the paragraph style "QUESTION"?…

You have to do an additional check. See code below:

// ExtendScript (JavaScript)

// Detect an anchored frame where the graphic contents is an ai-file

// Apply an object style to it if the anchor is formatted with a distinct paragraph style

( function()

{

    if(app.documents.length == 0){ return };

    var objectStyleName = "ObjectStyleName"; // Edit and insert your object styles's name here

    var paragraphStyleName = "QUESTION"; // Edit and insert your para style's name here

    var doc = app.documents[0];

    var objectStyle = doc.objectStyles.itemByName( objectStyleName );

    if( !objectStyle.isValid ){ return };

    var appliedParaStyle = doc.paragraphStyles.itemByName( paragraphStyleName );

    if( !appliedParaStyle.isValid ){ return };

    var allGraphicsArray = doc.allGraphics;

    var allGraphicsArrayLength = allGraphicsArray.length;

    for(var n=0;n<allGraphicsArrayLength;n++)

    {

        if( allGraphicsArray.parent.parent.constructor.name != "Character" ){ continue };

        if( allGraphicsArray.imageTypeName != "Adobe PDF" ){ continue };

      

        var anchorCharacter = allGraphicsArray.parent.parent;

        if

        (

            allGraphicsArray.itemLink.name.match(/\.ai$/i) &&

            anchorCharacter.appliedParagraphStyle == appliedParaStyle

        )

        {

            allGraphicsArray.parent.appliedObjectStyle = objectStyle ;

        };

    }

}() )

Here I assume that the paragraph style in question is not part of a style group.

Regards,
Uwe

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 21, 2018 Feb 21, 2018

It worked perfectly, it is so good to be able to count on a capacity of friends. If the "question" style within a group, do I have to edit just as in the row below? Big hug and thank you very much. I'm looking to study more and more Java Script for Indesign, but it's amazing how much we can learn with the help of friends. Big hug.


  anchorCharacter.appliedparagraphStyleGroups.item("ATIVIDADES").paragraphStyles== appliedParaStyle

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 21, 2018 Feb 21, 2018

Hi Flavio,

you have to change line 13.

Here just an example where your paragraph style is nested one level deep in a paragraph style group named "Group 1":

var appliedParaStyle =

doc.paragraphStyleGroups.itemByName("Group 1").

paragraphStyles.itemByName( paragraphStyleName );

If the group that is holding your paragraph style is nested even deeper add that group to the hierarchy you are seeing in my code.

Regards,
Uwe

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 22, 2018 Feb 22, 2018
LATEST

WONDERFUL! @Laubender just have to thank you for your willingness to help. It worked perfectly and met all my needs. Thank you very much. I will always be studying so that one day I can follow his example and also be able to share knowledge. Big hug my friend.

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