AppleScriptでプチ・プログラミング

年賀状を書く季節になってまして、仕事の関係で送る年賀状の数も増えつつあり、宛名の手書きもちょっときつくなってきたのでどんな方法で宛名の印刷をやるのがいいかと考えてました。

年賀状作成用のソフトなんて買うつもり無いので、既存のそして無料のリソースで。
Googleドキュメントなど使ってできそうだけど文字フォントを変えられないというのを知りNG。
ならMacに入ってるPagesを使おうと思うが、差し込み印刷ができないというのが分かった。

MacにはAppleScriptというアプリを操作することができるプログラミング機能が備わっているので、このAppleScriptでMacアプリの「連絡先」に入っているデータを元にPagesに流し込むことができるはずだと考え試行錯誤。
一応できました。

まず、Pagesで印刷したいレイアウトおよびデザインのハガキを作り、テンプレートで保存します。
そのテンプレートを開き、特定のキーワードを箇所を連絡先に登録してある情報で置き換える。
その置き換えたハガキを宛先の氏名でファイル名にして保存。
という手順のAppleScriptです。

テンプレートに入れている文字列は、「1234567」「住所」「氏」「名」「組織」「肩書き」でそれぞれの箇所を書き換えます。

連絡帳で「印刷グループ」に分類された人に対して処理を実行します。


tell application "Contacts"
 --activate
 
 set thePeople to every person
 
 repeat with thisperson in thePeople
  
  (*get the entries group list *)
  set inGroup to name of group of thisperson
  
  if inGroup contains "印刷グループ" then
   set PersonFName to first name of thisperson
   set PersonLName to last name of thisperson
   set PersonTitle to job title of thisperson
   if PersonTitle = false then set PersonTitle to ""
   set PersonCompany to organization of thisperson
   if PersonCompany = false then set PersonCompany to ""
   set PersonZip to zip of address of thisperson as string
   set NewZip to replaceTextFunc(PersonZip, "-", "") of me
   set PersonState to state of address of thisperson
   set PersonCity to city of address of thisperson
   set PersonStreet to street of address of thisperson
   set PersonAddress to (PersonState & return & PersonCity & PersonStreet) as string
   
   display dialog PersonLName & PersonFName & "(" & PersonCompany & " " & PersonTitle & ")" & "〒" & NewZip & PersonAddress
   
   tell application "Pages"
    activate
    set thisDocument to make new document with properties {document template:template "はがき宛名"}
    my replaceText("1234567", NewZip)
    my replaceText("住所", PersonAddress)
    my replaceText("氏", PersonLName)
    my replaceText("名", PersonFName)
    my replaceText("組織", PersonCompany)
    my replaceText("肩書き", PersonTitle)
    
    set theFileName to (PersonLName & PersonFName)
    
    set desktopFolder to path to desktop as text
    save thisDocument in file (desktopFolder & theFileName & ".pages")
    --close thisDocument
    
   end tell
   
   display dialog "続行する?"
  end if
  
 end repeat
end tell

on replaceText(searchWord, replacementString)
 tell application "Pages"
  activate
  tell the front document
   tell body text
    repeat with i from the (count of paragraphs) to 1 by -1
     set rsltText to replaceTextFunc(paragraph i, searchWord, replacementString) of me
     set paragraph i to rsltText
    end repeat
   end tell
   return true
  end tell
 end tell
end replaceText

on replaceTextFunc(theText, serchStr, replaceStr)
 set tmp to AppleScript's text item delimiters
 set AppleScript's text item delimiters to serchStr
 set theList to every text item of theText
 set AppleScript's text item delimiters to replaceStr
 set theText to theList as string
 set AppleScript's text item delimiters to tmp
 return theText
end replaceTextFunc
※Mac OSXのバージョン、Pagesのバージョン、そして連絡先のバージョンによってはこのまま動かない可能性があるとは思います。

コメント