求InDesign CS6 JS脚本:搜索指定段落样式内特定词汇并输出页码
InDesign CS6 JavaScript Script: Locate Keywords in Specific Paragraph Style & Output Formatted Page Numbers
Here's a tailored script for InDesign CS6 that does exactly what you need—it scans your document for target keywords within a specified paragraph style, then outputs formatted page numbers (like p6, p11) both to the console and a text file on your desktop.
// -------------------------- // CONFIGURE THESE SETTINGS FIRST! // -------------------------- var targetStyleName = "Your Paragraph Style Name"; // Replace with your actual style name var targetKeywords = ["Keyword1", "Keyword2"]; // Add/remove keywords as needed var outputFileName = "Keyword_Page_Results.txt"; // Name of the output text file // -------------------------- try { var doc = app.activeDocument; var targetStyle = doc.paragraphStyles.itemByName(targetStyleName); // Check if the target paragraph style exists if (targetStyle.isValid === false) { throw new Error("Paragraph style '" + targetStyleName + "' not found in the document."); } var matchedPages = []; var allParagraphs = doc.stories.everyItem().paragraphs.everyItem().getElements(); // Loop through every paragraph in the document for (var i = 0; i < allParagraphs.length; i++) { var para = allParagraphs[i]; // Skip paragraphs not using the target style if (para.appliedParagraphStyle !== targetStyle) continue; var paraText = para.contents; var hasMatch = false; // Check if the paragraph contains any of the target keywords for (var k = 0; k < targetKeywords.length; k++) { if (paraText.indexOf(targetKeywords[k]) !== -1) { hasMatch = true; break; } } if (hasMatch) { // Get the page number (convert 0-based documentOffset to 1-based) var pageNum = para.insertionPoints[0].pages[0].documentOffset + 1; var formattedPage = "p" + pageNum; // Add to matchedPages only if not already present (avoid duplicates) if (matchedPages.indexOf(formattedPage) === -1) { matchedPages.push(formattedPage); } } } // Output results to console if (matchedPages.length > 0) { $.writeln("Matched pages for keywords '" + targetKeywords.join(", ") + "' in style '" + targetStyleName + "':"); $.writeln(matchedPages.join(", ")); } else { $.writeln("No matches found for keywords '" + targetKeywords.join(", ") + "' in style '" + targetStyleName + "'."); } // Save results to text file on desktop var desktopFolder = Folder.desktop; var outputFile = File(desktopFolder.fsName + "/" + outputFileName); outputFile.open("w"); if (matchedPages.length > 0) { outputFile.write("Matched pages for keywords '" + targetKeywords.join(", ") + "' in paragraph style '" + targetStyleName + ":\n"); outputFile.write(matchedPages.join("\n")); } else { outputFile.write("No matches found for keywords '" + targetKeywords.join(", ") + "' in paragraph style '" + targetStyleName + "."); } outputFile.close(); alert("Script completed! Results saved to:\n" + outputFile.fsName); } catch (e) { alert("Error: " + e.message); $.writeln("Error: " + e.message); }
How to Use This Script:
- Open your target InDesign document in CS6.
- Go to
Window > Utilities > Script Editorto open the script editor panel. - Paste the entire script above into the editor.
- Modify the CONFIGURATION SETTINGS section at the top:
- Replace
"Your Paragraph Style Name"with the exact name of your target paragraph style. - Update
targetKeywordswith the words/phrases you want to search for (add more entries separated by commas). - Adjust
outputFileNameif you want a different name for the text file.
- Replace
- Click the "Run" button (play icon) in the script editor to execute the script.
Key Details:
- Duplicate Page Prevention: The script automatically removes duplicate page entries, so each page number only appears once even if the keyword is found multiple times on the same page.
- Page Number Accuracy: Uses the first insertion point of the matched paragraph to get the correct page (handles paragraphs that span multiple pages by targeting the page where the paragraph starts).
- Error Handling: Includes checks for missing paragraph styles and alerts you if something goes wrong.
- Dual Output: Results are printed to the script editor console and saved to a text file on your desktop for easy reference.
内容的提问来源于stack exchange,提问作者VHS
相关产品推荐
相关产品推荐

