如何在AppleScript中将一系列单词转换为驼峰式大写字母? [英] How can I convert a series of words into camel case in AppleScript?

查看:188
本文介绍了如何在AppleScript中将一系列单词转换为驼峰式大写字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改Dragon Dictate,它可以使用一系列已讲过的单词来执行AppleScript.我需要找出如何获取包含这些单词的字符串并将其转换为驼峰式大小写的方法.

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

因此,如果我设置了一个宏来执行上述脚本(称为骆驼),并且说骆驼字符串和字符串",那么dictatedText将设置为字符串字符串".这是DD的一个很酷的功能.但是我不知道AppleScript,所以我不知道如何将带字符串的字符串"转换为驼峰式的字符串,即stringWithString.

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.

推荐答案

如果只需要将短语转换为骆驼文字,这就是我的处理方法:

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

由于AppleScript认为"a" = "A"是真实的,因此您只需要将任何所需的字母与其大写字母进行比较,然后将其替换即可.

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

我希望这会有所帮助.

这篇关于如何在AppleScript中将一系列单词转换为驼峰式大写字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