Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
8

GREP/Script changing space before/after with a return

Community Beginner ,
Oct 20, 2023 Oct 20, 2023

Hi,
I got a Word-file where the paragraphs are not separated with a blank return but with a "space before"
(I hope that thr right commend for it)

dummy.png

We use a script to formate wordfiles. But right now the script ignores the word space before and all the text is now one block. 
I need the GREP style or script command to find "space before", preferable not with a fix number but a range, because there are different "space before" in the text.
That would help me immensly otherwise I have to add them by hand afterwards

TOPICS
How to , Scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Oct 23, 2023 Oct 23, 2023
var myDoc = app.activeDocument;
var mySelection = app.selection[0];
var pStyles = myDoc.allParagraphStyles;
var paragraphs = mySelection.paragraphs;

searchSpaceBefore();

function searchSpaceBefore(){
    for (var i = 0; i < paragraphs.length; i++) {
        if (paragraphs[i].spaceBefore != 0) {
            paragraphs[i].appliedParagraphStyle = '+1';
        }
    }
}
Translate
Community Expert ,
Oct 20, 2023 Oct 20, 2023

Hi @Nicole2961823487py, a script can be great to set special styles based on your imported document but don't forget about the power of paragraph styles—they should be doing the space before and after.

 

If that hint doesn't help, then you'll need to post the script and a demo .indd so we can understand. 

- Mark

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 23, 2023 Oct 23, 2023

Hi Marc,

I get you and our script does exactly that. My problem is that in the past our customer used blank returns to indicate a new paragraph and because we don't wanted them in the later Indesign file we search via script to find them an replace them with a paragraph styles which does apply the space before. But know the search can't find the new paragraphs because there are no double returns anymore. So instead of searching for double breaks we though the script should search for the space before/after. But I don't know the GREP for that and the space before/after isn't always the same in one file.
We can't apply the space before to every paragraph because the customer also use the return as a line break. 

I made test files, because I'm not allowed to use client files. I hope it will help you in understanding the circumstanzes.

//This script was auto generated by chainGREP.jsx
//chainGREP.jsx is provided by Gregor Fellenz https://www.publishingx.de/
//Download at https://www.publishingx.de/download/chain-grep

main();

