Quantcast
Channel: Adobe Community : Discussion List - Illustrator Scripting
Viewing all articles
Browse latest Browse all 3671

trouble with search and replace

$
0
0

I receive text documents from word which are a mess. I need to clean them before they go into the next stage of my workflow. I have a great script which cleans all of the text and it works fine, except for one thing – one of the characters in the copy is a square bullet. This square bullet needs to have 2 spaces preceding it in the final text. This snippet finds and replaces the square bullet with a unique symbol perfectly.

 

First Script

 

var search_string = / ▪/gi; // g for global search, remove i to make a case sensitive search 

var replace_string = "  Ω"; 

 

var text_frames = active_doc.textFrames; 

 

if (text_frames.length > 0) 

    for (var i = 0 ; i < text_frames.length; i++) 

      { 

          var this_text_frame = text_frames[i]; 

           var new_string = this_text_frame.contents.replace(search_string, replace_string); 

            

           if (new_string != this_text_frame.contents) 

               { 

                    this_text_frame.contents = new_string; 

               } 

      } 

}

 

The reason I chose to replace it with the Ω symbol is because it's likely not ever going to be found in the variety of word documents which I will be receiving.

 

The problem is that the square bullet in the final product needs to be produced in a specific font, Zapf Dingbats. The keystroke which is required to produce the square bullet in that font is a lowercase ‘n’. I thought I could find the Ω and replace it with a lowercase ‘n’ following another script I use which finds a specific character and applies a character style to it. Here is that script:

 

Second Script

 

var main = function() { 

  var doc; 

  if ( app.documents.length > 0 && app.activeDocument.textFrames.length > 0 ) { 

  // Set the value of the character to look for 

  searchCharacter1 = "Ω"; 

  // Iterate through all characters in the document 

  // and color the character that match searchWord 

  for ( i = 0; i < app.activeDocument.textFrames.length; i++ ) { 

  textArt = activeDocument.textFrames[i]; 

  for ( j = 0; j < textArt.characters.length; j++) { 

  character = textArt.characters[j]; 

  if ( character.contents == searchCharacter1 ) { 

  // character.filled = true; 

  // character.fillColor = CharacterColor; 

  app.activeDocument.characterStyles.getByName ( "Square Bullet" ).applyTo ( character, true ); 

  } 

  } 

  } 

  } 

 

 

 

var u; 

main();

 

 

var active_doc = app.activeDocument; 

 

var search_string = /Ω/gi; // g for global search, remove i to make a case sensitive search 

var replace_string = "n"; 

 

var text_frames = active_doc.textFrames; 

 

if (text_frames.length > 0) 

    for (var i = 0 ; i < text_frames.length; i++) 

      { 

          var this_text_frame = text_frames[i]; 

           var new_string = this_text_frame.contents.replace(search_string, replace_string); 

            

           if (new_string != this_text_frame.contents) 

               { 

                    this_text_frame.contents = new_string; 

               } 

      } 

}

 

The problem is, when I run the second script it successfully changes the character but it loses the character style?

 

I tried searchWord for "  n", but javascript doesn't like the spaces, yes I tried "\s\sn" and I tried "\u0020\u0020n". I can't replace with just an ‘n’ because then it would find every ‘n’ in the copy and replace them with square bullets.

 

All I want to do is clean the original text so I get "space space square bullet", then replace the square bullet with ‘n‘, then format just the ‘n’ with a character style named Square Bullet.

 

All of these scripts, which I have gratefully received from members of this forum, are set to execute throughout an entire document. Is there a way to restrict the execution of the scripts to a specific layer ONLY.

 

One last thing… I have read through the Illustrator Scripting Guide. I have skimmed through the Illustrator Scripting Reference - JavaScript and I have viewed the first four units of the JavaScript Essential Training course from Lynda.com. I'm kinda getting this but not fast enough. Any assistance would be helpful.

 

Marcrest


Viewing all articles
Browse latest Browse all 3671

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>