🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #9373

Re: Working with text frames...

#9373
From: harbs
Date: Tue, 22 Aug 2006 17:03
158 lines
6776 bytes
I wrote the script which was the reason for my post (about a week ago already). It was a running header script which I had in my head for almost a year and a half.

Here it is for anyone interested:

if(app.selection.length < 1){ 	alert("Please select some text and try again."); 	exit(0); 	} doc = app.activeDocument; myStory = doc.selection[0].parent; myStoryID = myStory.id;

var myDialog = app.dialogs.add({name:"Running Headers",canCancel:true});
with(myDialog){
	with(dialogColumns.add()){
		with(borderPanels.add()){
			staticTexts.add({staticLabel:"Type"});
			with(dialogColumns.add()){
				with(myTypeRadioButtons = radiobuttonGroups.add()){
					radiobuttonControls.add({staticLabel:"First Instance", checkedState:true});
					radiobuttonControls.add({staticLabel:"Last Instance"});
					radiobuttonControls.add({staticLabel:"First Word"});
					radiobuttonControls.add({staticLabel:"Last Word"});
					radiobuttonControls.add({staticLabel:"First and Last"});
					}
				}
			}
		with(borderPanels.add()){
			staticTexts.add({staticLabel:"Options:"});
			with(dialogColumns.add()){
				myHeaderOptionBx = checkboxControls.add({staticLabel:"Include Last Instance on Pages without source style", checkedState:false});
				}
			with(borderPanels.add()){
				with(dialogColumns.add()){
					staticTexts.add({staticLabel:"Prefix:"});
					staticTexts.add({staticLabel:"Suffix:"});
					staticTexts.add({staticLabel:"Separator (if all selected):"});
					}
				with(dialogColumns.add()){
					myPrefixField = textEditboxes.add({});
					mySuffixField = textEditboxes.add({});
					mySeparaterField = textEditboxes.add({});
					}
				}
			}
		with(borderPanels.add()){
			staticTexts.add({staticLabel:"Header Frame Label:"});
			with(dialogColumns.add()){
				myLabelField = textEditboxes.add({editContents:"header"});
				}
			}
		with(borderPanels.add()){
			with(dialogColumns.add()){
				staticTexts.add({staticLabel:"Source character style:"});
				staticTexts.add({staticLabel:"Header character style:"});
				}
			with(dialogColumns.add()){
				srcStyleDD = dropdowns.add( {stringList: [].concat( app.activeDocument.characterStyles.everyItem().name ), selectedIndex: 0} );
				hdrStyleDD = dropdowns.add( {stringList: [].concat( app.activeDocument.characterStyles.everyItem().name ), selectedIndex: 0} );
				}
			}
		}
	}
var myResult = myDialog.show();
if(myResult == true){
	MyHeaderType = myTypeRadioButtons.selectedButton;
	myPrefix = myPrefixField.editContents;
	mySuffix = mySuffixField.editContents;
	myLabel = myLabelField.editContents;
	mySeparater = mySeparaterField.editContents;
	srcStyleIdx = srcStyleDD.selectedIndex;
	hdrStyleIdx = hdrStyleDD.selectedIndex;
	myHeaderOption = myHeaderOptionBx.checkedState;
	myDialog.destroy();
	}
else{
	myDialog.destroy();
	}
