AppleScript名:バージョン番号文字列の正確な比較 |
— Created 2015-07-27 by Takaaki Naganoya — 2015 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set a to "10.10" set b to "10.10.0" set aRes to compareVersionNumericStrings(a, b) of me –> "A=B" set a to "10.10" set b to "10.10.0.1" set aRes to compareVersionNumericStrings(a, b) of me –> "B>A" set a to "10.10.1" set b to "10.10.0.1" set aRes to compareVersionNumericStrings(a, b) of me –> "A>B" set a to "10.10.1" set b to "10.9.1.1" set aRes to compareVersionNumericStrings(a, b) of me –> "A>B" –バージョン番号文字列の正確な比較 on compareVersionNumericStrings(a, b) set aList to parseVersionNumber(a) of me –> {"10", "10"} set bList to parseVersionNumber(b) of me –> {"10", "10", "0"} set aLen to length of aList set bLen to length of bList if aLen = bLen then –何もしない set buryTimes to 0 else if aLen > bLen then set buryTimes to (aLen – bLen) set bList to addNullItems(bList, "0", buryTimes) of me else if bLen > aLen then set buryTimes to (bLen – aLen) set aList to addNullItems(aList, "0", buryTimes) of me end if set cLen to length of aList –aListもbListも同じ長さなのでこれでいい repeat with i from 1 to cLen set aItem to contents of item i of aList set bItem to contents of item i of bList considering numeric strings if aItem > bItem then return "A>B" else if aItem < bItem then return "B>A" end if end considering end repeat return "A=B" end compareVersionNumericStrings –バージョン番号文字列からメジャーバージョンを取り出し数値として返す on parseVersionNumber(a) set aStr to current application’s NSString’s stringWithString:a set aRes to (aStr’s componentsSeparatedByString:".") set bRes to aRes’s allObjects() return bRes as list end parseVersionNumber –指定リストの末尾に指定個数、埋め草を追加する on addNullItems(aList, anItem, aTimes) copy aList to bList repeat aTimes times set the end of bList to anItem end repeat return bList end addNullItems |
More from my site
(Visited 28 times, 1 visits today)