Skip to main content
Inspiring
April 25, 2011
Question

[JS][CS5] Enhancing Section 508 accessibility of pdfs from InDesign script

  • April 25, 2011
  • 2 replies
  • 1131 views

I was using a script to automate mapping styles to tags and tagging graphics as artifacts or as figures with Alt attributes for section 508 accessibility reasons, but in order to encourage use by designers, I wanted to add code to unlock all locked items in the document and then relock those items before the end of the script. Otherwise, the script would hang up when it encountered a locked item. I have an approach that seems to work, but is there a more elegant way of handling the locking and unlocking, especially for anchored and inline graphics?

Also, if anyone finds this code useful, I'd appreciate any feedback based on your use of it.

/* Preps an InDesign document for pdf output that is closer to section 508 compliancy. This script acts on the active InDesign document. It removes any unused tags from the tags palette.  It unlocks everything that is locked. It creates all necessary tags in the tags palette if they do not already exist. It tags all untagged, placed graphics as either Artifacts or as Figures as follows: Any graphic on a master page is tagged as an artifact. Any graphic with a file name that contains either of two 4-character sequences, “art_” or “_art” (case insensitive) is also tagged as an artifact. All other untagged graphics are tagged as figures and assigned an Alt attribute. Graphics appearing on publication pages (not master pages) that were tagged prior to running the script will have an Alt attribute added to the pre-existing tag if it does not already have one. The script relocks everything that the script previously unlocked. It removes any existing styles-to-tags mappings. It creates new styles-to-tags mappings for all paragraph styles based on the following paragraph style naming convention: Every paragraph style is mapped to the P tag, unless the style name contains a prefix, suffix, or infix matching the format H2_ or _H1 (underscore character optional)  where the numeric digit indicates the level in the document hierarchy for the headings to which the style is applied. Paragraph styles with names containing H1, H2, H3, H4, H5, or H6 are mapped to tags with those names. The script then maps paragraph styles to tags based on the newly created mappings. THIS SCRIPT IS MADE AVAILABLE ON AN "AS IS" BASIS,  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED. */ #target "InDesign-7.0" //for InDesign CS5 var markup_tag_names = new Array( "H1","H2","H3","H4","H5","H6","Figure","P","Story","Article","Table","Cell","Artifact"); var re_artifact = new RegExp("(ART_)|(_ART)", "i" ); var re_heading = new RegExp("(H[1-6]_?)|(_?H[1-6])", "i" ); var arraysAnchoredInlineGraphicsLockStatuses = new Array(); var d = app.documents[0]; d.deleteUnusedTags(); var arrLayersLockStatuses = d.layers.everyItem().locked; var arrPageItemsLockStatuses = d.pageItems.everyItem().locked; for (var k  = 0; k < d.stories.count(); k++ ) {     arraysAnchoredInlineGraphicsLockStatuses.push( d.stories.pageItems.everyItem().locked ); } d.layers.everyItem().locked = false; d.pageItems.everyItem().locked = false; d.stories.everyItem().pageItems.everyItem().locked = false; for ( var i = 0; i < markup_tag_names.length; i++ ) {      zTag = d.xmlTags.itemByName( markup_tag_names );      if ( zTag.isValid ) continue;      else d.xmlTags.add( markup_tag_names ); } var p_tag = d.xmlTags.itemByName('P'); var figure_tag = d.xmlTags.itemByName( 'Figure' ); var artifact_tag = d.xmlTags.itemByName( 'Artifact' ); var root = d.xmlElements[0]; for ( var i = 0; i < d.allGraphics.length; i++ ) {      g = d.allGraphics;      pg = g.parentPage;      if ( pg == null ) continue;      isOnMaster = pg.parent.constructor.name == 'MasterSpread';      if ( g.itemLink.isValid != true ) continue;     fname = g.itemLink.filePath;     fname = fname.substring( fname.lastIndexOf(':') + 1 ); //Mac-specific folder separator ':'      if ( g.associatedXMLElement == null ) {         if ( isOnMaster )  {             root.xmlElements.add( artifact_tag, g );         }         else  if (  re_artifact.exec( fname ) != null ) {             root.xmlElements.add( artifact_tag, g );         }         else {             xmle = root.xmlElements.add( figure_tag, g );             xmle.xmlAttributes.add('Alt', '' );         }     }      else if ( ! ( g.associatedXMLElement.xmlAttributes.itemByName('Alt').isValid ) && !(isOnMaster ) ) g.associatedXMLElement.xmlAttributes.add('Alt', '' ); } for (var k  = 0; k < d.stories.count(); k++ ) {     if ( d.stories.pageItems.count() > 0 ) {         for ( var z = 0; z < d.stories.pageItems.count(); z++ ) {             d.stories.pageItems.locked = arraysAnchoredInlineGraphicsLockStatuses;         }     } } for ( var i = 0; i < d.pageItems.count(); i++ ) {     d.pageItems.locked = arrPageItemsLockStatuses; } for ( var i = 0; i < d.layers.count(); i++ ) {     d.layers.locked = arrLayersLockStatuses; } d.xmlExportMaps.everyItem().remove(); for ( var i = 0; i < d.allParagraphStyles.length; i++ ) {      var psty = d.allParagraphStyles;      var rslt = re_heading.exec( psty.name);      if ( rslt != null ) {           rslt = rslt[0].replace("_", "");           rslt = rslt.toUpperCase();           d.xmlExportMaps.add( psty, d.xmlTags.itemByName(rslt) );      }      else { d.xmlExportMaps.add( psty, p_tag ); } } d.mapStylesToXMLTags();

This topic has been closed for replies.

2 replies

SumitKumar
Inspiring
December 24, 2016

Dear Matthew,

It is really very helpful.

Could you edit for me if I provide my structure details?

I need it like below screen capture:

I explain, Need every page item in a separate Article then every story just put in p tag and every Graphics put in image tag.

My english is poor. I hope, you understand my problem.

Sumit

-Sumit
Inspiring
August 15, 2016

Hi

This looks really useful, do you know how I could edit this so that the image in my selection would turn into an artefact?

Thanks