我正在尝试在 OSX leopard 中创建一个服务来计算所选文本的字数。我已经将自动机设置为运行一个applescript,其中包含以下内容:
on run {input, parameters}
count words of input
display alert "Words: " & input
return input
end run
当我编译脚本时,它说它不能计算每个单词。我究竟做错了什么?
谢谢您的帮助,
艾略特
我正在尝试在 OSX leopard 中创建一个服务来计算所选文本的字数。我已经将自动机设置为运行一个applescript,其中包含以下内容:
on run {input, parameters}
count words of input
display alert "Words: " & input
return input
end run
当我编译脚本时,它说它不能计算每个单词。我究竟做错了什么?
谢谢您的帮助,
艾略特
首先,我假设您正在 Automator 中对此进行测试,而这就是发生错误的地方?如果是这样,可能的问题是没有输入——所以它不能数空话。为了成功测试它,您需要在 Run AppleScript 操作之前临时添加一个“获取指定文本”操作,并在该字段中输入一些测试文本。在将其用作实际服务之前,您必须删除 Get Specified Text 操作。
其次,你需要使用
count words of (input as string)
为了获得正确的计数,否则它将返回零。
我在这里做了一个,在 Github 上:
https://gist.github.com/1616556
目前的来源是:
on run {input, parameters}
tell application "System Events"
set _appname to name of first process whose frontmost is true
end tell
set word_count to count words of (input as string)
set character_count to count characters of (input as string)
tell application _appname
display alert "" & word_count & " words, " & character_count & " characters"
end tell
return input
end run
使用 Automator.app 创建新服务,然后选择运行 AppleScript 操作。将此代码粘贴到文本框中,并另存为字数和字符数。现在切换到一个新应用程序,选择一些文本,然后打开上下文菜单以找到新选项。