🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #8739

Re: Script that can convert character attribs to character stylesheets

#8739
From: Dave_Saunders
Date: Thu, 28 Sep 2006 11:10
84 lines
4013 bytes
Hi Martin,

Here's the script I use. I wrote it as a tool for a particular project that required me to preserve Bold, Italic, superscript, subscript, and certain Greek symbols.

//DESCRIPTION: Converts to character styles in selection (if any) or document (if not)

Object.prototype.isInArray = function(myArray){ 
  for (var i=0; myArray.length > i; i++) { 
    if(myArray[i] == this){ 
      return true; 
    } 
  } 
  return false; 
}

if (app.documents.length == 0) { exit() } 
if (app.selection.length == 0) { 
  if (!confirm("Are you sure you want to process the whole document?")) { 
    exit(); 
  } 
  theRange = app.activeDocument; 
} else { 
  theRange = app.selection[0]; 
  if (!theRange.constructor.name.isInArray(["Table", "Cell"]) && (theRange.insertionPoints.length == 1)) { 
    theRange = getParentTextFlow(theRange) 
  } 
} 
myStyles = ["Italic","Bold"]; 
myPosns = ["superscript","subscript"]; 
myPosnEnums = [Position.superscript,Position.subscript]; 
myDoc = app.activeDocument; 
try { 
  //This will do nothing if the style named "Sym"  does not exist already 
  myCharStyle = myDoc.characterStyles.item('Sym'); 
  mySymbols = ["κ","α","β","δ","Δ","ζ","γ","ε","λ", "θ"]; // "�", 
  app.findPreferences = null; 
  app.changePreferences = null; 
  for (var i=0; mySymbols.length > i; i++) { 
    theRange.search(mySymbols[i],false,false,undefined,{},{appliedCharacterStyle:myCharStyle}) 
  } 
} catch (e) { 
  //ignore error 
} 
noCharStyle = app.activeDocument.characterStyles[0]; 
for (var j = 0; myStyles.length > j; j++) { 
  try { 
    myCharStyle = myDoc.characterStyles.item(myStyles[j]); 
    myCharStyle.name; 
  } catch (e) { 
    myDoc.characterStyles.add({name:myStyles[j],fontStyle:myStyles[j]}) 
    myCharStyle = myDoc.characterStyles.item(myStyles[j]); 
  } 
  app.findPreferences = null; 
  app.changePreferences = null; 
  theRange.search("",false,false,undefined,{appliedCharacterStyle:noCharStyle,fontStyle:myStyles[j]},{appliedCharacterStyle:myStyles[j]}); 
} 
for (var j = 0; myPosns.length > j; j++) { 
  try { 
    myCharStyle = myDoc.characterStyles.item(myPosns[j]); 
    myCharStyle.name; 
  } catch (e) { 
    myDoc.characterStyles.add({name:myPosns[j],position:myPosnEnums [j]}) 
    myCharStyle = myDoc.characterStyles.item(myPosns[j]); 
  } 
  app.findPreferences = null; 
  app.changePreferences = null; 
  theRange.search("",false,false,undefined,{appliedCharacterStyle:noCharStyle,position:myPosnEnums[j]},{appliedCharacterStyle:myPosns[j]}); 
} 
// +++++++ Functions Start Here +++++++++++++++++++++++

function getParentTextFlow(theTextRef) { 
  // Returns reference to parent story or text of cell, as appropriate 
  if (theTextRef.parent.constructor.name == "Cell") { 
    return theTextRef.parent.texts[0]; 
  } else { 
    return theTextRef.parentStory; 
  } 
}

This is quite an old script. I've recently changed my approach for some of the things done in this script, but I've not gone back and updated this one to use my current style (if it ain't broke ... ).

To add more text formatting styles to the script, just extend the array "myStyles" on line 25 (or thereabouts).

Dave
Technical Details
Message-ID: <3bc1cee8.3@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeed.freenet.de!news.albasani.net!news.isoc.lu!webcrossing
References: <3bc1cee8.-1@webcrossing.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,9f7c3ecb4b002363
X-Google-Attributes: gidf939a,public
X-Google-Language: ENGLISH,ASCII
From: Dave_Saunders@adobeforums.com
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeed.freenet.de!news.albasani.net!news.isoc.lu!webcrossing
Newsgroups: adobe.scripting.indesign
Subject: Re: Script that can convert character attribs
 to character stylesheets
Message-ID: <3bc1cee8.3@webcrossing.la2eafNXanI>
Date: Thu, 28 Sep 2006 04:10:54 -0700
References: <3bc1cee8.-1@webcrossing.la2eafNXanI>
Lines: 102
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit