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

╄→尐↘猪︶ㄣ 提交于 2019-12-23 01:01:07

问题


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:


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.



来源:https://stackoverflow.com/questions/31100992/how-can-i-convert-a-series-of-words-into-camel-case-in-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!