//if (myPrefix != ""){
//	myPrefix = myPrefix + " ";
//	}
//if (mySuffix != ""){
//	mySuffix = " " + mySuffix;
//	}
srcStyle = app.activeDocument.characterStyles.item(srcStyleIdx);
hdrStyle = app.activeDocument.characterStyles.item(hdrStyleIdx);
//Lets cycle through pages
for (i=0;i<doc.pages.length;i++){ 	myStoryFrames = new Array; 	for(var j = 0; myStory.textFrames.length > j; j++){
		myFrames = myStory.textFrames[j];
		if (myStory.textFrames[j].parent == doc.pages[i]){
			myStoryFrames.push(myFrames);
			}
		}
	if (myStoryFrames.length > 0){
		myLength = myStoryFrames.length;
		myStart = myStoryFrames[0].characters[0].index;
		myEnd = myStoryFrames[(myLength-1)].characters[-1].index;
		mySearch = myStory.characters.itemByRange(myStart, myEnd).texts[0];
		app.findPreferences = app.changePreferences = null;
		myFinds = mySearch.search("", false, false, undefined, {appliedCharacterStyle:srcStyle});
		myFindsLength = myFinds.length - 1;
		if (myFinds.length > 0){
			if (MyHeaderType == 0){
				myReplaceText1 = "";
				myReplaceText2 = "";
				myReplaceText3 = myFinds[0].contents;
				}
			else if (MyHeaderType == 1){
				myReplaceText1 = "";
				myReplaceText2 = "";
				myReplaceText3 = myFinds[myFindsLength].contents;
				}
			else if (MyHeaderType == 2){
				myReplaceText1 = "";
				myReplaceText2 = "";
				myReplaceText3 = myFinds[0].words[0].contents;
				}
			else if (MyHeaderType == 3){
				myReplaceText1 = "";
				myReplaceText2 = "";
				myReplaceText3 = myFinds[myFindsLength].words[-1].contents;
				}
			else if (MyHeaderType == 4){
				if (myFinds.length > 1){
					myReplaceText1 = myFinds[0].contents;
					myReplaceText2 = mySeparater;
					myReplaceText3 = myFinds[myFindsLength].contents;
					}
				else {
					myReplaceText1 = "";
					myReplaceText2 = "";
					myReplaceText3 = myFinds[0].contents;
					}
				}
			myObjects=doc.pages[i].masterPageItems;
			for (k=0;k<myObjects.length;k++){ 				if (myObjects[k].label==myLabel){ 					myObjects[k].override(doc.pages[i]); 					} 				} 			app.findPreferences = app.changePreferences = null; 			myHdrFrames = doc.pages[i].textFrames; 			for (m=0;m<myHdrFrames.length;m++){ 				if (myHdrFrames[m].label==myLabel){ 					myHdrFinds = myHdrFrames[m].search("", false, false, undefined, {appliedCharacterStyle:hdrStyle}); 					if (myHdrFinds.length > 0){
						for (n=0;myHdrFinds.length > n;n++){
							myHdrFinds[n].contents = myPrefix + myReplaceText1 + myReplaceText2 + myReplaceText3 + mySuffix;
							}
						}
					}
				}
			}
		else if (myHeaderOption = true){
			app.findPreferences = app.changePreferences = null;
			myHdrFrames = doc.pages[i].textFrames;
			for (m=0;m<myHdrFrames.length;m++){ 				if (myHdrFrames[m].label==myLabel){ 					myHdrFinds = myHdrFrames[m].search("", false, false, undefined, {appliedCharacterStyle:hdrStyle}); 					if (myHdrFinds.length > 0){
						for (n=0;myHdrFinds.length > n;n++){
							try {
								myHdrFinds[n].contents = myPrefix + myReplaceText3 + mySuffix;
								} catch (e) {
								continue;
								}
							}
						}
					}
				}	
			}
		}
	}



Any critique is welcome :)
Technical Details
Message-ID: <3bc138b6.3@webcrossing.la2eafNXanI>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeed.freenet.de!news.albasani.net!news.ecp.fr!news.stben.net!webcrossing
References: <3bc138b6.-1@webcrossing.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Thread: f939a,319d2e63d27ba34a,start
X-Google-Attributes: gidf939a,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: harbs@adobeforums.com
Newsgroups: adobe.scripting.indesign
Subject: Re: Working with text frames...
Message-ID: <3bc138b6.3@webcrossing.la2eafNXanI>
Date: Tue, 22 Aug 2006 10:03:11 -0700
References: <3bc138b6.-1@webcrossing.la2eafNXanI>
Lines: 188
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeed.freenet.de!news.albasani.net!news.ecp.fr!news.stben.net!webcrossing