function main() 
{
    
	if (app.layoutWindows.length == 0) return;
	if (app.selection.length != 1 || !app.selection[0].hasOwnProperty('changeGrep')) 
	{
		alert('Please select only one textframe or text range!');
		return;
	}
	var changeObject = app.selection[0].parentStory;
	if (changeObject.hasOwnProperty('characters') && changeObject.characters.length == 0) return;
	var doc = app.documents[0];
	var style;
	var options = app.findChangeGrepOptions.properties;
	app.findGrepPreferences = NothingEnum.NOTHING;
	app.changeGrepPreferences = NothingEnum.NOTHING;

// Query [[01 - Bold in Zeichenbold]] -- If you delete this comment you break the update function
try {
	app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
	app.findGrepPreferences.properties = ({fontStyle:"Bold", capitalization:1852797549});
	app.changeGrepPreferences.properties = ({});
	style = getStyleByString(doc, 'Formatierung:bold', 'characterStyles');
	if (!style.isValid) throw Error(localize(({en:"Missing change characterstyle [%1] for query [%2]", de:"Fehlendes Ersetze-Zeichenformat [%1] bei Abfrage [%2]", fr:"La requête [%2] invoque en remplacement un style de caractère manquant : [%1]", ja_JP:"クエリ[%2]の置換形式に設定された文字スタイル[%1]が見つかりませんでした", nl:"Vervangende tekenstijl [%1] mist voor zoekopdracht [%2]"}), 'Formatierung:bold', '01 - Bold in Zeichenbold') );
	app.changeGrepPreferences.appliedCharacterStyle =  style;
	changeObject.changeGrep();
} catch (e) {alert(e + ' at line ' + e.line)}
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;	

// Query [[21 - Absatzabstand]] -- If you delete this comment you break the update function
try {
    app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
    app.findGrepPreferences.properties = ({findWhat:"(?<=\\r\\r).", capitalization:1852797549});
    app.changeGrepPreferences.properties = ({});
    style = getStyleByString(doc, 'Abstand davor', 'paragraphStyles');
    if (!style.isValid) throw Error(localize(({en:"Missing change pagraphstyle [%1] for query [%2]", de:"Fehlendes Ersetze-Absatzsformat [%1] bei Abfrage [%2]", fr:"La requête [%2] invoque en remplacement un style de paragraphe manquant : [%1]", ja_JP:"クエリ[%2]の置換形式に設定された段落スタイル[%1]が見つかりませんでした", nl:"Vervangende alineastijl [%1] mist voor zoekopdracht [%2]"}), '+ 1/2', '21 - Absatzabstand') );
    app.changeGrepPreferences.appliedParagraphStyle =  style;
    changeObject.changeGrep();
} catch (e) {alert(e + ' at line ' + e.line)}
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

// Query [[22 - Bold Versal +1]] -- If you delete this comment you break the update function
try {
    app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
    app.findGrepPreferences.properties = ({fontStyle:"Bold", capitalization:1634493296});
    app.changeGrepPreferences.properties = ({});
    style = getStyleByString(doc, 'Abstand davor', 'paragraphStyles');
    if (!style.isValid) throw Error(localize(({en:"Missing change pagraphstyle [%1] for query [%2]", de:"Fehlendes Ersetze-Absatzsformat [%1] bei Abfrage [%2]", fr:"La requête [%2] invoque en remplacement un style de paragraphe manquant : [%1]", ja_JP:"クエリ[%2]の置換形式に設定された段落スタイル[%1]が見つかりませんでした", nl:"Vervangende alineastijl [%1] mist voor zoekopdracht [%2]"}), '+ 1', '22 - Bold Versal +1') );
    app.changeGrepPreferences.appliedParagraphStyle =  style;
    changeObject.changeGrep();
} catch (e) {alert(e + ' at line ' + e.line)}
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

// Query [[19 - copy normal]] -- If you delete this comment you break the update function
try {
	app.findChangeGrepOptions.properties = ({includeFootnotes:true, kanaSensitive:true, widthSensitive:true});
	app.findGrepPreferences.properties = ({fontStyle:"Regular", capitalization:1852797549, position:1852797549});
	app.changeGrepPreferences.properties = ({});
	style = getStyleByString(doc, 'copy', 'paragraphStyles');
	if (!style.isValid) throw Error(localize(({en:"Missing change pagraphstyle [%1] for query [%2]", de:"Fehlendes Ersetze-Absatzsformat [%1] bei Abfrage [%2]", fr:"La requête [%2] invoque en remplacement un style de paragraphe manquant : [%1]", ja_JP:"クエリ[%2]の置換形式に設定された段落スタイル[%1]が見つかりませんでした", nl:"Vervangende alineastijl [%1] mist voor zoekopdracht [%2]"}), 'copy', '19 - copy normal') );
	app.changeGrepPreferences.appliedParagraphStyle =  style;
	changeObject.changeGrep();
} catch (e) {alert(e + ' at line ' + e.line)}
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

// Query [[25 - mehrere Umbrüche in einzelnen Umbruch]] -- If you delete this comment you break the update function
try {
	app.findChangeGrepOptions.properties = ({includeFootnotes:true, widthSensitive:true});
	app.findGrepPreferences.properties = ({findWhat:"^\\r"});
	app.changeGrepPreferences.properties = ({});
	changeObject.changeGrep();
} catch (e) {alert(e + ' at line ' + e.line)}
app.findGrepPreferences = NothingEnum.NOTHING;
app.changeGrepPreferences = NothingEnum.NOTHING;

};

