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

查看:57
本文介绍了如何在 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

因此,如果我设置一个宏来执行上述脚本,称为camel,并且我说camel string with string",则dictedText 将设置为string with string".这是 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天全站免登陆