Skip to main content
Known Participant
October 9, 2022
Answered

Anchored images disappear after script replaces text

  • October 9, 2022
  • 4 replies
  • 2527 views

I have a script that changes the encoding of an Indic document to Unicode by iterating over textStyleRanges. The encoding change is based on existing well-tested JS code and works great except that the anchors text-colors all move or disappear on conversion.

 
 
function ascii2unicode(text){
        var words = text.split(' ');
    
        // To stote converted words
        var op_words = [];
    
        // Process and append to main array
        words.forEach(function(word, k, arr){
            op_words.push('mwe-text');                      
        });
    
        // Return converted line
        return op_words.join(' ');
    }
    
    var doc = app.activeDocument;
    var stories = doc.stories;
    var textStyleRanges = stories.everyItem().textStyleRanges.everyItem().getElements();
    
    for (var i = textStyleRanges.length-1; i >= 0; i--) {
        var myText = textStyleRanges[i];
        if (myText.appliedFont.fontFamily.toLowerCase().indexOf('nudi') == 0) { 
            var converted = ascii2unicode(myText.contents);
            if (myText.contents != converted) {  
                myText.contents = "";                 
                myText.appliedFont = app.fonts.item("Tunga");
                myText.contents = converted; 
                myText.composer="Adobe World-Ready Paragraph Composer";
            }
        }
    } 

 

This topic has been closed for replies.
Correct answer Marc Autret

OK, not outside the realm of triviality. Something like this might work. Still don't know what's going on with your asciiToUnicode function, and if the length of the text you're replacing changes, then the anchor is going to move with it.  

 

 

var doc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedFont = "Tunga";
app.findGrepPreferences.findWhat = "[^~a]+";
var fs = doc.findGrep();
var i = fs.length;
while(i--) {
    fs[i].contents = ascii2unicode(fs[i].contents);
}

 

 


Hi all,

 

@brian_p_dts's approach seems to me the way to go—as long as GREP slow perfs are not an issue.

 

Anyway, the original problem is interesting: how to skip over problematic characters (anchored objects U+FFFC, but also tables U+0016, note references U+0004, and so on) when processing an array of Text Style Ranges?

 

That's tricky! We probably need to sub-split each individual text range with respect to special characters that must be preserved. But then we have to resolve the sub-ranges and take care of text indices while replacing the contents.

 

Here is a quick proof of concept:

TextConverterPattern.jsx 

 

Should solve the particular issue that @Suhaib Husain posed. But above all, this might serve as a skeleton for more general converters intended to run fast on long documents. Of course, special text containers like notes or tables are not adressed here—since the target is myDoc.stories.everyItem().textStyleRanges.everyItem().

 

Hope that helps.

 

Best,

Marc

4 replies

Participant
October 21, 2022

Ford employees frequently access the website MyFordBenefits, one of the most popular Ford websites. As a Ford employee who recently discovered the MyFordBenefits website, we have put together a detailed article on the website, which includes a guide to how to log into MyFordBenefits. https://my-ford-benefits.live/ 

brian_p_dts
Community Expert
Community Expert
October 9, 2022

If you use .contents to replace you're going to wipe out the insertion points containing anchors. You can try using findText() instead.  

Known Participant
October 9, 2022

Could please help me make the necessary changes in the script, I am farely novice when it comes to scripting

James Gifford—NitroPress
Legend
October 9, 2022

I'm not a script wizard, but I think you are overlooking that anchors are, in effect, just another character in the text string. Your replacement of the text is deleting the anchors, which could be for any of several specific technical reasons.

 

It seems to me that the script should check for all hidden and formatting characters and specifically 'do nothing' with them.

 

Known Participant
October 9, 2022

Thannks James, i will check and share if it works

James Gifford—NitroPress
Legend
October 9, 2022

I think this identifies the problem; I am not enough of a script expert to help modify the code. There are some very good scripters here, though.

 

Known Participant
October 9, 2022

Plesae refer to this code, i mistakenly pasted it into the body part

function ascii2unicode(text){
         var words = text.split(' ');
     
         // To stote converted words
         var op_words = [];
     
         // Process and append to main array
         words.forEach(function(word, k, arr){
             op_words.push('mwe-text');                      
         });
     
         // Return converted line
         return op_words.join(' ');
     }
     
     var doc = app.activeDocument;
     var stories = doc.stories;
     var textStyleRanges = stories.everyItem().textStyleRanges.everyItem().getElements();
     
     for (var i = textStyleRanges.length-1; i >= 0; i--) {
         var myText = textStyleRanges[i];
         if (myText.appliedFont.fontFamily.toLowerCase().indexOf('nudi') == 0) { 
             var converted = ascii2unicode(myText.contents);
             if (myText.contents != converted) {  
                 myText.contents = "";                 
                 myText.appliedFont = app.fonts.item("Tunga");
                 myText.contents = converted; 
                 myText.composer="Adobe World-Ready Paragraph Composer";
             }
         }
     }

 

Known Participant
October 9, 2022

@Marc Autretcould you please suggest something