Skip to main content
zeRafio
Inspiring
May 1, 2024
Answered

How to Remove one condition from paragraph?

  • May 1, 2024
  • 4 replies
  • 1074 views

Given a paragraph with conditions {a, b, c, d} applied to some words and the condition {e} applied to the entire paragraph: is there a command to remove the condition {e} without removing those applied to words?

 

<Title renamed by moderator>

This topic has been closed for replies.
Correct answer zeRafio

@Eugene Tyson , @Nick Passmore , @Laubender , @tobias.wantzen 

Thank you guys!

Even if none of these script is perfect, they put me in the right direction:

 

tell application id "InDn"
	
	set theConditions to name of conditions of active document
	set condToClear to condition "relectureWord" of active document
	
	repeat with aCondition in theConditions
		if (every text style range of selection whose name of applied conditions contains aCondition) ≠ {} then
			set theFound to object reference of (every text style range of selection whose name of applied conditions contains aCondition)
			repeat with aFound in theFound
				set condApplied to applied conditions of aFound
				set condToKeep to {}
				repeat with aCond in condApplied
					if contents of aCond ≠ condToClear then set end of condToKeep to contents of aCond
				end repeat
				apply conditions aFound using condToKeep with remove existing
			end repeat
		end if
	end repeat
end tell

 

This snippet is part of a script that loops through selected paragraphs. Be careful if you use it "as is": the text style ranges will not necessarily stop at the end of the selection...

4 replies

Community Expert
May 2, 2024

Hi @zeRafio ,

also look into this thread from 2011 where this is discussed at length:

 

Returning a conditional text value.
Trevor:, Aug 02, 2011
https://community.adobe.com/t5/indesign-discussions/returning-a-conditional-text-value/td-p/3637111

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Participating Frequently
May 3, 2024

Based on Uwe's suggestion to look at text style ranges, here's a much quicker AppleScript version.

 

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- Select all the text to be processed
tell application id "InDn"
	tell document 1
		set c to (condition "relectureWord")
		set x to (a reference to (item 1 of the selection))
		set TSRs to (every text style range of x)
		repeat with i from 1 to (count of items of TSRs)
			set NextRange to (a reference to (text style range i of x))
			set AC to (get applied conditions of NextRange)
			set NC to {}
			repeat with f from 1 to count of items of AC
				if item f of AC is not c then
					set NC to NC & (item f of AC)
				end if
			end repeat
			set applied conditions of NextRange to NC
		end repeat
	end tell
end tell

  

zeRafio
zeRafioAuthorCorrect answer
Inspiring
May 3, 2024

@Eugene Tyson , @Nick Passmore , @Laubender , @tobias.wantzen 

Thank you guys!

Even if none of these script is perfect, they put me in the right direction:

 

tell application id "InDn"
	
	set theConditions to name of conditions of active document
	set condToClear to condition "relectureWord" of active document
	
	repeat with aCondition in theConditions
		if (every text style range of selection whose name of applied conditions contains aCondition) ≠ {} then
			set theFound to object reference of (every text style range of selection whose name of applied conditions contains aCondition)
			repeat with aFound in theFound
				set condApplied to applied conditions of aFound
				set condToKeep to {}
				repeat with aCond in condApplied
					if contents of aCond ≠ condToClear then set end of condToKeep to contents of aCond
				end repeat
				apply conditions aFound using condToKeep with remove existing
			end repeat
		end if
	end repeat
end tell

 

This snippet is part of a script that loops through selected paragraphs. Be careful if you use it "as is": the text style ranges will not necessarily stop at the end of the selection...

Community Expert
May 2, 2024

Hi @zeRafio ,

well, if you remove a condition you will remove it from the document.

Not only from the text where you applied it:

app.documents[0].conditions.itemByName( "relectureWord" ).remove();

 

FWIW: If you remove condition "relectureWord" from the document the condition is also removed from the text in that document. But I assume you need to do that on a case-by-case basis, so you need more code.

 

Two ideas:

[1] You could move text out to a new document, remove the condition there and move the text back in to the original document.

 

Note, in the code below that is working on selected text, I used parent instead of parentStory for variable sourceStory, because I'd like to make the code also work on selected text in a table cell. For text in a table cell, parent returns all the text of the cell; parentStory returns the story where the table is.

 

// Name of the condition we want to remove from selected text
// and NOT from the document!

var nameOfCondition = "relectureWord";

// Text selected:
var formattedText = app.selection[0];
var sourceStory = formattedText.parent;

var ind = app.selection[0].insertionPoints[0].index;

var tempDoc = app.documents.add();
var temptextFrame = tempDoc.textFrames.add();

formattedText.move( LocationOptions.AT_BEGINNING , temptextFrame.parentStory );

tempDoc.conditions.itemByName( nameOfCondition ).remove();
temptextFrame.parentStory.texts[0].move( LocationOptions.AT_BEGINNING , sourceStory.insertionPoints[ind] );

tempDoc.close( SaveOptions.NO );

 

[2] Or you could work with text style ranges instead of characters and apply an appropriate array of conditions to a given text style range. All applied conditions minus the one you'd like to remove from the text.

Note, that a text style range could easily extend a selection of characters.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Barb Binder
Community Expert
Community Expert
May 1, 2024

Hi @zeRafio:

 

Select the entire paragraph and uncheck relectureWord in the Conditional Text panel. 

 

~Barb

 

~Barb at Rocky Mountain Training
zeRafio
zeRafioAuthor
Inspiring
May 2, 2024

My appologies. I forgot to mention that I was asking for a scripting solution in applescript or extendscript. The post is tagged but it's not obvious, I can see.

In both languages, there's a command to 'apply' a condition, another to 'delete' a condition at the document level, but there's not to 'remove' an applied condition among others.

So I'm inquirering for a method to do by script what you're suggesting using the interface.

Community Expert
May 2, 2024

It's not clear what you're asking - you mean something like this?
I have no idea if this works or not - probably not - but you can let us know

Without a sample file with context it's impossible to troubleshoot the script. 

// Iterate through each paragraph in the document
for (var i = 0; i < app.activeDocument.stories.length; i++) {
    var story = app.activeDocument.stories[i];
    var paragraphs = story.paragraphs;
    
    for (var j = 0; j < paragraphs.length; j++) {
        var paragraph = paragraphs[j];
        
        // Check if the paragraph has the condition applied
        if (paragraph.appliedConditions.itemByName("relectureWord").isValid) {
            // Remove the condition
            paragraph.appliedConditions.itemByName("relectureWord").remove();
        }
    }
}

 

Community Expert
May 1, 2024

I don't follow your train of thought here - are you talking about conditional text?
I don't understand

Can you give a broader context with an example?

zeRafio
zeRafioAuthor
Inspiring
May 1, 2024

In the example above you can see that some words have (each one) a different condition applied on it. [conditions = contitional text]

Plus, the entire paragraph has an other condition applied (the sinusoidal underline).

I want to remove the sinusoidal condition from the paragraph (the condition named "relectureWord") while keeping the 5 other applied to words.

(Not sure I'm much more understanable!)