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

[JS IDCS5] Space before paragraph if previous paragraph style is....

Participant ,
Mar 24, 2011 Mar 24, 2011

Hi,

I've got a heading paragraph style that has got some space before defined. But when a previous paragraph has got some space after defined, I've got two times spaces between them. Is it possible to script when two certain paragraph styles follow up, that one of these styles is changing to a style without spacing?

Regards, Sjoerd

TOPICS
Scripting
3.0K
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

Community Expert , Mar 24, 2011 Mar 24, 2011

This ought to get you started -- but Sjoerd, there is no way you can 'attach' this kind of script to a paragraph style. That's why you have to run the script *every time* you changed a style somewhere ...

app.findTextPreferences = null;
app.findTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("Heading 1");
sourcelist = app.activeDocument.findText();
targetlist = [];
for (l=0; l<sourcelist.length; l++)
{
next = sourcelist.paragraphs.nextItem(sourcelist.paragraphs[0]);
if
...
Translate
Contributor ,
Mar 24, 2011 Mar 24, 2011

You could certainly script it, but then you'd have to run the script every now and then when you've added or removed text. Personally I would have much preferred if the space before and after could be set to behave like margins in HTML, with adjacent margins being collapsed to the equivalent of the larger margin only...

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
Participant ,
Mar 24, 2011 Mar 24, 2011

Ow, that sounds like a good option, but how do you define that in a paragraph style?

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 ,
Mar 24, 2011 Mar 24, 2011

stoereee wrote:

Ow, that sounds like a good option, but how do you define that in a paragraph style?

Ahh, sorry, you can't. I just wish the option was available as that is how I'd want it to behave. I could whip up a script which does it that but it'd still have to be run periodically. Unless perhaps one wrote it as a script plug-in... hmm... sounds like a fun project to tackle!

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
Explorer ,
Mar 24, 2011 Mar 24, 2011

Sjoerd - that's funny, I was just about to ask the same query! I have separate styles for an A heading and a B heading with spacing above/below them, but I was looking for a way to apply different styles to those paragraphs (same styling but with no spacing or less spacing) when a B heading immediately follows an A heading. I can find and replace the styles of individual paragraphs using GREP in a javascript but I can't work out how to identify an A followed by a B and then change each paragraph's style to something different.

Hope someone can help!

Iain

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
Participant ,
Mar 24, 2011 Mar 24, 2011

I think a have the script:

var myDocument = app.activeDocument;

var myParagraphStyle = "AAA";

app.findGrepPreferences = null;

app.changeGrepPreferences = null;

app.findGrepPreferences.appliedParagraphStyle = myParagraphStyle;

var myFoundParagraphs = myDocument.findGrep();

for (i = 0; i < myFoundParagraphs.length; i++){

    try {

var myNextPar = myFoundParagraphs.nextItem(myFoundParagraphs);

        if (myFoundParagraphs.appliedParagraphStyle.name == "AAA" && myNextPar.appliedParagraphStyle.name == "BBB")
{    myFoundParagraphs.spaceBefore = 3;

        }

    }

    catch (myError){}            

}

But I would like to know what's Mayhem's option looks like??

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
Explorer ,
Mar 24, 2011 Mar 24, 2011

Stoereee - I think I can follow what's going on in your script but I can't get it to work. It errors here

var myNextPar = myFoundParagraphs.nextItem(myFoundParagraphs);

complaining that myFoundParagraphs.nextItem is not a function. I'm using CS3. If I do an alert(myFoundParagraphs.contents) the result is undefined. Any suggestions? I like your script because I can follow it! If we can sort that I am going to change the "fix" to

myFoundParagraphs.appliedParagraphStyle="A head followed by B";

to apply an amended style to my A heads and I'll change it around to do the same for the B head.

thanks,

Iain

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
Participant ,
Mar 24, 2011 Mar 24, 2011

Iain:

app.findGrepPreferences = null;

app.findGrepPreferences.appliedParagraphStyle = "BB";

f = app.activeDocument.findGrep();

for (i = 0; i < f.length; i++) {

  if (f.parentStory.insertionPoints[f.index-1].appliedParagraphStyle.name == "AA")

      f.spaceBefore = 3;
     
}

This should work!

Succes

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
Explorer ,
Mar 24, 2011 Mar 24, 2011

Thanks Stoeree, I'll give that a try.


And thanks to everyone who made suggestions - I'm learning, slowly!

Iain

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 ,
Mar 24, 2011 Mar 24, 2011

This forum is littered with the remains of those that went before you and tried to take the fast route.

It took me years (literally, honest!) to take the leap from pasting together working bits from the example scripts to a working mental model of this oddball language called "Javascript". Marc Autret's scripts, for example, still go zooom, way over my head.

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
Explorer ,
Mar 24, 2011 Mar 24, 2011
LATEST

I know. It is useful though, seeing if you can understand someone else's work enough to rework it into your own script. Glad it took you years - must be almost 2 years since I first started looking at JS. Every tinme I come back to it it's like starting again almost. Oh well, onwards and upwards. Thanks for all your help. I'll no doubt be back tomorrow.

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 ,
Mar 24, 2011 Mar 24, 2011

What you really need is a feature called "conditional styles", which, unfortunately, doesn't exist. It works as follows: in the paragraph style options you can now just set an absolute value for Space Before. If you have conditional styles, you could say "apply 0 units before if preceded by paragraph style x, otherwise apply n units". This feature has been prominent on the all-time feature-request list for many years, but Adobe has so far resisted the temptation to implement it. Please add your voice!

So in the absence of conditional styles you need to script it, which is not too hard: if an instance of par. X's previousItem is a paragraph whose name is Y, then remove any space. Loosely:

app.findGrepPreferences.appliedParagraphStyle = "sectionB";

f = app.activeDocument.findGrep();

for (i = 0; i < f.length; i++)

   if (f.paragraphs.previousItem(x).appliedParagraphStyle.name == "sectionA")

      f.spaceBefore = 0;

Note: previousItem() is very slow. This (arcane-looking) formulation is much quicker:

if (f.parentStory.insertionPoints[f.index-1].paragraphs[0])

Peter

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
Enthusiast ,
Mar 24, 2011 Mar 24, 2011

Hi Peter,

Note: previousItem() is very slow.

I am little messed why the previous item is very very slow. Few months ago I had the same problem. I have tried with "Next item" also I got the same result here too. Is there any way to overcome that.

Thanks,

Green4ever

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 ,
Mar 24, 2011 Mar 24, 2011

nextItem() and previousItem() are slow because they (re)build a collection (of paragraphs, pages, etc) every time they're called. There are different ways of getting around that, it depends on the type of object you want to get the next or previous item of. For paragraphs you can use this:

MyNextPar = MyPar.insertionPoints[-1].paragraphs[0];

MyPrevPar = MyPar.parentStory.insertionPoints[MyPar.index-1].paragraphs[0];

Pages are less of a problem, but can be sped up as well. I usually first get an array of all pages using something like this:

DocPages = app.activeDocument.pages.everyItem().getElements();

then later this:

MyNextPage = DocPages[MyPage.documentOffset+1];

MyPreviousPage = DocPages[MyPage.documentOffset-1];

Peter

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
Enthusiast ,
Mar 24, 2011 Mar 24, 2011

Thanks Peter. I'll keep in my mind. Most of the times i'll use it for paragraphs only. So far I did not faced any problem for the pages while using nextItem() and previousItem(). As you said it will help in speed up the process. Once again thanks for giving some trick 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 Expert ,
Mar 24, 2011 Mar 24, 2011

This ought to get you started -- but Sjoerd, there is no way you can 'attach' this kind of script to a paragraph style. That's why you have to run the script *every time* you changed a style somewhere ...

app.findTextPreferences = null;
app.findTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item("Heading 1");
sourcelist = app.activeDocument.findText();
targetlist = [];
for (l=0; l<sourcelist.length; l++)
{
next = sourcelist.paragraphs.nextItem(sourcelist.paragraphs[0]);
if (next != null && next.appliedParagraphStyle.name == "Heading 2")
  targetlist.push(sourcelist);
}
alert (sourcelist.length+" -> "+targetlist.length);
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 ,
Mar 24, 2011 Mar 24, 2011

Three scripts in ten minutes! They differ only in that you and Jongware look at the next paragraph, I at the previous one. That's because in my experience, space is removed from before the second paragraph, not from after the first one. There is often a difference.

Peter

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
Participant ,
Mar 24, 2011 Mar 24, 2011

I import XML with style mapping via XSLT, so I've to run the script once after importing the XML!

Thank you all!

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