AppleScript名:文字種別にカウントする v2.1(JeditΩ版) |
— Created 2018-1-11 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" –http://piyocast.com/as/archives/5111 property NSString : a reference to current application’s NSString property NSNumber : a reference to current application’s NSNumber 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 NSNumberFormatter : a reference to current application’s NSNumberFormatter property NSRegularExpressionSearch : a reference to current application’s NSRegularExpressionSearch property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp set aRes to getEditorText() of me if aRes = false then return set myPath to retFrontDocsPath() of me if myPath = missing value then return set aRec to detectCharKindRatio(aRes) of me set bRes to retFormattedStr(aRec) of me makeNewDocument of me given parameter:(myPath & return & return & bRes) on detectCharKindRatio(aStr as string) set theCountedSet to NSCountedSet’s alloc()’s initWithArray:(characters of aStr) set theEnumerator to theCountedSet’s objectEnumerator() set cCount to 0 set hCount to 0 set kCount to 0 set oCount to 0 set lfCount to 0 set scCount to 0 set anCount to 0 set totalC to length of aStr set otherList to {} repeat set aValue to theEnumerator’s nextObject() if aValue is missing value then exit repeat set aStr to aValue as string set tmpCount to (theCountedSet’s countForObject:aValue) set s1Res to chkKanji(aStr) of me set s2Res to chkKatakana(aStr) of me set s3Res to chkHiragana(aStr) of me set s4Res to chkLineFeed(aStr) of me set s5Res to chkSpecialSign(aStr) of me set s6Res to chkAlphaNumeric(aStr) if s1Res = true then set cCount to cCount + tmpCount else if s2Res = true then set kCount to kCount + tmpCount else if s3Res = true then set hCount to hCount + tmpCount else if s4Res = true then set lfCount to lfCount + tmpCount else if s5Res = true then set scCount to scCount + tmpCount else if s6Res = true then set anCount to anCount + tmpCount else set oCount to oCount + tmpCount set the end of otherList to aStr end if end repeat set ckRes to roundingUp((cCount / totalC) * 100, 1) of me set kkRes to roundingUp((kCount / totalC) * 100, 1) of me set hgRes to roundingUp((hCount / totalC) * 100, 1) of me set otRes to roundingUp((oCount / totalC) * 100, 1) of me set lfRes to roundingUp((lfCount / totalC) * 100, 1) of me set scRes to roundingUp((scCount / totalC) * 100, 1) of me set anRes to roundingUp((anCount / totalC) * 100, 1) of me return {kanjiNum:cCount, kanjiRatio:ckRes, hiraganaNum:hCount, hiraganaRatio:hgRes, katakanaNum:kCount, katakanaRatio:kkRes, otherNum:oCount, otherRatio:otRes, otherContent:otherList, lineFeedCount:lfCount, lineFeedRatio:lfRes, specialSignNum:scCount, specialSignRatio:scRes, alphaNumericNum:anCount, alphaNumericRatio:anRes, totalCount:totalC} end detectCharKindRatio on chkKanji(aChar) return detectCharKind(aChar, "[一-龠]") of me end chkKanji on chkHiragana(aChar) return detectCharKind(aChar, "[ぁ-ん]") of me end chkHiragana on chkKatakana(aChar) return detectCharKind(aChar, "[ァ-ヶ]") of me end chkKatakana on chkLineFeed(aChar) return aChar is in {string id 10, string id 13, string id 13 & string id 10} end chkLineFeed on chkSpecialSign(aChar) return aChar is in {"「", "」", "『", "』", "ー", "―", "〜", "~", "!", "?", "&", "/", "《", "》", "#", "…", "・", "♪", "。", "、", ".", "々", "“", "”", "*", "(", ")", "(", ")", " ", " ", "§", "【", "】", "■", "%", "≒"} end chkSpecialSign on chkAlphaNumeric(aChar) return detectCharKind(aChar, "[a-zA-Z0-9a-zA-Z0-9]") of me –半角全角英数字 end chkAlphaNumeric on detectCharKind(aChar, aPattern) set aChar to NSString’s stringWithString:aChar set searchStr to NSString’s stringWithString:aPattern set matchRes to aChar’s rangeOfString:searchStr options:(NSRegularExpressionSearch) if matchRes’s location() = (current application’s NSNotFound) or (matchRes’s location() as number) > 9.99999999E+8 then return false else return true end if end detectCharKind on roundingUp(aNum, aDigit as integer) set a to aNum as real set aFormatter to NSNumberFormatter’s alloc()’s init() aFormatter’s setMaximumFractionDigits:aDigit aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp) set aStr to aFormatter’s stringFromNumber:(NSNumber’s numberWithFloat:a) return (aStr as text) as real end roundingUp on getEditorText() tell application id "jp.co.artman21.JeditOmega" if (count every document) = 0 then return false tell front document return text of it end tell end tell end getEditorText –処理結果から出力データの組み立て on retFormattedStr(aRec) set a to "漢字 :" & (repChar("■", ((kanjiRatio of aRec) div 10)) of me & " " & (kanjiRatio of aRec) as string) & "%" & return set b to "ひらがな:" & (repChar("■", ((hiraganaRatio of aRec) div 10)) of me & " " & (hiraganaRatio of aRec) as string) & "%" & return set c to "カタカナ:" & (repChar("■", ((katakanaRatio of aRec) div 10)) of me & " " & (katakanaRatio of aRec) as string) & "%" & return set d to "改行文字:" & (repChar("■", ((lineFeedRatio of aRec) div 10)) of me & " " & (lineFeedRatio of aRec) as string) & "%" & return set e to "特殊記号:" & (repChar("■", ((specialSignRatio of aRec) div 10)) of me & " " & (specialSignRatio of aRec) as string) & "%" & return set f to "英数字 :" & (repChar("■", ((alphaNumericRatio of aRec) div 10)) of me & " " & (alphaNumericRatio of aRec) as string) & "%" & return & return set g to ("総文字数:" & encodeJapaneseNumText(totalCount of aRec) & " 文字。 400字詰め原稿用紙で約 " & ((totalCount of aRec) div 400) as string) & " 枚分" & return set all to a & b & c & d & e & f & g return all end retFormattedStr on repChar(aChar, aTimes) set outStr to "" repeat aTimes times set outStr to outStr & aChar end repeat return outStr end repChar –数字文字列を日本語数値表現文字列に変換 on encodeJapaneseNumText(aText as string) set dotText to "." as string set upperDigit to "" set lowerDigit to "" –小数点の処理 if dotText is in aText then set b to offset of dotText in aText set upperDigit to characters 1 thru (b – 1) of aText set upperDigit to upperDigit as string set lowerDigit to characters b thru -1 of aText set lowerDigit to lowerDigit as string else set upperDigit to aText end if set scaleList3 to {"", "万", "億", "兆", "京", "垓", "丈", "壌", "溝", "砂", "正", "載", "極", "恒河沙", "阿僧梢", "那由他", "不可思議", "無量大数"} set splitDigit to 4 set nList to splitByDigit(upperDigit, splitDigit) of me set nList to reverse of nList set resText to "" set digCount to 1 repeat with i in nList set b to (contents of i) as number if b is not equal to 0 then set resText to (b as text) & item digCount of scaleList3 & resText end if set digCount to digCount + 1 end repeat return resText & lowerDigit end encodeJapaneseNumText –指定桁数で区切る on splitByDigit(a, splitDigit) set aList to characters of a set aList to reverse of aList set resList to {} set tempT to "" set tempC to 1 repeat with i in aList set tempT to contents of i & tempT if tempC mod splitDigit = 0 then set resList to {tempT} & resList set tempT to "" end if set tempC to tempC + 1 end repeat if tempT is not equal to "" then set resList to {tempT} & resList end if resList end splitByDigit on makeNewDocument given parameter:aStr tell application id "jp.co.artman21.JeditOmega" set aDoc to make new document with properties {text:aStr} end tell end makeNewDocument on retFrontDocsPath() tell application id "jp.co.artman21.JeditOmega" tell front document return path end tell end tell end retFrontDocsPath |
(Visited 31 times, 1 visits today)