Brulog

Words of occasional wisdom from Bruce Oakley

Change Case

Links: Tips-Traps | Change Case | Parse Data | Search-Replace

Purpose: Change case of selected text to all upper, all lower, or first letter of each word capitalized.
Operates on: A selection in the frontmost window of MediaSpan Media Software’s NewsEdit application.
How it works: Presents a dialog box with buttons “Lower,” “Upper,” “Cap Words.” Invokes a routine from the folks at Apple to replace characters according to the choice.
Assumptions, definitions: For the Cap Word choice, the script assumes the selection starts at a word boundary and considers any character preceded by a return, period or space as being at the start of a word.
Potential enhancements:

  • Add characters (tab, hyphen, open quote?) to the word-boundary test if return, space and period do not sufficiently define word boundaries for your purposes.
  • Add a “Cap Sentence” choice to cap the first letter of every sentence in a selection.
  • Adjust “Cap Words” to keep mid-word capitalization such as “AppleScript” or “O’Brien.”

Caution: The code is presented so that it can be copied and pasted into a Script Editor window, but as with any copy and paste, you should check for unintended line breaks or mistranslated special characters. Some script lines may be longer than the browser window; stretch the display to be sure.

Inline comments are indicated by two hyphens:
-- This is an inline comment.
Block comments are wrapped in parentheses and asterisks:
(* This is a block comment.
*)

The code:

--Declare variable scope
global new_text, selectedCase, selectedText, fixedText, thisChar, fixedChar

--Clear variables
set fixedText to ""
set fixedChar to ""
set thisChar to ""

--Take control of the application
tell application "NewsEditPro IQue"
  --A "try ... on error ... end try" loop captures errors
  try
    --Take control of the story in the frontmost window
    --Different applications will vary in the commands
    --for referring to window, story, text (word, paragraph, etc.)
    tell story 1
      considering case
        set selectedText to the selection
        display dialog "Set selection to" buttons {"Lower", "Upper", "Cap Words"} default button 1
        set selectedCase to button returned of result
        if selectedCase is not "Cap Words" then
          --If choice is "Lower" or "Upper," call the subroutine to make change
          set fixedText to my change_case(selectedText, selectedCase)
        else
          -- Cap first letter at any word boundary (return, space or period);
          -- lowercase all others
          repeat with i from 1 to count characters of selectedText
            set thisChar to character i of selectedText
            if i is 1 or (character (i - 1) of selectedText is return) or (character (i - 1) of selectedText is in " .") then
              set thisChar to my change_case(thisChar, "Upper")
            else
              set thisChar to my change_case(thisChar, "Lower")
            end if
            set fixedText to fixedText & thisChar
          end repeat
        end if
        set the selection to fixedText
      end considering
    end tell
  on error
    display dialog "You must have a file open and text selected!" & return with icon 2
  end try

end tell

--Subroutine compares against list of upper/lower letters and changes accordingly
on change_case(this_text, this_case)
  if this_case is "Lower" then
    set the comparison_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    set the source_string to "abcdefghijklmnopqrstuvwxyz"
  else if this_case is "Upper" then
    set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
    set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  end if
  set the new_text to ""
  repeat with this_char in this_text
    set x to the offset of this_char in the comparison_string
    if x is not 0 then
      set the new_text to ¬
        (the new_text & character x of the source_string) as string
    else
      set the new_text to (the new_text & this_char) as string
    end if
  end repeat
  return new_text
end change_case

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: