###アップルスクリプト上での文字置き換え
テキストファイルから読み込んだ文字を『全角英数字→半角英数字』『半角カタカナ→全角カタカナ』に
置き換えたいのですが、うまく変換することができません。
現在、『半角カタカナ→全角カタカナ』の部分は除外しておりますが、
下記URLを参考にやってみたものの、こちらもうまく動かす事ができませんでした。
http://www.serendip.ws/archives/2185
なにぶん、まだ右も左もわかららない初心者ですので、質問もつたないかとおもいますが、
何卒よろしくお願いいたします。
###発生している問題・エラーメッセージ
はきだされるテキストが空白(なにもない状態)になる。
アップルスクリプト上で 返された値 $string = jcode($string)->tr('A-Za-z0-9@−','A-Za-z0-9@-'); print '$string';" --> ""
###該当のソースコード
(* **テキスト整形(基本) **ドロップレット形式にて、テキストファイルを読み込み基本的な文字の置き換えをする。 **書き出されるファイルは上書きする。 **●処理事項 **・半角カナ→全角カナ **・全角英数字→半角英数字 **・半角カッコ→全角カッコ ==>完了 **・空行のスペース削除(改行のみにする) ==>完了 **・複数スペースをひとつのスペースに ==>完了 **・行はじめからのスペース削除 ==>完了 **・行末タブ削除 ==>完了 *) property extension_list : {"txt"} --拡張子のリスト property selectFilePath : {} property text_contents : "" --読み込んだテキスト set this_item to "テキストファイルが入ったフォルダまでのパス" my process_folder(this_item, doPerlKit) on process_folder(myFol, doPerlKit) --フォルダの中身のファイル名と拡張子を取得する set inner_items to list folder myFol without invisibles --対象アイテムのファイルパスを組み立てる repeat with j from 1 to the count of inner_items --対象フォルダの中身。 set inner_item to alias ((myFol as Unicode text) & (item j of inner_items)) --"inner_item"の情報を"item_info"に格納 set the item_info to info for inner_item --もし"item j of inner_item"がフォルダならば if folder of the item_info is true then my process_folder(inner_item, doPerlKit) --フォルダがあればフォルダの処理を再帰 else my process_file(inner_item, doPerlKit) --ファイルがあればファイルの処理へ end if end repeat end process_folder --ドロップされたファイルの処理 on process_file(myFile, doPerlKit) --処理対象のファイルが、拡張子".txt"なのか確認する。 buildNameList(myFile) tell application "Finder" tell current application --拡張子確認が終わった対象ファイルをシェルスクリプトで使用できるファイルパス形式に変換 set myFilePath to quoted form of the POSIX path of selectFilePath log myFilePath --テキストファイルを読み込む。シェルスクリプトで処理する。 set text_contents to do shell script "cat < " & myFilePath --テキストを変換する --perlで処理する set aStr to text_contents --ここが問題 --全角英数字→半角英数字 set aStr to do shell script "#!/usr/bin/perl my $string = '" & aStr & "'; use Encode; my $decoded = Encode::decode('utf-8', $string); my $encoded = Encode::encode('utf-8', $decoded); use Jcode; $string = jcode($string)->tr('A-Za-z0-9@−','A-Za-z0-9@-'); print '$string';" do shell script "echo " & aStr & " > " & myFilePath do shell script "cat " & myFilePath end tell end tell end process_file --フォルダ内のファイル名リストを作り、拡張子の確認をおこなう。 to buildNameList(myFilePath) --"thisItem"にユニコードに変換したファイルパスを格納 set thisItem to myFilePath as Unicode text --"delimiters"は文字。OS自体が持つ値でスクリプトが終了してもそのまま。元に戻す必要があるので、"tmp"に代入しておく。 set tmp to AppleScript's text item delimiters --"."を区切り文字とする。 set AppleScript's text item delimiters to "." --{ファイルパス, 拡張子}…となっている。"text item"は"delimiters"で区切られた文字列単位。"every"は含まれる全てのもの。 set currentItem to every text item of thisItem --拡張子を調べる。 set currentItemExtention to (item -1 in currentItem) as string --"AppleScript's text item delimiters"を元に戻す。 set AppleScript's text item delimiters to tmp --テキストファイルなら。"extension_list"は上で設定した拡張子。 if extension_list contains currentItemExtention then --変換対象のファイル。 set selectFilePath to myFilePath end if end buildNameList --————————————— --AppleScript内でPerl正規表現を使用するためのルーチン群 --————————————— script doPerlKit --★targetがregexにマッチするか判定 on regex_match(regex, target) set command to "$target = q/" & target & "/; if ($target =~ " & regex & "){ print q/1/; }else{ print q/0/; }" --※引数を使用してperlコマンド生成 --log {"command=", command} (*command=, $target = q/xxxx@ezweb.ne.jp/; if ($target =~ /ezweb/){ print q/1/; }else{ print q/0/; }*) set str_result to exec_perl(command) of me --log {"str_result =", str_result} (*str_result =, 1*) return evalResult(str_result) of me end regex_match --★targetをregexで置換 on regex_replace(regex, target) set command to "$target = q/" & target & "/; $target =~ s" & regex & "; print $target;" --※引数を使用してperlコマンド生成 set str_result to exec_perl(command) of me --return evalResult(str_result) of me return str_result end regex_replace --失敗 on hankaku2zenkaku(regex, target) set command to "use utf8; binmode STDOUT, ':utf8'; $target = q/" & target & "/; $target =~ s" & regex & "; print $target;" --※引数を使用してperlコマンド生成 set str_result to exec_perl(command) of me return str_result end hankaku2zenkaku --★commandをperlで実行 on exec_perl(command) set one_liner to "perl -e '" & command & "'" as Unicode text --log {"one_liner =", one_liner} (*one_liner =, perl -e '$target = q/xxxx@ezweb.ne.jp/; $target =~ s/e/A/g; print $target;'*) --エラー処理 try set str_result to do shell script one_liner --※shell実行 --log {"str_result =", str_result} on error set str_result to "syntax error : " & one_liner --perlがエラーを吐いたとき end try return str_result end exec_perl --★結果がtrueかfalseかerrorか判断 on evalResult(str_result) if str_result is "1" then return true else if str_result begins with "syntax error : " then return str_result else return false end if end evalResult end script

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。