Content

The discovery, acceptance & management of life's gaps

Using Applescript & Python To Set Title Case (AppleScrunix)

Thursday 30 June 2011 - Filed under automation + gaps + Technology

One way in which Apple has turned the Mac into the most powerful scripting/automation platform on the planet has been to provide a litany of scriptable languages as part of the standard install. Just some of the languages you will find in Mac OS X are JavaScript, Ruby, Python, and of course AppleScript. I refer to the mashing all these languages together as AppleScrunix.

Each of the languages has strengths and weaknesses. By bringing them all together on a single platform, and making it easy for them to be used together, Apple has put an amazing amount of automation power in our hands.

As shown in previous posts AppleScrunix allows me to work primarily in AppleScript and draw from other languages when my scripting language of choice comes up a bit short.

I often run across the need to do ‘simple’ title case conversion. ‘Simple’ meaning every word in the title has its first letter capitalized and all remaining letters are lowercase. Though this is possible using only standard AppleScript, the project quickly becomes complicated and ends up running slower than it needs to.

If you are interested in solving this problem just using AppleScript, you can get much of the code you need from my previous post, Applescript – Converting Uppercase & Lowercase (AppleScrunix Style).

Or you can do it using AppleScrunix and just 2 lines of code. I have messed up the title ahead of time to make it more interesting… thIs is thE stORY of a GIRL becomes This Is The Story Of A Girl.

Begin Script…

set myTitle to "thIs is thE stORY of a GIRL"

do shell script "python -c \"print " & quoted form of (myTitle) & ".title()\""


…End Script

Result: “This Is The Story Of A Girl”


AppleScript / AppleScrunix Examples – using the do shell script command in AppleScript.

2011-06-30  »  Russ Leseberg

Talkback x 2

  1. Jason
    2 August 2011 @ 6:44 pm

    I am trying to implement this using coda and can’t seem to get it to work. This is what I have :

    – script settings
    on CodaScriptSettings()
    return {displayName:”Titlecase”, inContextMenu:”yes”}
    end CodaScriptSettings

    – actual script
    tell application “Coda”

    try

    tell current split of front document

    if selected text is not equal to “” then
    set someText to selected text
    else
    set someText to contents
    end if

    end tell

    on error
    beep
    return
    end try

    end tell

    set shellscriptString to do shell script “python -c \”print ” & quoted form of (someText) & “.title()\”"

    set shellresult to do shell script shellscriptString without altering line endings

    tell application “Coda”
    try
    tell current split of document 1

    if selected text is not equal to “” then
    set selected text to shellresult

    else
    set contents to shellresult
    end if

    end tell

    on error
    beep

    end try
    end tell

  2. Russ Leseberg
    13 August 2011 @ 12:57 pm

    I don’t personally use coda so I don’t have a ready answer. A couple of questions: Are you trying to create a coda plugin? Have you gotten AppleScript in any form to work in your coda code? Or.. are you trying to use Applescript to enhance the coda edit experience?

Share your thoughts

Re: Using Applescript & Python To Set Title Case (AppleScrunix)







Tags you can use (optional):
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>