Skip to content
The Chat Archive
claude-sonnet-4-20250514· Anthropic LMArena Human Preference 140K

اسکریپت ایجاد نمايه از طريق کاندیشنال تکست در ایندیزاین

  1. User

    اسکریپت ایجاد نمايه از طريق کاندیشنال تکست در ایندیزاین

  2. Assistant

    برای ایجاد نمایه (Index) از طریق Conditional Text در InDesign، می‌توانید از این اسکریپت استفاده کنید:

    // اسکریپت ایجاد نمایه از Conditional Text در InDesign
    
    app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "ایجاد نمایه از Conditional Text");
    
    function main() {
        // بررسی وجود سند باز
        if (app.documents.length == 0) {
            alert("لطفاً ابتدا سندی را باز کنید.");
            return;
        }
        
        var doc = app.activeDocument;
        var indexEntries = [];
        
        // جستجوی تمام متن‌های شرطی
        var allStories = doc.stories;
        
        for (var i = 0; i < allStories.length; i++) {
            var story = allStories[i];
            
            // جستجو در تمام کاراکترها
            for (var j = 0; j < story.characters.length; j++) {
                var char = story.characters[j];
                
                // بررسی وجود Conditional Text
                if (char.appliedConditions.length > 0) {
                    var conditionName = char.appliedConditions[0].name;
                    var charContent = char.contents;
                    
                    // اضافه کردن به لیست نمایه
                    if (charContent.length > 0 && charContent != "\r") {
                        indexEntries.push({
                            text: charContent,
                            condition: conditionName,
                            page: getPageNumber(char)
                        });
                    }
                }
            }
        }
        
        // ایجاد نمایه
        if (indexEntries.length > 0) {
            createIndex(doc, indexEntries);
            alert("نمایه با موفقیت ایجاد شد. تعداد ورودی‌ها: " + indexEntries.length);
        } else {
            alert("هیچ Conditional Text یافت نشد.");
        }
    }
    
    function getPageNumber(textObject) {
        try {
            var parentTextFrame = textObject.parentTextFrames[0];
            if (parentTextFrame) {
                return parentTextFrame.parentPage.name;
            }
        } catch (e) {
            return "نامشخص";
        }
        return "نامشخص";
    }
    
    function createIndex(doc, entries) {
        // مرتب‌سازی ورودی‌ها بر اساس حروف الفبا
        entries.sort(function(a, b) {
            return a.text.localeCompare(b.text, 'fa');
        });
        
        // ایجاد صفحه جدید برای نمایه
        var indexPage = doc.pages.add();
        
        // ایجاد Text Frame برای نمایه
        var textFrame = indexPage.textFrames.add();
        textFrame.geometricBounds = [50, 50, 700, 500]; // تنظیم ابعاد
        
        // ایجاد محتوای نمایه
        var indexContent = "نمایه\n\n";
        
        for (var i = 0; i < entries.length; i++) {
            var entry = entries[i];
            indexContent += entry.text + " .................. " + entry.page + "\n";
        }
        
        textFrame.contents = indexContent;
        
        // تنظیم فرمت متن
        var paraStyle = doc.paragraphStyles.itemByName("نمایه");
        if (!paraStyle.isValid) {
            paraStyle = doc.paragraphStyles.add({name: "نمایه"});
            paraStyle.appliedFont = "Arial";
            paraStyle.pointSize = 12;
            paraStyle.rightIndent = 20;
        }
        
        textFrame.parentStory.paragraphs.everyItem().appliedParagraphStyle = paraStyle;
    }
    

    همچنین اسکریپت پیشرفته‌تری برای کنترل بیشتر:

    // اسکریپت پیشرفته ایجاد نمایه از Conditional Text
    
    function advancedIndexFromConditionalText() {
        if (app.documents.length == 0) {
            alert("لطفاً سندی را باز کنید.");
            return;
        }
        
        var doc = app.activeDocument;
        
        // دیالوگ تنظیمات
        var dialog = app.dialogs.add({name: "تنظیمات نمایه"});
        
        with (dialog.dialogColumns.add()) {
            with (dialogRows.add()) {
                staticTexts.add({staticLabel: "شرط مورد نظر:"});
                var conditionDropdown = dropdowns.add({
                    stringList: getConditionNames(doc),
                    selectedIndex: 0
                });
            }
            
            with (dialogRows.add()) {
                staticTexts.add({staticLabel: "نوع مرتب‌سازی:"});
                var sortDropdown = dropdowns.add({
                    stringList: ["حروف الفبا", "ترتیب ظهور"],
                    selectedIndex: 0
                });
            }
            
            with (dialogRows.add()) {
                var includePageNumbers = checkboxControls.add({
                    staticLabel: "شامل شماره صفحه",
                    checkedState: true
                });
            }
        }
        
        if (dialog.show() == true) {
            var selectedCondition = conditionDropdown.stringList[conditionDropdown.selectedIndex];
            var sortType = sortDropdown.selectedIndex;
            var showPageNumbers = includePageNumbers.checkedState;
            
            dialog.destroy();
            
            // ایجاد نمایه با تنظیمات انتخابی
            createAdvancedIndex(doc, selectedCondition, sortType, showPageNumbers);
        } else {
            dialog.destroy();
        }
    }
    
    function getConditionNames(doc) {
        var conditions = [];
        conditions.push("همه شرایط");
        
        for (var i = 0; i < doc.conditions.length; i++) {
            conditions.push(doc.conditions[i].name);
        }
        
        return conditions;
    }
    
    function createAdvancedIndex(doc, condition, sortType, showPageNumbers) {
        var entries = collectConditionalTextEntries(doc, condition);
        
        if (entries.length == 0) {
            alert("هیچ ورودی یافت نشد.");
            return;
        }
        
        // مرتب‌سازی
        if (sortType == 0) { // حروف الفبا
            entries.sort(function(a, b) {
                return a.text.localeCompare(b.text, 'fa');
            });
        }
        // برای ترتیب ظهور، مرتب‌سازی نمی‌کنیم
        
        // ایجاد صفحه نمایه
        var indexPage = doc.pages.add();
        var textFrame = indexPage.textFrames.add();
        textFrame.geometricBounds = [50, 50, 700, 500];
        
        var content = "نمایه\n" + "=".repeat(20) + "\n\n";
        
        for (var i = 0; i < entries.length; i++) {
            var entry = entries[i];
            content += entry.text;
            
            if (showPageNumbers) {
                content += " .................. صفحه " + entry.page;
            }
            
            content += "\n";
        }
        
        textFrame.contents = content;
        
        alert("نمایه ایجاد شد. تعداد ورودی‌ها: " + entries.length);
    }
    
    function collectConditionalTextEntries(doc, targetCondition) {
        var entries = [];
        var processedTexts = {}; // برای جلوگیری از تکرار
        
        for (var i = 0; i < doc.stories.length; i++) {
            var story = doc.stories[i];
            
            for (var j = 0; j < story.characters.length; j++) {
                var char = story.characters[j];
                
                if (char.appliedConditions.length > 0) {
                    var conditionName = char.appliedConditions[0].name;
                    
                    // بررسی شرط
                    if (targetCondition == "همه شرایط" || conditionName == targetCondition) {
                        var text = char.contents.replace(/[\r\n]/g, "").trim();
                        
                        if (text.length > 0 && !processedTexts[text]) {
                            entries.push({
                                text: text,
                                condition: conditionName,
                                page: getPageNumber(char)
                            });
                            
                            processedTexts[text] = true;
                        }
                    }
                }
            }
        }
        
        return entries;
    }
    
    // اجرای اسکریپت
    advancedIndexFromConditionalText();
    

    نحوه استفاده:

    1. متن‌های مورد نظر را با Conditional Text مشخص کنید
    2. اسکریپت را در InDesign اجرا کنید
    3. تنظیمات دلخواه را انتخاب کنید
    4. نمایه به صورت خودکار ایجاد می‌شود

    این اسکریپت‌ها امکانات مختلفی مانند مرتب‌سازی، فیلتر کردن بر اساس شرط خاص، و تنظیم فرمت نمایه را فراهم می‌کنند.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.