Change all caps

15 December 2004 · 0 comments

Today I’ve had a raging success with an Applescript problem which has stumped me for at least a couple of years. When I update the CommunityNet Aotearoa website I use Tex-Edit Plus and Applescripts to tidy and recode submitted items. One group regularly submit texts which go like this: Trainer NIGERIA blah blah blah Engineer [...]

 

Today I’ve had a raging success with an Applescript problem which has stumped me for at least a couple of years. When I update the CommunityNet Aotearoa website I use Tex-Edit Plus and Applescripts to tidy and recode submitted items. One group regularly submit texts which go like this:

Trainer

NIGERIA

blah blah blah

Engineer

SOLOMON ISLANDS

blah blah blah

My script at the moment just does a find and replace for each placename (along with a swag of other actions) but I wanted to convert any words in all caps to word caps.

On the off-chance this is interesting to others, the script is below. The first part puts the text in a new window for safety. The script looks for runs of at least 3 capital letters as I don’t want to include NZ in my catch.

tell application "Tex-Edit Plus"

  set theText to the contents of window 1

  make new window with data theText

  select insertion point before character 1 of front window

  repeat

    search window 1 looking for "[A-Z]{3,}" with grep, ¬

      whole words matching and cases matching

    if result is not false then

      change case of selection into word caps

      set color of selection to red

    else

      exit repeat

    end if

  end repeat

end tell

Comments: I’m not sure why I need the “cases matching” since I’m using grep to catch upper case only, but if I take that out the script fails. I like to change the colour of any changes so I can easily see what’s happened.

Note: Doug Adams AppleScripts for Tex-Edit Plus Archives is a treasure trove of help and information. I don’t know much about AppleScript, but most of what I know has been learned from there.

Clip to Evernote

Be the first to comment ⇒

Tell us what you think.
Note: there may be a delay before your comment appears. I now approve all comments from new visitors, in an attempt to keep spam at bay.

Add your Comment

Older Post:

Newer Post: