08/10 特定月のDiary++の業務日誌の内容をExcel用Tab区切りテキストとして書き出す
特定年・月のDiary++上の業務日誌の内容をExcelに渡すためのTab区切りテキストとして書き出すAppleScriptです。

日頃から、このようにして業務内容を記録しているものですが、大人の事情で結局1か月に一度、Excelの決められたシートに転載しておく必要があります。
これをすべて手作業で行っていたのですが……どうせ自分が記録するものなので、記入のゆらぎはほとんどないはず(過信)。そこで、プログラムで処理するようにしてみました。

あんまり参考にならない内容かもしれませんが、Diary++ Xを実際に活用しているAppleScriptのサンプルとしては、(こんな風に使っているよ、という実例として)有用ではないでしょうか。
| スクリプト名:特定月のDiary++の業務日誌の内容をExcel用Tab区切りテキストとして書き出す |
| set targYear to 2010 set targMonth to 7 set outFile to choose file name with prompt “書き出し先ファイル名を入力” set targYearStr to (targYear as string) & “年” set targMonthStr to targYearStr & (targMonth as string) & “月” tell application “Diary++X” tell document 1 tell category “日記” tell category targYearStr –2010年 tell category targMonthStr –2010年7月 set aList to every article end tell end tell end tell end tell –業務日報のarticleのみ抽出(本来はタグなどで抽出すべき) set bList to {} repeat with i in aList set aCon to text contents of i if aCon contains “職場関連:” then set the end of bList to contents of i end if end repeat –日付とアーティクル本文のセットのリストを作成する set newList to {} repeat with i in bList set aDate to diary date of i set aText to text contents of i set the end of newList to {aDate, aText} end repeat set n2List to shellSortListAscending(newList, 1) of me n2List end tell –処理対象の月の日数を取得 set dLen to getMlen(targYear, targMonth) of me –処理対象月の各日付でループし、業務日報の該当アーティクルが存在する場合には、各タスクの情報を取り出してファイル出力 –該当アーティクルが存在しない場合には日付のみのカラ行をファイル出力 するメインループ repeat with i from 1 to dLen set tDate to (targYear as string) & “/” & (targMonth as string) & “/” & (i as string) set aDate to date tDate –アーティクルのデータを日付で検索 set findF to false repeat with ii in n2List if item 1 of ii = aDate then set findF to true exit repeat end if end repeat –処理対象日のテキスト(○月○日) set thisDateStr to (targMonth as string) & “月” & (i as string) & “日” if findF = false then set blankDate to thisDateStr & return write_to_file(blankDate, outFile, true) of me else –1日分のコンテンツ処理 set dList to retStartWithBlankAndEndsWithBlank(contents of item 2 of ii) of me –空白行で囲まれたブロックを日報本体としてピックアップ set dList to listSweep(dList) of me –カラの行(リターンのみ)を除去 set firstF to true –1行目フラグ(1行目のみ日付データを出力)をtrueに repeat with iii in dList set jjj to contents of iii set {timeStr, targetStr, actionStr} to parseByDelim(jjj, tab) of me set {sTime, eTime} to parseByDelim(timeStr, “〜”) of me set diffNum to getTimeDiffNum(sTime, eTime) of me if firstF = true then –1日の日付の予定の中の1行目 set outData to thisDateStr & tab & sTime & tab & eTime & tab & (diffNum as string) & tab & tab & tab & tab & targetStr & tab & actionStr & return set firstF to false else –2行目以降 set outData to tab & sTime & tab & eTime & tab & (diffNum as string) & tab & tab & tab & tab & targetStr & tab & actionStr & return end if write_to_file(outData, outFile, true) of me end repeat end if end repeat –リスト項目のうち、空白であった場合にはリストから除去する on listSweep(aList) set bList to {} set aLen to length of (item 1 of aList) repeat with i in aList if (contents of i is not equal to “”) then set the end of bList to contents of i end if end repeat return bList end listSweep –改行のみで囲まれた行をリストで取り出す on retStartWithBlankAndEndsWithBlank(aStr) set mList to paragraphs of aStr set wList to {} set trimF to false set tStart to 0 set tEnd to 0 set tCounter to 1 repeat with i in mList set j to contents of i set jLen to length of j if trimF = false then if jLen = 0 then set tStart to tCounter set trimF to true end if else if jLen = 0 then set tEnd to tCounter exit repeat end if end if set tCounter to tCounter + 1 end repeat set rData to items tStart thru tEnd of mList return rData end retStartWithBlankAndEndsWithBlank –指定デリミタで文字列をparseする on parseByDelim(aData, aDelim) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set dList to text items of aData set AppleScript’s text item delimiters to curDelim return dList end parseByDelim –数値にゼロパディングしたテキストを返す on retZeroPaddingText(aNum, aLen) set tText to (“0000000000″ & aNum as text) set tCount to length of tText set resText to text (tCount - aLen + 1) thru tCount of tText return resText end retZeroPaddingText –シェルソートで入れ子のリストを昇順ソート on shellSortListAscending(a, keyItem) set n to length of a set cols to {1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1} repeat with h in cols if (h ≤ (n - 1)) then repeat with i from h to (n - 1) set v to item (i + 1) of a set j to i repeat while (j ≥ h) and ((contents of item keyItem of item (j - h + 1) of a) > (item keyItem of v)) set (item (j + 1) of a) to (item (j - h + 1) of a) set j to j - h end repeat set item (j + 1) of a to v end repeat end if end repeat return a end shellSortListAscending –指定月の日数を返す on getMlen(aYear, aMonth) set aYear to aYear as number set aMonth to aMonth as number set aDat to (aYear as text) & “/” & (aMonth as text) & “/1″ if aMonth is 12 then set eDat to ((aYear + 1) as text) & “/” & (1 as text) & “/1″ else set eDat to ((aYear as text) & “/” & (aMonth + 1) as text) & “/1″ end if set eDat to date eDat set eDat to eDat - 1 set mLen to day of eDat return mLen end getMlen –ファイルの追記ルーチン「write_to_file」 –追記データ、追記対象ファイル、boolean(trueで追記) on write_to_file(this_data, target_file, append_data) try set the target_file to the target_file as text set the open_target_file to open for access file target_file with write permission if append_data is false then set eof of the open_target_file to 0 write this_data to the open_target_file starting at eof close access the open_target_file return true on error error_message try close access file target_file end try return error_message end try end write_to_file –文字列で与えられた時刻(時:分)の「差」を数値で返す(30分=0.5、15分=0.25) on getTimeDiffNum(startTImestr, endTimeStr) set {sH, sM} to parseByDelim(startTImestr, “:”) of me set sDate to current date set hours of sDate to (sH as number) set minutes of sDate to (sM as number) set seconds of sDate to 0 set {eH, eM} to parseByDelim(endTimeStr, “:”) of me set eDate to current date set hours of eDate to (eH as number) set minutes of eDate to (eM as number) set seconds of eDate to 0 set d to eDate - sDate set h to d div 3600 set m to d mod 3600 set m2 to m / 3600 set m3 to h + m2 return m3 end getTimeDiffNum |



