How can I convert a series of words into camel case in AppleScript?

769 views Asked by At

I'm trying to modify Dragon Dictate, which can execute AppleScript with a series of words that have been spoken. I need to find out how I can take a string that contains these words and convert it to camel case.

on srhandler(vars)
    set dictatedText to varDiddly of vars
    say dictatedText
end srhandler

So if I set up a macro to execute the above script, called camel, and I say "camel string with string", dictatedText would be set to "string with string". It's a cool feature of DD. However I don't know AppleScript, so I don't know how to convert "string with string" to camel-case i.e. stringWithString.

If I could learn this basic thing, I could perhaps finally start programming by voice which would be better than dealing with chicklet keyboards and gamer keyboards, which are prevalent but I find them to be awful.

1

There are 1 answers

0
Craig Smith On BEST ANSWER

If you only need to convert a phrase to camel text, here is how I would do it:

set targetString to "string with string"
set allCaps to every character of "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
global allCaps
set camel to my MakeTheCamel(targetString)

to MakeTheCamel(txt)
    set allWords to every word of txt
    set camelString to ""
    repeat with eachWord in allWords
        set char01 to character 1 of (eachWord as text)
        set remainder to characters 2 thru -1 of (eachWord as text)
        repeat with eachChar in allCaps
            if char01 = (eachChar as text) then
                set camelString to camelString & (eachChar as text) & (remainder as text)
                exit repeat
            end if
        end repeat
    end repeat
    return camelString
end MakeTheCamel

Since AppleScript considers "a" = "A" to be true, you need only compare any desired letter to its capitalized equivalent, and replace it.

I hope this helps.