今更ながら JavaScript で半角 → 全角

まあ、半角・全角という表現がアレなのはおいておいてください。

最近、郵送にクリックポストという手段を使うのですが、ヤマトのメール便に似た感じのサービスで、A4 サイズ、3cm までの厚みのものが安価に送れます。

ウェブで送り先などを入力して、ラベルを作成するんですが、ある程度の数になると、それが結構面倒臭いんですよね。

需要があるのか、さっぱりわかりませんが、重松は住所録ソフトは筆まめを使っているんですが、その筆まめの住所データーをいちいち Windows と Mac とを切り替えながらコピペするのも面倒くさいので、自動化しました。

その時、住所は半角があってはいけないので、変換する必要があるんですが、以前は、Script Managaer を使って変換していたんですけど、もう OSAX がどっかに行ってしまっているし、JavaScript でやることにしました。

以下のページを参考にしました。

tell application "Finder"
    set myText to the clipboard
end tell

tell application "Safari"
    set myClickPostDocument to 0
    
    set myNumOfDocuments to count every document
    repeat with myDocumentNum from 1 to myNumOfDocuments
        set theDocumentTitle to do JavaScript "document.title" in document myDocumentNum
        if theDocumentTitle contains "新規発送" then
            set myClickPostDocument to myDocumentNum
            exit repeat
        end if
    end repeat
    
    if myClickPostDocument is 0 then
        say "click post document was not found"
        return
    end if
    
    tell application "Finder" to set myClip to the clipboard
    set buf to AppleScript's text item delimiters
    set AppleScript's text item delimiters to " "
    set myData to every text item of myClip
    set AppleScript's text item delimiters to buf
        
    set myName to item 4 of myData
    set myZip to item 67 of myData
    set myAddr to item 68 of myData
    if 0 < (length of (item 69 of myData)) then set myAddr to myAddr & item 69 of myData
    
    tell document myClickPostDocument
        do JavaScript "
document.querySelector('div.edit_panel input[name$=id39]').value='おふだ';
document.querySelector('div.edit_panel input[name$=no_missive]').checked=true;
document.querySelector('div.edit_panel input[name$=checked_pack_size]').checked=true;

myZip = '" & myZip & "';

document.querySelector('div.edit_panel input[name$=zip1]').value = myZip.substr(0,3);
document.querySelector('div.edit_panel input[name$=zip2]').value = myZip.substr(4,4);

myAddr = '" & myAddr & "'.replace(/[!-~]/g, function(s) {
    return String.fromCharCode(s.charCodeAt(0) + 0xFEE0);
});
myAddr = myAddr.replace(/^愛媛県/,'');
document.querySelector('div.edit_panel textarea[name$=receiver_address]').value = myAddr;

myName = '" & myName & "';
document.querySelector('div.edit_panel input[name$=receiver_name]').value = myName;
"
    end tell
end tell

にほんブログ村 ライフスタイルブログ 薪ストーブ暮らしへ
にほんブログ村 ライフスタイルブログ 薪ストーブ暮らしに参加しています。 励みになりますので、足あとがわりに、ランクアップにご協力下さい。

Leave a Reply