Here's another approach, depends exactly on what you are looking to do. /** * [matchesCharacterProperties] matches textStyleRanges with a sample style * @param {DOM TextStyleRange} sampleTextStyleRange Sample character, textStyleRange * @param {DOM Collection of TextStyleRange} textStyleRanges Collection of ranges that uses .everyItem() or itemByRange() * @param {Array} properties [Optional defaults to set list] properties you want to match the sample * @return {Array} Array of matching textStyleRanges * By Trevor http://creative-scripts.com 25 Dec 18 * Not Optimized Uses toSource which will probably be dumped sometime */ function matchesCharacterProperties(sampleTextStyleRange, textStyleRanges, properties) { if (!properties) { properties = [ 'appliedFont', 'appliedLanguage', 'appliedCharacterStyle', 'fillColor', 'strokeColor', 'baseline', 'underline', 'capitalization' // etc. etc. etc..... ]; } if (typeof properties !== 'object') { properties = [properties]; } var n, propertiesCount, sampleProperties, textStyleRangeProperties, textStyleCount, match, c, matches, textStyleRangeArray, propertiesCache; var resolveProperties = function(properties) { if (typeof properties !== 'object') { properties = [properties]; } return properties.toSource().replace(/^\[/, '').replace(/]$/, '').split(', '); }; propertiesCount = properties.length; sampleProperties = []; propertiesCache = []; matches = []; // cache the properties for (n = 0; n < propertiesCount; n++) { // resolveProperties returns an array and we only want the values hence the [0] sampleProperties.push(resolveProperties(sampleTextStyleRange[properties ])[0]); // For the propertiesCache we want the Array of results for the whole collection propertiesCache.push(resolveProperties(textStyleRanges[properties ])); } textStyleRangeArray = textStyleRanges.getElements(); textStyleCount = textStyleRangeArray.length; for (c = 0; c < textStyleCount; c++) { match = true; for (n = 0; n < propertiesCount; n++) { textStyleRangeProperties = propertiesCache ; if (textStyleRangeProperties !== sampleProperties ) { match = false; break; } } if (match) { matches.push(textStyleRangeArray ); // matches.push(c); // if you just want the indexes use this line instead of the above one } } sampleTextStyleRange = textStyleRanges = properties = n = propertiesCount = sampleProperties = propertiesCache = textStyleRangeProperties = textStyleCount = match = c = textStyleRangeArray = resolveProperties = null; return matches; } alert( matchesCharacterProperties( app.selection[0] || app.activeDocument.stories[0].characters[0], // The sample range you want to match app.activeDocument.stories[0].textStyleRanges.everyItem(), // the COLLECTION you want to check ['appliedFont', 'pointSize', 'fillColor', 'underline'] // [Optional] the properties you want to match ).toSource() );
... View more