Copy link to clipboard
Copied
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
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
...
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
Ow, that sounds like a good option, but how do you define that in a paragraph style?
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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??
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thanks Stoeree, I'll give that a try.
And thanks to everyone who made suggestions - I'm learning, slowly!
Iain
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I import XML with style mapping via XSLT, so I've to run the script once after importing the XML!
Thank you all!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now