H.M.F.B AppleScript.



久々ですが、、今回は限定されてしましますが有名なメーラEudoraのAppleScriptを紹介したいと思います。
殆どのメーラには「アドレス帳」といって他人の個人情報を記入する所があると思うのですが、アドレス帳を持つアプリケーションは沢山ある為データを手作業で統一させるのはちと辛い物がありますね。 そこをAppleScriptでどうにかしよう、、ということで今回のやりダマはEudoraのアドレス帳です、。



さて、今回あつかうアドレス帳は↓です、
左側は「nickname files」と「nickname」を、右側は「nicknameの各要素」をあらわしています。
要素の名前などはそのフィールド内に記述してある通りです。


そして上記をスクリプトで現したのが下となります。
nickname files は、上記の例では「テストフォルダ」が「nickname file 2」となり、nickname は「nickname 0」となっています。

選択している所をスクリプトで現すと nickname 0 of nickname file 2 もしくは、nickname "nickname"となっています。

上から順に1ではなく、0から始まっているんですね。

000
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024






tell application "Eudora"
  
  --アドレス帳のナンバー("0"が一番上)もしくは名前
  set Add_HN to 2
  --ニックネームナンバー("0"が一番上)
  set Nick_Num to 0
  --ニックネーム
  set Nick_HN to nickname of nickname Nick_Num of nickname file Add_HN
  
  nickname of nickname Nick_HN --ニックネーム
  recipient of nickname Nick_HN --受取人リスト
  raw notes of nickname Nick_HN --全て "<name><address><phone><fax>備考" の形

  field "name" of nickname Nick_HN --名前  
  field "address" of nickname Nick_HN --住所
  field "phone" of nickname Nick_HN --電話番号
  field "fax" of nickname Nick_HN -- ファックス

  addresses of nickname Nick_HN --アドレス
  notes of nickname Nick_HN --備考
  
  --expansion of nickname Nick_HN --展開 "ニックネーム"<アドレス> の形
  
end tell


このスクリプトは全ての欄を埋めてある場合は良いのですが、field "〜〜" で指定した所が空の場合はどうやら文字化けしてしまっているようですね、、困りました。
実はこのfield "〜〜" は、raw notes でも内容が求められるようで、、
field "name" / "address" / "phone" / "fax" の部分はraw notes で返ってきた値のみを求めた方がよさそうですね。

さて、rawnote をどうやって処理して行きましょうか?
この中の備考(notes)の部分はそのまま処理出来ますので無視して良さそうですので考えないでよさそうですねそれをふまえて処理していきましょう、、。




rawnoteの返値は全てが埋まっている場合は"<name><address><phone><fax>備考"といった形で記入されていない所は返ってきません、つまりname、phone、fax、備考は記述が有るが、addressに記述がない場合の返り値は"<name><phone><fax>備考"という形です。
この記述されているname、phone、faxならばfield "name"などで読み込んでんでよさそうです。

要するにrawnoteに、何が含まれていて、何が含まれていないのか?を判定してからfieldでrawnoteの各項目を読み込みましょう、、。
ということでその処理が下になります。

000
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041






tell application "Eudora"
  --アドレス帳のナンバー("0"が一番上)もしくは名前
  set Add_HN to 2
  --ニックネームナンバー("0"が一番上)
  set Nick_Num to 0
  --ニックネーム
  set Nick_HN to nickname of nickname Nick_Num of nickname file Add_HN
  
  set Add_Nickname to nickname of nickname Nick_HN --ニックネーム
  set Add_Recipient to recipient of nickname Nick_HN --受取人リスト
  set Add_Addresses to addresses of nickname Nick_HN --メールアドレス
  set Add_notes to notes of nickname Nick_HN --備考
  
  set Add_Raw to raw notes of nickname Nick_HN --"<name><address><phone><fax>備考"
  
  if Add_Raw contains "name" then
    set Add_Name to field "name" of nickname Nick_HN --名前
  else
    set Add_Name to ""
  end if
  
  if Add_Raw contains "address" then
    set Add_Address to field "address" of nickname Nick_HN --住所
  else
    set Add_Address to ""
  end if
  
  if Add_Raw contains "phone" then
    set Add_Phone to field "phone" of nickname Nick_HN --電話
  else
    set Add_Phone to ""
  end if
  
  if Add_Raw contains "fax" then
    set Add_Fax to field "fax" of nickname Nick_HN --ファックス
  else
    set Add_Fax to ""
  end if

