Re: Need help with AppleScript that converts a selected string of words to "Title Case"

2015-10-21 Thread Christian Boyce
Hi Michelle. I did this a few years ago and modified it a few times too. You 
will find an array of words that you want to keep lowercase in the script— 
first you see the array capitalized, then you see it lowercase (the way you 
want it).

I found, after using my original script, that there were exceptions: words like 
“MacBook” and “iPhone” and “iTunes” needed to stay capitalized as they are. I 
made a list of words like those and put them in a text file. You would do this 
too. The name of the file doesn’t matter as it will be stored as a property. 
Next time you run the script, it will not ask you to locate the file as it will 
already know where it is. You can edit that file as time goes by— easier than 
editing the script since it’s just a list of words. You could delete this 
section of the script of course.

I also found that even though you don’t want to capitalize words like “a” and 
“an” you might need to anyway. For example, if you have a sentence like “A 
horse is not a good house pet” you want the first “a” to be capitalized and the 
second “a” not to be. So I added code to test whether I was leading a sentence 
with one of those words. You could throw this part of the script out too.

I also found that I was messing up URLs. Some are case-sensitive. So, if it’s a 
URL, I save it and put it back. In my case I knew that the URL would be on a 
line by itself, so I test for that. This should be generalized but I didn’t do 
it.

I also set this script up to allow you to display a dialog box asking whether 
you want to use the regular Change Case box or this new one. But it is 
commented out here. 

Hoping this helps and makes sense. Here’s the script.

property the_file : ""
--If there is no value for "the_file" there will be the next time you run the 
script.
-- by Christian Boyce, mac...@christianboyce.com, www.christianboyce.com
-- Using "grep" throughout.
-- February 18th, 2011. Corrected Step 2 for single-quotes. 
-- Revised June 4th, 2011. Changed initial case change step to better method.
-- Revised again June 22nd, 2011. Was making errors by mistakenly affecting 
URLs. 
-- Revised/corrected June 24th, 2011 to catch case where there aren't any links.
-- Created new method in which lines that begin with "http" are stored, and 
later put back into the changed text. Much more efficient than working line by 
line. Other speed improvements also. 
--
--This script, with the "on run {} and "end run" wrapper, works when it is
--in the Menu Scripts folder, inside the BBEdit folder, inside the Application 
Support folder,
--in your user Library ("~/Library/Application Support/BBEdit/Menu Scripts/").
--
--The script gives the option of using the original Change Case command.
(*
-- we would use this On Run handler if we put the script into the Menu Scripts 
folder.
--
on run
-- The run handler is called when the script is invoked normally,
-- such as from BBEdit's Scripts menu.
changecaseformichelle()
end run
*)
--
(*
--commented out because we are not going to attach to a menu item after all
on menuselect()
-- The menuselect() handler gets called when the script is invoked
-- by BBEdit as a menu script.
-- true means we do the changecaseformichelle script instead of the 
normal command
-- false means we do the normal command
--
set the_status to changecaseformichelle()
return the_status
end menuselect
*)
--
-- this would be called "on changecaseformichelle() if we wanted to put this in 
the Menu Scripts folder.
on run {}
(*
-- User gets to choose whether to use standard Change Case or this 
modified version. Added 2-14-2011. Happy Valentine's Day.
set the_button to display dialog "Would you like to use the standard 
Change Case, or Michelle's version?" buttons {"Standard", "Michelle's"} default 
button "Michelle's"
*)
--set the_button to button returned of the_button-- as if it had been 
clicked.
set the_button to "Michelle's"

if the_button is "Michelle's" then
set the_exceptions_upper to {"A", "An", "And", "As", "At", 
"But", "By", "For", "From", "In", "Into", "It", "Nor", "Of", "On", "Onto", 
"Or", "so", "The", "To", "with"}
set the_exceptions_lower to {"a", "an", "and", "as", "at", 
"but", "by", "for", "from", "in", "into", "it", "nor", "of", "on", "onto", 
"or", "so", "the", "to", "with"}

-- Reading exceptions from a file
if the_file = "" then
set the_file to choose file with prompt "Choose the 
file where you store the exceptions."
end if
set the_text to read the_file
set replace_with to paragraphs of the_text

-- we will use "replace_with" in Part 4 below--

tell application "BBEdit"


Re: Need help with AppleScript that converts a selected string of words to "Title Case"

2015-10-21 Thread Christopher Stone
On Oct 20, 2015, at 20:21, Michelle  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:
__

Hey Michelle,

This sort of job is precisely why I've used the Satimage.osax 
 
since 2003.

The fixedCaseWordList lets you designate the case of any given word.  It is 
arranged vertically for easy maintenance and sorting.

First and last words in the title are adjusted at the end of the script.

On my system the handlers are hidden in a library.

