yyyy-MM-dd HH:mm:ss形式の日付テキストをdateオブジェクトに変換して日付を年単位で集計するAppleScriptです。
Blog Archiveのデータベースのダンプテキストを読み取って、年別の投稿記事本数の集計を行うために作成しました(といっても、既存のルーチンを組み合わせただけです)。
AppleScript名:日付テキストを年単位で集計 |
— Created 2018-06-28 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSArray : a reference to current application’s NSArray property NSString : a reference to current application’s NSString property NSLocale : a reference to current application’s NSLocale property NSDictionary : a reference to current application’s NSDictionary property NSCountedSet : a reference to current application’s NSCountedSet property NSMutableArray : a reference to current application’s NSMutableArray property NSSortDescriptor : a reference to current application’s NSSortDescriptor property NSDateFormatter : a reference to current application’s NSDateFormatter set aList to {"2006-06-27 02:38:52", "2008-03-09 19:08:05", "2008-03-09 19:29:29"} set aRes to totalDateStrByYear(aList, "yyyy-MM-dd HH:mm:ss") of me –> {{aCount:1, aKey:2006}, {aCount:2, aKey:2008}} –日付テキストを年単位で集計 on totalDateStrByYear(aList, strFormat) set theCountedSet to NSCountedSet’s |set|() set newArray to NSMutableArray’s new() repeat with i in aList set postDate to dateFromStringWithDateFormat(i, strFormat) of me –日付テキストをDate Objectに変換 set yearStr to (year of postDate) –「年」の数字 (newArray’s addObject:yearStr) end repeat set resArray to countItemsByItsAppearance(newArray) of me set bList to sortRecListByLabel(resArray, "aKey", true) of me –昇順ソート return bList end totalDateStrByYear –日付文字列からdate objectを作成する on dateFromStringWithDateFormat(dateString, dateFormat) set dStr to NSString’s stringWithString:dateString set dateFormatStr to NSString’s stringWithString:dateFormat set aDateFormatter to NSDateFormatter’s alloc()’s init() aDateFormatter’s setDateFormat:dateFormatStr aDateFormatter’s setLocale:(NSLocale’s alloc()’s initWithLocaleIdentifier:"en_US_POSIX") set aDestDate to (aDateFormatter’s dateFromString:dStr) return aDestDate as list of string or string end dateFromStringWithDateFormat –登場頻度でリストを集計する on countItemsByItsAppearance(aList) set aSet to NSCountedSet’s alloc()’s initWithArray:aList set bArray to NSMutableArray’s array() set theEnumerator to aSet’s objectEnumerator() repeat set aValue to theEnumerator’s nextObject() if aValue is missing value then exit repeat bArray’s addObject:(NSDictionary’s dictionaryWithObjects:{aValue, (aSet’s countForObject:aValue)} forKeys:{"aKey", "aCount"}) end repeat return bArray end countItemsByItsAppearance –リストに入れたレコードを、指定の属性ラベルの値でソート on sortRecListByLabel(aRecList as list, aLabelStr as string, ascendF as boolean) set aArray to NSArray’s arrayWithArray:aRecList set sortDesc to NSSortDescriptor’s alloc()’s initWithKey:aLabelStr ascending:ascendF set sortDescArray to NSArray’s arrayWithObjects:sortDesc set sortedArray to aArray’s sortedArrayUsingDescriptors:sortDescArray set bList to (sortedArray) as list of string or string return bList end sortRecListByLabel |
More from my site
(Visited 59 times, 1 visits today)