I found a regular expression online and adapted it for BBEdit. If you replace 
the else conditional of your script with this it seems to work.

tell application "BBEdit"
  tell window 1
    if (selection as text) is "" then
      set cursorPoint to characterOffset of selection
      find "\\b\\w" options {search mode:grep, backwards:true} with selecting 
match
      set selection to (grep substitution of "\\U&")
      select insertion point before character cursorPoint
    else
      replace 
"\\b(a)(?!(nd?|s|t)?\\b)|\\b(b)(?!(ut|y)?\\b)|\\b(f)(?!(or|rom)?\\b)|\\b(i)\\b|\\b(i)(?!(n|nto|t)?\\b)|\\b(n)(?!(or)?\\b)|\\b(o)(?!(f|n|nto|r)?\\b)|\\b(s)(?!(o)?\\b)|\\b(t)(?!(he|o)?\\b)|\\b(w)(?!(ith)?\\b)|\\b([^abfinostw])(?!\\b)"
 using "\\u\\0" options {search mode:grep} searching in selection
    end if
  end tell
end tell

The find pattern is kind of a bear, but it breaks down to selecting the first 
letter of a word which is not equal to one of the items in your list. You can 
use this directly in BBEdit and then it's double escaped in the AppleScript 
above. For example, it selects an "a" which starts a word and is not followed 
by "n", "nd", "s", or "t", a "b" which starts a word and is not followed by 
"ut" or "y", etc.

\b(a)(?!(nd?|s|t)?\b)|\b(b)(?!(ut|y)?\b)|\b(f)(?!(or|rom)?\b)|\b(i)\b|\b(i)(?!(n|nto|t)?\b)|\b(n)(?!(or)?\b)|\b(o)(?!(f|n|nto|r)?\b)|\b(s)(?!(o)?\b)|\b(t)(?!(he|o)?\b)|\b(w)(?!(ith)?\b)|\b([^abfinostw])(?!\b)

The replacement pattern is \u\0 which changes the found character to uppercase.

The regex pattern I adapted can be found here, but it uses a different engine 
than BBEdit and didn't code quite the same list of words.

http://indesignsecrets.com/grep-solution-to-flawed-title-case-feature.php

[fletcher]

> On Oct 20, 2015, at 6:21 PM, Michelle <[email protected]> wrote:
> 
> Can anyone help me with this?  I have the following, very helpful AppleScript 
> that capitalizes the first letter of every word that I select/highlight:
> 
> tell application "BBEdit"
>  tell window 1
>  if selection as text is "" then
>  set cursorPoint to characterOffset of selection
>  find "\\b\\w" options {search mode:grep, backwards:true} with selecting match
>  set selection to (grep substitution of "\\U&")
>  select insertion point before character cursorPoint
>  else
>  change case selection making capitalize words with replacing target
>  end if
>  end tell
> end tell
> 
> It does what it's supposed to do perfectly.  However, I also need it to make 
> (or keep) all of the following words lower-case: 
> 
> a
> an
> and
> as
> at
> but
> by
> for
> from
> in
> into
> it
> nor
> of
> on
> onto
> or
> so
> the
> to
> with
> 
> Basically, I need it to convert selected text to "Title Case."  For example, 
> both "converts a selected string of words to title case" and "CONVERTS A 
> SELECTED STRING OF WORDS TO TITLE CASE" need to become "Converts a Selected 
> String of Words to Title Case." 
> 
> I'd greatly appreciate the help.
> 
> Thank you!
> 
> -- 
> This is the BBEdit Talk public discussion group. If you have a 
> feature request or would like to report a problem, please email
> "[email protected]" rather than posting to the group.
> Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>
> 
> --- 
> You received this message because you are subscribed to the Google Groups 
> "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].

-- 
This is the BBEdit Talk public discussion group. If you have a 
feature request or would like to report a problem, please email
"[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>

--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].

Reply via email to