Skip to main content
dublove
Legend
October 20, 2026
Question

How to determine the continuous execution of code? I only want a piece of code to run once.

I have chosen some text.
First run: I want to restore the line spacing of the selected text to the "original paragraph style" line spacing of the first paragraph.
Second run: Take the line spacing of the first paragraph of text, add a dot, and then apply it to the selected text.
After the third time, repeat the second operation.


It's a bit contradictory, these are two separate actions. We need to ensure that the line spacing is restored to its original state while also increasing it on top of it.

var d = app.activeDocument;
var item = app.selection[0];
//-----------------------A-------------------
//Apply the first leading to the selected text
var OriginalStyleLeading = item.paragraph[0].appliedParagraphStyle.leading;
app.selection[0].text[0].leading = OriginalStyleLeading;

//-----------------------B-------------------
//Adjust line spacing  When operating continuously, only repeat this step
var firstParLeading = item.paragraph[0].appliedParagraphStyle.leading;
app.selection[0].text[0].leading = firstParLeading + 1;

After the second time, only part B can be repeated.

Is it possible use a clock to determine whether to execute for the first time? For example, within 3 seconds, it is considered as the second time.

    4 replies

    Community Expert
    October 20, 2026

    The first operation would not lead to any change after the 1st run so even if it is run it has no effect. If you still want to optimise it you can check if the leading is already not equal to what you are trying to set. Did I miss something?

    Translate
    -Manan
    dublove
    dubloveAuthor
    Legend
    October 20, 2026

    Hi ​@Manan Joshi 

    To restore the leading of the original style, it is because the current leading may have been manually adjusted.

    I need to operate on the original leading.

    Translate
    Community Expert
    October 20, 2026

    That is what I am saying on the first run it will restore it on the subsequent runs if will not do anything because the leading is already set to the value you desire.

    Translate
    -Manan