end tell


はい、、演算子が出てきました、、その名も「contains」くんです。
実に簡単な処理ですが、見た目が長いです、、笑。

このcontainsの拡張子は僕は覚えてはいませんでしたが、「何かが含まれているか?」という演算子が有る事だけを覚えていたので過去にこのサイトでも紹介したリストから一番良いと思われる演算子を探して使用しました、 (演算子も沢山あるので必要になった時に探せばOKでしょう、、良く使うのは覚えてられるでしょうしね、)

もうちょっと手を加える所があるんだけどわかりますか?
014行目のraw notesで求められる所全てが空の場合は空白を返してきますのでその場合は16行目から38行目全ての処理が無駄になりますし、elseも簡略化させる事が可能です



000
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041






tell application "Eudora"
  --アドレス帳のナンバー("0"が一番上)もしくは名前
  set Add_HN to 2
  --ニックネームナンバー("0"が一番上)
  set Nick_Num to 0
  --ニックネーム
  set Nick_HN to nickname of nickname Nick_Num of nickname file Add_HN
  
  set Add_Nickname to nickname of nickname Nick_HN --ニックネーム
  set Add_Recipient to recipient of nickname Nick_HN --受取人リスト
  set Add_Addresses to addresses of nickname Nick_HN --メールアドレス
  set Add_notes to notes of nickname Nick_HN --備考
  
  
  --エラーにならない為に先に変数に空を入れておく。
  set Add_Name to "" --名前
  set Add_Address to "" --住所
  set Add_Phone to "" --電話
  set Add_Fax to "" --ファックス
  
  set Add_Raw to raw notes of nickname Nick_HN -- "<name><address><phone><fax>備考"
  
  if Add_Raw /= "" then --Add_Rawが""でなければ
    if Add_Raw contains "name" then
      set Add_Name to field "name" of nickname Nick_HN --名前
    end if
    
    if Add_Raw contains "address" then
      set Add_Address to field "address" of nickname Nick_HN --住所
    end if
    
    if Add_Raw contains "phone" then
      set Add_Phone to field "phone" of nickname Nick_HN --電話
    end if
    
    if Add_Raw contains "fax" then
      set Add_Fax to field "fax" of nickname Nick_HN --ファックス
    end if
  end if
end tell

ってな感じでスクリプト全体もスマートになったようです。
024行目の「/=」演算子はコンパイルするとちっちゃな「ユ」になりますが、これは日本語フォントで文字化けしているだけで英語フォントの/=の記号になっていますので御安心を、、。

ここで注意したいのは、017〜020行目の変数への代入です、構文内で使われていない変数を呼出すとエラーになってしまう、また空を変数に代入しておけば、025行から始まるif文でelseが発生した時の為にも無駄な代入をしないで済み一石二鳥な代入ですね、、。




一様今回はEudoraのアドレス帳の読み込みを全部書けたと思いますので(というか今回ちょっと長く書いてしまったので、)続きのファイルメーカーに読み込みは次回に回したいと思いますのでよろしくお願いします。

また他のメーラもやってきたいですね、、OutLookとか、、汗。 でも、自分が使ってないのでやる気があんまないです、、見た感じではEudoraのスクリプトより簡単だと思いますので楽勝かな?挑戦してみてくださいね、、さて、さて、、ではでは、JinJinでした、、またねー♪


(c)HandyMacForBeginner's