しばらく返答が寄せられていないようです。 再度ディスカッションを開始するには、新たに質問してください。

Apple Scriptによる選択範囲の文字カウント(改行、スペース抜き)について

初めて質問させて頂きます。


文書作成でテキストエディットを使っているのですが、文字数のカウントができないため、以下の様なサイトを使用して文字数を都度調べている状態です。


http://www2u.biglobe.ne.jp/~yuichi/rest/strcount.html


上記のサイトは、改行を除いた文字数、改行、空白を除いた文字数を表示できるため、非常にありがたく使わせていただいていますが、Apple Scriptで同じことができないかと調べています。


以下のサイトにワードカウントの機能のApple Scriptがあるのですが、このscriptに改行、空白を除いた文字数カウントをするようにはどのようにできるのか、お教え願えればと思います。


http://macfan.jp/guide/2010/05/13/post_671.html


また、今回の件でApple Scriptを活用して色々と便利にMacを使いこなしたいので、上級者の方はどのような本などで勉強されてきたのかもお教え願えれば幸いです。

Mac mini, Mac OS X (10.7.5)

投稿日 2012/10/11 11:13

返信
スレッドに付いたマーク ベストな回答

投稿日 2012/10/12 07:17

こんにちは


ご指摘のJavascriptを用いた文字数カウンタと類似する機能のAppleScript例を下記に示します。

参考になれば幸いです。

--

on run

tell application "TextEdit"

set theText to the text of document 1

set word_count to count words of document 1

set char_count to count characters of document 1

set para_count to count paragraphs of document 1

end tell


set theLF to ASCII character 10

set LF_count to countSubstring(theText, theLF)

set space_count to countSubstring(theText, " ")

set show_words to "文字数:" & (char_count as string) & return & "改行を除いた文字数:" & ((char_count - LF_count) as string) & return & "空白、改行を除いた文字数:" & ((char_count - space_count - LF_count) as string) & return & "行数:" & (para_count as string)

set dialog_title to "TextEdit Word Count"

display dialog show_words with icon 1 with title dialog_title buttons {"OK"} default button "Ok"

end run


-- e.g. countSubstring("abc abc abc", "abc") --> 3


on countSubstring(theText, theSubstring)

