Publi-Script Forums

Applescript / Javascript => Scripting in general => Topic started by: AutoFetishist on November 23, 2010, 09:44:08 AM

Title: restricting a search to ...
Post by: AutoFetishist on November 23, 2010, 09:44:08 AM
a basic question,

I want to run search/replace routines in Tex Edit +
BUT I want to restrict the search to just bold occurrences.

How would I enable that, in the routine below?:

 repeat with ThisText in RunInWords
            set ThisText to ThisText as string
                    if paragraph p contains ThisText then
                         --if the occurrence of ThisText in para p is bold then
                         replace paragraph p looking for "^*" looking for styles {style:{bold}, color:blue} replacing with "@stylename:" replacing with styles {style:{bold}, color:green}
                    end if
                    --end if
end repeat
               
Title: Re: restricting a search to ...
Post by: Publi-Script on November 23, 2010, 01:18:26 PM
I believe your code is setup properly, however, you do not need the "if paragraph.." bit. Tex-Edit Plus is already "looking for" that text for you and won't do anything if it does not find it.

HTH
Title: Re: restricting a search to ...
Post by: AutoFetishist on November 23, 2010, 01:24:00 PM
Right, but I only want it to flag bold instances of the string.
Won't I need to narrow the searching somehow, to prevent it catching plain or italic instances of the string? 
Title: Re: restricting a search to ...
Post by: Publi-Script on November 23, 2010, 01:37:35 PM
Your current code will only look for the specified string on text that is  BOLD and BLUE and will replace it with your "@stylename:" set in BOLD and GREEN.

The key here is the "looking for styles" bit. That is what is narrowing your searches.

HTH
Title: Re: restricting a search to ...
Post by: AutoFetishist on November 23, 2010, 01:59:58 PM
Aha.
I see now. I wasn't explaining properly.

What I'm doing with this line: 
replace paragraph p looking for "^*" looking for styles {style:{bold}, color:blue} replacing with "@stylename:" replacing with styles {style:{bold}, color:green}

...is if one of my search strings is found, in bold, to then have the script apply a different QXP style sheet.

So if "dog" is one of my strings, the script would change:

   @stylesheet a: the dog is happy
to
   @stylesheet x: the dog is happy

So I need to first find the string (in bold), and that's what I don't know how to do.

Title: Re: restricting a search to ...
Post by: Publi-Script on November 23, 2010, 02:06:39 PM
Well currently your script is looking for EVERY TEXT set in BOLD and BLUE
What I think you want is:

replace paragraph p looking for ThisText looking for styles {Style:{bold}} replacing with ("@stylename:^*") replacing with styles {color:green}
Title: Re: restricting a search to ...
Post by: AutoFetishist on November 23, 2010, 02:35:38 PM
Why would it change every text?
I have my "change paragraph p" command nested inside an if block:


 repeat with ThisText in RunInWords
            set ThisText to ThisText as string
                    if paragraph p contains ThisText then
                         replace paragraph p looking for "^*" looking for styles {style:{bold}, color:blue} replacing with "@stylename:" replacing with styles {style:{bold}, color:green}
                    end if
end repeat
               
Wouldn't that ensure that the script will only do the replace within the parargraphs I want it to?
Title: Re: restricting a search to ...
Post by: Publi-Script on November 23, 2010, 02:56:43 PM
Sorry, I meant to say every text in paragraph p that is set in BOLD and BLUE. That is what the wilcard "^*" mean and why I suggested you use [looking for ThisText] instead
Title: Re: restricting a search to ...
Post by: AutoFetishist on November 23, 2010, 03:15:41 PM
Right, it changes anything in that that's bold & blue.
Which will be the stylesheet name.

I don't want to actually do anything to the occurences of ThisText.

I'm just using their presence to then decide to change the stylesheet.

Does that clarify what I'm attempting?

Title: Re: restricting a search to ...
Post by: Publi-Script on November 23, 2010, 03:30:23 PM
Yes it does...

Does your code do what you want it to do?
Title: Re: restricting a search to ...
Post by: AutoFetishist on November 23, 2010, 03:44:08 PM
No.

I need to add some sort of code here:

               if paragraph p contains ThisText then

to make sure that my search only finds bold strings.

almost like this:
if paragraph p contains ThisText  in bold style then
 
Title: Re: restricting a search to ...
Post by: AutoFetishist on November 29, 2010, 10:51:16 AM
In other words, do the Tex Edit+ equivalent of this [QXP] search routine below:

(http://img.skitch.com/20101129-827n974bf5j1t4dwugcm2e5e1y.png)

Hope that makes sense.
Title: Re: restricting a search to ...
Post by: Publi-Script on November 29, 2010, 11:16:17 AM
Sorry, i got sidetracked on this.

I believe the following should do what you want:

Quote
001     tell application "Tex-Edit Plus"
002          repeat with ThisText in RunInWords
003               set ThisText to ThisText as string
004               if (search paragraph p looking for ThisText looking for styles {style:{bold}}) then
005                    replace paragraph p looking for "^*" looking for styles {style:{bold}, color:blue} replacing with "@stylename:" replacing with styles {style:{bold}, color:green}
006               end if
007          end repeat
008     end tell
Notes:
The line numbers included with this script are there to aid future discussions. In order to use this script, you will have to strip them all from the code.


HTH
Title: Re: restricting a search to ...
Post by: AutoFetishist on November 29, 2010, 01:54:39 PM
Eureka! You've got it!
Title: Re: restricting a search to ...
Post by: Publi-Script on November 29, 2010, 02:14:23 PM
Well, once I took the time to really read what you were asking instead of inferring/assuming, it all made sense to me.

Glad I could be of help!