2009-09-07 38 views
2

我正在嘗試在OSX leopard中創建一項服務,該服務計算所選文本的字數。我有自動機設置爲運行AppleScript,與下面放它:AppleScript字數統計服務

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

當我編譯腳本,它說,它不能指望每一個字。我究竟做錯了什麼?

感謝您的幫助,

埃利奧特

回答

3

首先,我想你在Automator的測試這一點,這就是錯誤的發生?如果是這樣,可能的問題是沒有輸入 - 所以它不能計算無關的詞。爲了成功測試它,您需要在運行AppleScript操作之前臨時添加「獲取指定文本」操作,並在該字段中輸入一些測試文本。在將其用作實際服務之前,您必須刪除「獲取指定的文本」操作。

其次,你需要爲了得到正確的計數使用

count words of (input as string) 

,否則將返回零。

+0

感謝您的答案 - 我有沒有使用字符串的問題,所以謝謝你澄清了。 – 2009-09-08 02:01:01

3

我做了一個在這裏,在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動作。將此代碼粘貼到文本框中,並保存爲Word和Character Count。現在切換到一個新的應用程序,選擇一些文本,並打開上下文菜單找到新的選項。