-- ljr (http://applescript.bratis-lover.net/library/string/), modified from Aurelio.net (http://aurelio.net/doc/as4pp.html)

local ASTID, theText, theSubstring, i

set ASTID to AppleScript's text item delimiters

try

set AppleScript's text item delimiters to theSubstring

set i to (count theText's text items) - 1

set AppleScript's text item delimiters to ASTID

return i

on error eMsg number eNum

set AppleScript's text item delimiters to ASTID

error "Can't countSubstring: " & eMsg number eNum

end try

end countSubstring

--

参考文献:

http://applescript.bratis-lover.net/library/string/#countSubstring

http://hints.macworld.com/article.php?story=20070730101510623


動作環境:

-Mac OSX 10.6.8 Snow Leopard

-AppleScriptエディタ 2.3(118) / AppleScript 2.1.2

-テキストエディット 1.6(264)

返信: 45
スレッドに付いたマーク ベストな回答

2012/10/12 07:17 milestone への返信

こんにちは


ご指摘のJavascriptを用いた文字数カウンタと類似する機能のAppleScript例を下記に示します。

参考になれば幸いです。

--

on run

tell application "TextEdit"

set theText to the text of document 1

set word_count to count words of document 1

set char_count to count characters of document 1

set para_count to count paragraphs of document 1

end tell


set theLF to ASCII character 10

set LF_count to countSubstring(theText, theLF)

set space_count to countSubstring(theText, " ")

set show_words to "文字数:" & (char_count as string) & return & "改行を除いた文字数:" & ((char_count - LF_count) as string) & return & "空白、改行を除いた文字数:" & ((char_count - space_count - LF_count) as string) & return & "行数:" & (para_count as string)

set dialog_title to "TextEdit Word Count"

display dialog show_words with icon 1 with title dialog_title buttons {"OK"} default button "Ok"

end run


-- e.g. countSubstring("abc abc abc", "abc") --> 3


on countSubstring(theText, theSubstring)

-- ljr (http://applescript.bratis-lover.net/library/string/), modified from Aurelio.net (http://aurelio.net/doc/as4pp.html)

local ASTID, theText, theSubstring, i

set ASTID to AppleScript's text item delimiters

try

set AppleScript's text item delimiters to theSubstring

set i to (count theText's text items) - 1

set AppleScript's text item delimiters to ASTID

return i

on error eMsg number eNum

set AppleScript's text item delimiters to ASTID

error "Can't countSubstring: " & eMsg number eNum

end try

end countSubstring

--

参考文献:

http://applescript.bratis-lover.net/library/string/#countSubstring

http://hints.macworld.com/article.php?story=20070730101510623


動作環境:

-Mac OSX 10.6.8 Snow Leopard

-AppleScriptエディタ 2.3(118) / AppleScript 2.1.2

-テキストエディット 1.6(264)

2012/10/12 12:28 kimukazu への返信

kimukazu様


こんにちは。


詳細なご回答、ありがとうございます。


AppleScriptエディタにて動作確認したところ、問題無く文字カウントをすることができました。


しかし、以下のページにあるようにサービスから文字カウントを行おうとAutomatorにて、AppleScriptを再生ボタンを押し、実行したところ、以下のメッセージが出てきてしまいました。


文字数を数えるサービスをAutomator + Apple Script で作る

http://forthesleeplessnight.blogspot.jp/2011/07/automator-applescript.html


構文エラー


end of line などがあるべきところですが、identifier が見つかりました。


となってしまい、文字のカウントができないようです。


また、文字のカウントをJavaScriptでするサイトは複数あり、使用していますが、どれも結果が微妙に違っています。


通常、Macで文章を書く際は、ー文字程度という制約の基で書くことが多いのですが、リアルタイムで計測できるサイト(http://phpjavascriptroom.com/exp.php?f=include/js/formcheck/getInputLength.inc&ttl=入力文字数をリアルタイム表示)は非常に便利ですが、ネット接続されていないと使えないデメリットがあり、実際、深夜などにサーバーのメンテナンスで使えず困った経験があります。


サイトによって文字数の表記が違ったりするのは、日本語が英語とは違う言語で、一文字あたりのバイト数の違いなどにもよると思いますが、文字コード、改行コードの違いなども文字カウントの機能に影響するのでしょうか?


実際、MacからWindowsにファイルをよく転送するので、改行コードの変換ソフトを使っています。


文字数がご回答頂いたscriptと各サイトで異なっているので、再三のお願いにはなりますが、お教え願えればと思います。

2012/10/12 13:28 milestone への返信

文字数を数えるサービスをAutomator + Apple Script で作る

このサイトにあるスクリプト部分を下記に変えてみてください。


on run {input, parameters}

display dialog (count character of (do shell script "echo '" & (input as Unicode text) & "' | perl -pe 's/(\n|\r|\nr| | |)//g'"))

end run




いけると思います。


本ですが、

Amazon.co.jp: AppleScriptリファレンス: こばやし ゆたか,AppleScriptリファレンス制作委員会: 本

初心者向けというわけではないんですが、あると便利です。

基本操作はウエブ上で探した方が早いと思います。

2012/10/12 15:02 uni-factory への返信

弘法も筆の誤りかしら。バックスラッシュが抜けてますよ。


perl -pe 's/(\n|\r|\nr| | |)//g'

perl -pe 's/(\\n|\\r|\\nr| | |)//g'


または

perl -pe 's/(\\s)//g'


#追記: 失礼しました。私が間違ってました。perl -pe 's/(\n|\r|\nr| | |)//g' でもOKですね。


このメッセージは次により編集されています:Hiro.S

2012/10/12 18:13 Hiro__S への返信

面白そうってことで試させていただいているのですが、「'」(シングルクォート)が含まれるとエラーで止まるのと、「s/(\\s)//g」をカウントするとカウントが少なくなる(実際は10だけど9になる)のは回避可能でしょうか?

2012/10/12 18:58 HAL への返信

「'」(シングルクォート)が含まれるとエラーで止まる


quoted form of xxx を使うと良いと思います。


on run {input}

set x to item 1 of input

set y to do shell script "printf '%s' " & quoted form of x & " | perl -CIO -pe 's|\\s||g;'"

set z to count character of y

display dialog z

end run


-----


「s/(\\s)//g」をカウントするとカウントが少なくなる(実際は10だけど9になる)のは回避可能でしょうか?


私が勘違いしてるかもしれないので、具体的な文字列を教えていただけますか?

たしかに \s はより多くのキャラクターにマッチします「全半角スペース+改行+タブ+そのた空白」

2012/10/12 19:23 Hiro__S への返信

「'」(シングルクォート)が含まれるとエラーで止まる

Hiro.Sさんの通りですね。


on run {input, parameters}

display dialog (length of (do shell script "echo " & quoted form of (input as Unicode text) & " | perl -pe 's/(\r|\r|\rr| | |)//g'"))

end run


「s/(\\s)//g」をカウントするとカウントが少なくなる(実際は10だけど9になる)のは回避可能でしょうか?

「s/(\\s)//g」の文字長をはかろうとすると、

「\\」が「\」になって1文字としてカウントされてしまうと言うことですね。

済みませんがちょっとわかりません。



Hiro.Sさん

AppleScriptはそれなりにかけるのですが、shell script はまだまだわからない事だらけです。

今回は間違っていなくて安心しましたが、今後も突っ込んでください。

2012/10/12 19:30 Hiro__S への返信

quoted form of xxx を使うと良いと思います。


ばっちりです。ありがとうございます。


私が勘違いしてるかもしれないので、具体的な文字列を教えていただけますか?


具体的も何もそのままの文字列です (^^;

どうやら // が1とカウントされるみたいです。

これは、実害がなさそうなので無理に回避する必要はないかもしれません (^^;;

2012/10/12 20:32 milestone への返信

コメント、および、新たな課題のご提示、ありがとうございます。

下記の通り、ご指摘の課題に回答させていただきます。

【課題1】文字数カウントのAppleScriptをAutomatorでラッピングした時の構文エラー

原因:display dialog文のwith titleオプションがAutomatorでは機能しないため

対策:下記の通り、AppleScriptを一部修正(エラー箇所を削除)

--

on run {input, parameters}

tell application "TextEdit"

set theText to the text of document 1

set word_count to count words of document 1

set char_count to count characters of document 1

set para_count to count paragraphs of document 1

end tell


set theLF to ASCII character 10

set LF_count to countSubstring(theText, theLF)

set space_count to countSubstring(theText, " ")

set show_words to "文字数:" & (char_count as string) & return & "改行を除いた文字数:" & ((char_count - LF_count) as string) & return & "空白、改行を除いた文字数:" & ((char_count - space_count - LF_count) as string) & return & "行数:" & (para_count as string)

display dialog show_words with icon 1 buttons {"OK"} default button "Ok"

end run


-- e.g. countSubstring("abc abc abc", "abc") --> 3


on countSubstring(theText, theSubstring)

-- ljr (http://applescript.bratis-lover.net/library/string/), modified from Aurelio.net (http://aurelio.net/doc/as4pp.html)

local ASTID, theText, theSubstring, i

set ASTID to AppleScript's text item delimiters

try

set AppleScript's text item delimiters to theSubstring

set i to (count theText's text items) - 1

set AppleScript's text item delimiters to ASTID

return i

on error eMsg number eNum

set AppleScript's text item delimiters to ASTID

error "Can't countSubstring: " & eMsg number eNum

end try

end countSubstring

--

動作環境:

-Automator 2.1.1 (247.1)


【課題2】”入力文字数をリアルタイム表示”サイト機能の非ネット接続時の利用

ご指摘のサイトに掲載されているソースをhtml形式でローカルディスクに保存し、ブラウザで参照することで対応できると思います。


【課題3】文字コード、改行コードの違いなどが、各サイトの文字カウントの結果に影響するか?

ご指摘の通り、各サイトの文字カウントの結果は、そのサイトの文字数カウントの仕様(機能)や入力データ(文字コード、改行コード)に依存すると思います。各サイトの詳細な機能については、そのコード(Javascript等)をご確認ください。

※AppleScriptも同様です。多くのみなさまからコメントいただきましたように、ご希望とされる目的に合わせて適宜コードを修正いただければ幸いです。


【課題4】AppleScriptの参考文献

http://en.wikipedia.org/wiki/AppleScript

http://ja.wikipedia.org/wiki/AppleScript

AppleScriptに関しては、書籍よりも海外サイトの情報のほうが豊富のように思います。

ご期待される回答になっているかわかりませんが、もし、ご不明点等ございましたら、ご指摘いただければ幸いです。


追伸:みなさま

perlの正規表現を用いた代替策に関する貴重なコメント、ありがとうございました。

非常に簡易なコードなので当方も大変参考になりました。

2012/10/12 20:39 HAL への返信

外部コマンドの /bin/echo を使うか、printf コマンドを使うと回避できます。

なるほど。参考になります。


on run {input, parameters}

display dialog (length of (do shell script "printf '%s' " & quoted form of (input as Unicode text) & " | perl -pe 's/(\r|\r|\rr| | |)//g'"))

end run


いけました。有り難うございます。

2012/10/12 21:01 kimukazu への返信

原因:display dialog文のwith titleオプションがAutomatorでは機能しないため


これ私も以前悩みました。


Automator - AppleScript のトップレベルのダイアログは Automator のダイアログがでるんですよね。アラートダイアログも同様です。回避方法はお書きのとおりにするか、


-- A案

tell application "TextEdit"

display dialog "xxx" with title "タイトル"

end tell


-- B案 - ちょっとトリッキー (Finder ではなく、System Events でも OK)

using terms from application "Finder"

display dialog "xxx" with title "タイトル"

end using terms from


また、Automator のダイアログはタイムアウトしないので注意が必要です。


-----


あと、milestone さんのご希望は「選択したテキストの文字数」で kimukazu さんご提示のものは「ドキュメント全体」なので、ちょっと違うような気がしますが...。

Apple Scriptによる選択範囲の文字カウント(改行、スペース抜き)について

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple ID を使ってご参加ください。