将小写字符转换为大写的脚本与服务操作的工作方式不同
我正在尝试一个简单的脚本作为自动化程序中执行此功能的服务操作:
在任何应用程序中接收选定的文本并替换选定的文本 文本包含大写字母
所以我使用了这个脚本:
on run {input, parameters}
set upperCaseString to ""
repeat with i in input
if (ASCII number i) > 96 and (ASCII number i) < 123 then
set upperCaseString to upperCaseString & (ASCII character ((ASCII number i) - 32))
else
set upperCaseString to upperCaseString & (ASCII character (ASCII number i))
end if
end repeat
return upperCaseString
end run
但我发现了这个问题:
它返回输入的第一个字母作为大写字母,例如。 输入 - 小写文本,输出 - L,而预期输出为 - 小写字母文本。
为了检查问题,我在重复循环中添加了这行代码:
display dialog i
并发现它一次显示完整的文本而不是单个字符,即。它不是以小写文本显示 l.. o.. w..,而是立即显示小写文本。
谁能告诉我为什么它在 Apple 脚本编辑器中工作正常时却以服务操作的形式困扰我?
I am trying a simple script as a service action in automator which performs this function:
Receives selected text in any application and replaces selected text
with the text containing capital letters
So I used this script:
on run {input, parameters}
set upperCaseString to ""
repeat with i in input
if (ASCII number i) > 96 and (ASCII number i) < 123 then
set upperCaseString to upperCaseString & (ASCII character ((ASCII number i) - 32))
else
set upperCaseString to upperCaseString & (ASCII character (ASCII number i))
end if
end repeat
return upperCaseString
end run
But I found this problem:
It was returning first letter of input as an upper case letter, eg.
input - lowercasetext, output - L, whereas the expected output was -
LOWERCASETEXT.
To check the problem I added this line of code in repeat loop:
display dialog i
and found that it is displaying complete text in place of single character at a time ,ie. in place of displaying l.. o.. w.. in lowercasetext it is displaying lowercasetext at once.
Can anyone suggest me why is it bugging me as service action while it is working fine in Apple Script Editor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这适用于很多语言:
This works for a lot of languages:
当我运行你的脚本时,我得到了正确的结果。但您可能想做的一件事是将结果显式强制为文本。最简单的方法是在最后:
这可能会或可能不会为您做这件事,但如果您在可能存在歧义时明确强制数据,您将避免很多挫败感。
另一种(更快)的方法是利用 Unix tr (translate) 命令 via
do shell script
:这对于“英语”语言来说已经足够了,但您也可以添加变音翻译,例如
tr会将任何内容翻译为任何内容,因此您可以添加您可能遇到的任何字符以及您想要的内容希望他们能翻译成。我想到了“利特语”翻译。
When I run your script, I get the correct result. But one thing you may want to do is to explicitly coerce your result to text. The easiest way to do that would be at the end:
That may or may not do it for you, but you'll avoid a lot of frustration if you explicitly coerce data when there is a possibility of ambiguity.
Another (faster) way is to leverage the Unix tr (translate) command the via
do shell script
:That's enough for 'English' language, but you can also add diacritical translation, like so
tr will translate anything to anything, so you can add any characters you may encounter and what you'd like them to translate to. A 'leet-speak' translator comes to mind.
如果输入变量设置为列表,您将在 AppleScript 编辑器中获得相同的结果。 Automator 操作的输入参数也是一个列表,因此您的比较并没有按照您的想法进行。请注意,文本 ID 已废弃ASCII 字符 和 ASCII 数字 命令 - 请参阅 10.5 AppleScript 发行说明。
You will get the same result in the AppleScript Editor if the input variable is set to a list. The input parameter of an Automator action is also a list, so your comparison isn't doing what you think. Note that text id's have obsoleted ASCII character and ASCII number commands - see the 10.5 AppleScript Release notes.
@Matt Strange:
您也可以尝试:
如果您在“OS X 10.10”上运行“man tr”,您可能会看到应该使用字符类 [:lower:] 和 [:upper:],而不是像 ' 这样的显式字符范围。 az' 或 'AZ',因为它们可能不会产生正确的结果,正如手册页上所解释的那样。
@Matt Strange:
You could also try:
If you run 'man tr' on 'OS X 10.10' you may see that the character classes [:lower:] and [:upper:] should be used instead of explicit character ranges like 'a-z' or 'A-Z', since these may not produce correct results as it is explained there, on the manual page.