2 Replies Latest reply on Dec 24, 2016 5:48 AM by Oriup

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

    MatthewMariani Level 1

      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[k].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[i] );
           if ( zTag.isValid ) continue;
           else d.xmlTags.add( markup_tag_names[i] );
      }
      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[i];
           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[k].pageItems.count() > 0 ) {
              for ( var z = 0; z < d.stories[k].pageItems.count(); z++ ) {
                  d.stories[k].pageItems[z].locked = arraysAnchoredInlineGraphicsLockStatuses[k][z];
              }
          }
      }
      for ( var i = 0; i < d.pageItems.count(); i++ ) {
          d.pageItems[i].locked = arrPageItemsLockStatuses[i];
       }
      for ( var i = 0; i < d.layers.count(); i++ ) {
          d.layers[i].locked = arrLayersLockStatuses[i];
       }
      d.xmlExportMaps.everyItem().remove();
      for ( var i = 0; i < d.allParagraphStyles.length; i++ ) {
           var psty = d.allParagraphStyles[i];
           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();