Copy link to clipboard
Copied
Hi
I have been trying to convert an old cs3 script to CC with no luck. The script finds all the phone numbers in a document and changes the space between the number blocks from normal apces to be non breaking spaces. This is the original script. I have tried to convert it in CC but just ended up making a mess. Any help?
var random = Math.random();
var lstrNumber = parseInt((1000 - 1 + 1) * random + 1);
app.activeDocument.characterStyles.add({name:"NonBreakingSpace_" + lstrNumber});
app.findPreferences = null;
app.changePreferences = null;
app.findPreferences.findText = "^9 ^9";
app.changePreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item("NonBreakingSpace_" + lstrNumber);
var aStyle = app.activeDocument.characterStyles.item("NonBreakingSpace_" + lstrNumber);
var lobjFoundItems = app.activeDocument.search("^9 ^9",false, false, undefined, {},{appliedCharacterStyle:aStyle});
if( lobjFoundItems != null )
{
var lstrMessage = "Occurances found: " + lobjFoundItems.length;
}
app.findPreferences = null;
app.changePreferences = null;
app.findPreferences.findText = " ";
app.findPreferences.appliedCharacterStyle = aStyle;
app.changePreferences.changeText = "^s";
var lobjReplacedItems = app.activeDocument.search(app.findPreferences.findText, false, false, app.changePreferences.changeText);
app.activeDocument.characterStyles.item("NonBreakingSpace_" + lstrNumber).remove();
lstrMessage = lstrMessage + " Occurances changed: " + lobjReplacedItems.length;
alert(lstrMessage);
Copy link to clipboard
Copied
Hi jingo,
there is no more "search" on the document. This can now be done with findTextPreferences:
var random = Math.random();
var lstrNumber = parseInt((1000 - 1 + 1) * random + 1);
app.activeDocument.characterStyles.add({name:"NonBreakingSpace_" + lstrNumber});
app.findTextPreferences = NothingEnum.nothing;
app.changeTextPreferences = NothingEnum.nothing;
app.changeTextPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item("NonBreakingSpace_" + lstrNumber);
var aStyle = app.activeDocument.characterStyles.item("NonBreakingSpace_" + lstrNumber);
app.findTextPreferences.findWhat = "^9 ^9";
app.changeTextPreferences.appliedCharacterStyle = aStyle;
app.activeDocument.changeText();
Thanks
Stefan
Copy link to clipboard
Copied
The .search() function is pre-CS3. If that script worked in CS3, it was versioned. To get it to work in CC, (re)set the versioning, using
app.scriptPreferences.version = . . .
Not sure what the version string for CS2 is.