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

Script to find/replace regular to Italic based on search terms in .txt

New Here ,
Feb 12, 2012 Feb 12, 2012

Copy link to clipboard

Copied

Sorry for such a newbie post.

I have been reading on JS scripts for find/replace.  I'm hoping develop a script or find one that is written that

1. Looks into a .txt or any searchable file format, containing search terms

2. Script will run on the entire Indesign document

3. For any matching search terms script replace the style from regular to Italic, or from Bold to Bold/Italic. 

TOPICS
Scripting

Views

3.1K

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
community guidelines
Community Expert ,
Feb 13, 2012 Feb 13, 2012

Copy link to clipboard

Copied

Perfectly doable. Do you want to give it a go yourself?

A few first thoughts (things I'd probably solve myself while writing the script):

1. JS can read any file, but getting just the plain text out of an MS Word document is kind of ambitious -- even for me. Keep it simple: if your search text is nothing but a list of keywords or short phrases, write them out in a plain text file, one search item per line.

2. With that I presume you mean not on one single continuous story, but all over the file -- including loose text frames, master pages, and what-have-you-got. Thass' easy, you can set the target for a Find or Replace operation with pin-point accuracy.

3. .. This is the interesting part, actually.

(a) Do you want to change the text style? That will set an override "+" for the modified text. It would be safer (wiser, etc.) to change it using a character style. (For the script it doesn't matter which method you use, this is just for the Future You who is going to edit this file in 6 months.)

(b) If you do use a character style: what should happen if there already is one applied? Not bold or italic, but underline, for instance. Applying a new char style will override this one.

(c) The naive approach (which I probably would have taken first) is to first search for each of the search terms, go over the 'found' list and test for each single entry if it's in regular or bold, then take appropriate action.

A better approach -- without knowing anything else about this document -- could be to blindly use Replace to change ALL search terms in "Regular" to "Italic" and ALL search terms in "Bold" to "Bold Italic". It sure would be faster.

(d) Just like in the interface, you can search for a word and 'change' only the formatting -- no need to change the text as well. And just like in the interface, you need to specify "whole words" and/or "case sensitive".

Votes

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
community guidelines
New Here ,
Feb 13, 2012 Feb 13, 2012

Copy link to clipboard

Copied

Thanks for the reply.  I'm still learning the Adobe commands and such.  I've done a bit of JS work for another platform, but I'm not sure how much work I'm going to be doing within Adobe.  So I might try it and let everyone get a laugh out of my way of coding, but its a good learning tool for understanding how the program works. 

1.  Yes I think a .txt file is likely the best, these are two word names for the most part. 

2.  I currently use Paragraph and Character styles.  I just find/replace using the built in function within Adobe and it preforms what i need.  Just looking to automate it.

3. a) I'd like to just have it do an overide of the stlye, the reason is that these words always have to Italic and we don't generaly go back to old documents.  Once we produce them they are set, baring some small changes at the beginning.  There are a lot of different character and paragraph styles in document, and the find/replace as I'm currently doing it works great and doesn't cause any issues.

     c) I will let it replace all from the list as any term in the list must be Italics.  Script will only do this one type of change. 

     d) yes I will just change the font style nothing else.  I think I saw the produedure listed in a recent post but I have to track it back down.

Thanks for the help.

Votes

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
community guidelines
Community Expert ,
Feb 13, 2012 Feb 13, 2012

Copy link to clipboard

Copied

Go ahead, make us laugh -- I promise we'll be gentle

Some previous experience with JS is good, it doesn't matter on what platform. The only thing to watch out for is things that you took for granted but were in fact not part of JS but of the surrounding DOM (Document Object Model). For example, if you worked with web pages, you would have used the HTML DOM without even being aware of it.

The InDesign DOM is comp-lick-a-ted ... but that's because InDesign is complicated! And in fact it's a good thing too; fortunately the programmers took care to make virtually every function in ID available to scripters (one exclusion, for example, seems to be "Show Text Threads" -- and no-one ever mentioned that in the decade I've been working with ID/JS, so it's not like it is something important).