function getStyleByString(docOrGroup, string, property) 
{
	if (string == '[No character style]') return docOrGroup[property][0];
	if (string == '[No paragraph style]') return docOrGroup[property][0];
	if (string == 'NormalParagraphStyle') return docOrGroup[property][1];
	stringResult = string.match (/^(.*?[^\\]):(.*)$/);
	var styleName = (stringResult) ? stringResult[1] : string;
	styleName = styleName.replace (/\\:/g, ':');
	remainingString = (stringResult) ? stringResult[2] : '';
	var newProperty = (stringResult) ? property.replace(/s$/, '') + 'Groups' : property;
	var styleOrGroup = docOrGroup[newProperty].itemByName(styleName);
	if (remainingString.length > 0 && styleOrGroup.isValid) styleOrGroup = getStyleByString (styleOrGroup, remainingString, property);
	return styleOrGroup;
};

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 20, 2023 Oct 20, 2023

With a selected text frame

var mySelection = app.selection[0];
var paragraphs = mySelection.paragraphs;

searchSpaceBefore();

function searchSpaceBefore(){
    for (var i = 0; i < paragraphs.length; i++) {
        if (paragraphs[i].spaceBefore != 0) {
            alert ('This paragraph has space before:\r' + paragraphs[i].contents);
        }
    }
}

something like this?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 22, 2023 Oct 22, 2023

Hi Nicosh,
I realized I explained not detailed enough. But your approchs helps. Instead of a warning I wand to apply a paragraphstyle with the name "+1". Would that be possible

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 23, 2023 Oct 23, 2023
var myDoc = app.activeDocument;
var mySelection = app.selection[0];
var pStyles = myDoc.allParagraphStyles;
var paragraphs = mySelection.paragraphs;

searchSpaceBefore();

function searchSpaceBefore(){
    for (var i = 0; i < paragraphs.length; i++) {
        if (paragraphs[i].spaceBefore != 0) {
            paragraphs[i].appliedParagraphStyle = '+1';
        }
    }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 23, 2023 Oct 23, 2023
LATEST

Hi Nicosh,
That's it. Thank you so very much. I'm not so good at JavaScript, so for my understanding searchSpaceBefore or searchSpaceAfter only search paragaphs where it is applied. So if no space after is applied nothing will happen?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2023 Oct 20, 2023

I have no idea of what your workflow involves, but I want to say that the use of blank paragraphs instead of space before/after is really bad typographic practice.

Every paragraph ends with a paragraph break character. You don't need a second on on the next line to indicate the start of a new paragraph, and using an empty paragraph to put space between can end up causing blank lines at the tops of text frames in threaded text.

GREP styles cannot change the text, only the formatting applied to that text, so if what you want to do can be automated it will require a script, and I don't write scripts so I cannot help with that. I would strongly urge you however, to rethink how you are setting up your paragraphs, adopt some paragraph styles that include space before or after where appropriate, and perhaps use aligne to baseline grid (that one is a tougher call and is highly text dependent), and use Find/Change to remove any empty paragraphs (I believe there's a sample query that does this installed with the program), and apply the appropriate styles with added spacing.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 22, 2023 Oct 22, 2023

Hi Peter,
we get the word files from our Customer and have no way to tell them how to format their Word file. In the past they used a blank return to indicate a new paragraph. So our script searches double blank returns and apply a Paragraphstyle which uses space before. But now it cannot be applied because there is only one return and of course there a returns which don't indicate a new paragraph. I need to separate between thoses two. So my idea or approach was to finde the space before and after changing it in a return and voila our script works again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 23, 2023 Oct 23, 2023
You can achieve consistency in your document (every paragraph with a blank after and no space before, or every paragraph with space before and no blank) with GREP search (Find/Search Panel, GREP tab):
 
1. To add a blank return after every paragraph.
Search: (.)$~b(.)
Replace: $1~b~b$2
Find any character at the end of the paragraph, "(.)$", followed by carriage return ,"~b", followed by any character "." (For example: .¶A)
Replace by the first character, "$1", followed by two carriage return, "~b~b" followed by second character, "$2" (.¶¶A)
 
2. Delete blank return after every paragraph.
Search: (?<=~b)~b(.)
Replace: $
Find a carriage return (~b) followed by any character (.) if both are after a carriage return (?<=~b): ¶¶A
Replace by the character.
You can apply any paragraph style to replace expression in change format options of the pannel.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines