AppleScript 字数统计服务

发布于 2024-08-03 20:23:38 字数 300 浏览 2 评论 0原文

我正在尝试在 OSX Leopard 中创建一项服务来计算所选文本的字数。我将自动化程序设置为运行一个 applescript,其中包含以下内容:

on run {input, parameters}
        count words of input
        display alert "Words: " & input
        return input
end run

当我编译脚本时,它说它无法计算每个单词。我做错了什么?

感谢您的帮助,

埃利奥特

I am trying to create a service in OSX leopard that counts the number of words of selected text. I have automator set to run an applescript, with the following put in it:

on run {input, parameters}
        count words of input
        display alert "Words: " & input
        return input
end run

When I compile the script, it says it cannot count every word. What am I doing wrong?

Thanks for the help,

Elliott

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东北女汉子 2024-08-10 20:23:38

首先,我假设您正在 Automator 中对此进行测试,这就是发生错误的地方?如果是这样,可能的问题是没有输入,因此它无法计算空的单词数。为了成功测试它,您需要在“运行AppleScript”操作之前临时添加“获取指定文本”操作,并在该字段中输入一些测试文本。在将其用作实际服务之前,您必须删除“获取指定文本”操作。

其次,您需要使用

count words of (input as string)

以获得正确的计数,否则它将返回零。

First of all, I presume you are testing this in Automator, and that's where the error is taking place? If so, the likely problem is that there is no input—so it can't count the words of nothing. In order to test it successfully, you need to temporarily add a "Get Specified Text" action before the Run AppleScript action, and enter some test text into that field. You'll have to remove the Get Specified Text action before using it as an actual service.

Secondly, you need to use

count words of (input as string)

in order to get a proper count, otherwise it'll return zero.

2024-08-10 20:23:38

我在 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”操作。将此代码粘贴到文本框中,然后另存为“字数和字符数”。现在切换到新应用程序,选择一些文本,然后打开上下文菜单以查找新选项。

I made one here, on Github:

https://gist.github.com/1616556

The current source is:

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

Use Automator.app to create a new service, and then select the Run AppleScript action. Paste this code in to the text box, and save as Word and Character Count. Now switch to a new app, select some text, and open the context menu to find the new option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文