To get your head around ID's DOM, I'll gladly recommend "the best" reference, that is, my own: http://www.jongware.com/idjshelp.html, because even though it contains the exact same text as Adobe's own help, mine looks nicer, is hyper-referenced, searchable, and shows a graphical hierarchy of most object relations.

Votes

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
community guidelines
New Here ,
Feb 13, 2012 Feb 13, 2012

Copy link to clipboard

Copied

Ok, so I have it working with the FindChangebyList that comes with Indesign.  I just put the style in the text search.

text{findWhat:"XXX"}{changeTo:"XXX",fontStyle:"Bold Italic"}{includeFootnotes:true, includeMasterPages:true, includeHiddenLayers:true, wholeWord:false}Find XXX and replace with XXX

I guess I'll have to learn how it works to make the last a bit easier to use.  I'll want just a list of terms in the end. I can script this with VBA in excel to give me a fresh .txt that currently works, but that really shows my inabilty to do good work.

Votes

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
community guidelines
Community Beginner ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

@Jongware It's funny you mentioned the "Show Text Threads" — and no one asking about it in decades — because it's something I've tried to sort out for 8 years, to no avail. I basically just figured it's not possible ... is that true — it's really not possible?

I'm effectively trying to consolidate a three-step Find/Change process that I do for every book project into a single script at the moment — appling individual character styles to all the Italic, Bold, and Superscript text. I haven't been able to sort out how to only change the formatting of text ... so I came across this post.

Funny.

Votes

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
community guidelines
Community Expert ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

Hi @s.vd.z , you can use the invoke() method to toggle the Show Text Threads menu item. There are over 4000 menuActions available:

 

 

 

var menuItem =  app.menuActions.itemByID(24332)
if(menuItem.enabled){  
    menuItem.invoke();  
};

 

 

 

You might want to start a new thread, but you can setup grep or text find and changes to do what you are looking for. Here are two grep F&C functions, one searches for a text style, the other for a position

 

 

 

//get the character styles you want to apply in the searches
var bs = app.activeDocument.characterStyles.itemByName("MyBold");
var is = app.activeDocument.characterStyles.itemByName("MyItalic");   
var ps = app.activeDocument.characterStyles.itemByName("MySuper");

//search for a font style with the character style to apply
styleSearch("Bold", bs)
styleSearch("Italic", is)

//search for a position with the character style to apply
//https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Position.html for possible positions
positionSearch(Position.SUPERSCRIPT, ps)

/**
* Search for a text style and apply a character style 
* @ param the text style to search for e.g. "Bold""
* @ param the character style to apply 
* @ return void 
*/
function styleSearch(s, cs){
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findGrepPreferences.fontStyle = s;
    app.changeGrepPreferences.appliedCharacterStyle = cs;
    app.activeDocument.changeGrep( true )
}


/**
* Search for a text position and apply a character style 
* @ param the position to search for e.g. Position.SUPERSCRIPT
* @ param the character style to apply 
* @ return void 
*/
function positionSearch(p, cs){
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findGrepPreferences.position = p;
    app.changeGrepPreferences.appliedCharacterStyle = cs;
    app.activeDocument.changeGrep( true )
}

 

 

Screen Shot 15.pngScreen Shot 14.png

 

Votes

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
community guidelines
Community Beginner ,
Jun 19, 2022 Jun 19, 2022

Copy link to clipboard

Copied

LATEST

@rob day Hey, thanks a lot for this. Indeed, kind of a mess addition to this thread — including two items — however, I appreciate your reply to both. This worked, for both.

Oddly, with the Find/Replace script of fontStyle to characterStyle, I think there was simply an issue with my script reading my Character Style names — e.g. "Bold — Template", "Italic — Template", etc. — it always gave me the error in the attached. I wasn't using Grep but text, and the script wasn't exactly the same; however, very similar. When I changed the names to match your example of "MyBold", etc. it worked fine in all instances. Perhaps the spaces don't work in this case ... though it's never been an issue with other scripts.

Anyway, all good. I really appreciate the help — issues resolved.

Thanks!

Votes

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
community guidelines