Back in the days I could use Eudora I had a 1000+ line script that reformatted 
reply-text using various rules for different email lists and people.  It 
hijacked Reply's keyboard shortcut and was only about 0.2 seconds slower than a 
normal reply.

I had a similar script for editing the junk out of list-mail I wanted to keep.

--
Best Regards,
Chris

---
# Auth: Christopher Stone 
# dCre: 2015/10/21 17:20
# dMod: 2015/10/21 17:52 
# Appl: BBEdit & the Satimage.osax
# Task: Change case of selected text to title-case.
# Libs: None
# Osax: Satimage.osax { http://tinyurl.com/dc3soh }
# Tags: @Applescript, @Script, @BBEdit, @Change, @Case, @Selected, @Text
---
# SCRIPT REQUIRES INSTALLATION OF THE SATIMAGE.OSAX APPLESCRIPT EXTENSION!
---
set fixedCaseWordList to paragraphs 2 thru -2 of "
a
AFL-CIO
an
and
as
at
but
by
for
from
in
into
it
NAACP
nor
of
on
onto
or
so
the
to
with
"
set lowerCaseWordRegEx to change "(.+)" into "b\\1b" in 
fixedCaseWordList with regexp without case sensitive
set _text to getSelectionOfNamedBBEditWindow(1)
set _text to titlecase _text
set newText to change lowerCaseWordRegEx into fixedCaseWordList in _text with 
regexp without case sensitive
set newText to change "^(\\w)" into "\\u\\1" in newText with regexp without 
case sensitive
set newText to change "(\\w)(\\w*)$" into "\\u\\1\\2" in newText with regexp 
without case sensitive
setBBEditTextSelectionTo(newText)

---
--» HANDLERS
---
on getSelectionOfNamedBBEditWindow(_window)
  tell application "BBEdit"
if _window = "front" then
  set _window to window (name of front text window)
else if class of _window = integer or class of _window = text then
  set _window to text window _window
end if

tell _window to return contents of selection

  end tell
end getSelectionOfNamedBBEditWindow
---
on setBBEditTextSelectionTo(_text)
  tell application "BBEdit" to set contents of selection's text to _text
end setBBEditTextSelectionTo
---



-- 
This is the BBEdit Talk public discussion group. If you have a 
feature request or would like to report a problem, please email
"supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: 

--- 
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 bbedit+unsubscr...@googlegroups.com.
To post to this group, send email to bbedit@googlegroups.com.


Re: Need help with AppleScript that converts a selected string of words to "Title Case"

2015-10-21 Thread TJ Luoma


I have noticed this with most “titlecase” programs that I have 
tried. My solution has been to first make everything lowercase and then 
apply the titlecase, which seems to work more reliably.


YMMV.

TjL


On 21 Oct 2015, at 2:23, Michelle wrote:


Hello, Fletcher,

Thanks!  It's a big improvement over my original.  However, it's still 
not

quite 100% there.  Couple things:

1.  I need it to change "THE" and "The" to "the" (and the same for all 
of

the other words that I listed).  Currently, it leaves lower-case "the"
uncapitalized, which is good, but it completely ignores "THE" and 
"The."


2.  If one of the listed words happens to be the first or last word of 
a

string, it should be capitalized.

Thanks again!  I hope that many others can benefit from this, too.

Michelle



On Tuesday, October 20, 2015 at 10:21:33 PM UTC-7, flet...@cumuli.com 
wrote:


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]




--
This is the BBEdit Talk public discussion group. If you have a
feature request or would like to report a problem, please email
"supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: 

---
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 bbedit+unsubscr...@googlegroups.com.

To post to this group, send email to bbedit@googlegroups.com.


--
This is the BBEdit Talk public discussion group. If you have a 
feature request or would like to report a problem, please email

"supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: 

--- 
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 bbedit+unsubscr...@googlegroups.com.
To post to this group, send email to bbedit@googlegroups.com.


Re: Need help with AppleScript that converts a selected string of words to "Title Case"

2015-10-21 Thread Michelle
Hello, Fletcher,

Thanks!  It's a big improvement over my original.  However, it's still not 
quite 100% there.  Couple things: 

1.  I need it to change "THE" and "The" to "the" (and the same for all of 
the other words that I listed).  Currently, it leaves lower-case "the" 
uncapitalized, which is good, but it completely ignores "THE" and "The."

2.  If one of the listed words happens to be the first or last word of a 
string, it should be capitalized. 

Thanks again!  I hope that many others can benefit from this, too.

Michelle



On Tuesday, October 20, 2015 at 10:21:33 PM UTC-7, flet...@cumuli.com wrote:
>
> 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] 
>
>

-- 
This is the BBEdit Talk public discussion group. If you have a 
feature request or would like to report a problem, please email
"supp...@barebones.com" rather than posting to the group.
Follow @bbedit on Twitter: 

--- 
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 bbedit+unsubscr...@googlegroups.com.
To post to this group, send email to bbedit@googlegroups.com.