AppleScript名:レコードのうち、最大の値を持つKeyを返す |
— Created 2016-12-14 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" set aDict to current application’s NSDictionary’s dictionaryWithDictionary:{sadness:0.01133416, anger:1.03975857E-4, happiness:2.90919736E-4, fear:2.28432211E-4, neutral:0.9830475, contempt:2.4698046E-4, disgust:1.02946949E-4, surprise:0.00464509334} set aMacKey to retMaxValueKey(aDict) of me –> "neutral" on retMaxValueKey(aDict) set aValList to aDict’s allValues() set maxVal to (sort1DNumList(aValList, false) of me)’s firstObject() set keyList to (aDict’s allKeys()) as list repeat with i in keyList set j to contents of i set aTmp to (aDict’s valueForKey:j) set aRes to compareNumerically(maxVal, aTmp) of me if aRes = true then return j end repeat return false end retMaxValueKey –Numerical Strings Compare on compareNumerically(aText as text, bText as text) set aStr to current application’s NSString’s stringWithString:aText return (aStr’s compare:bText options:(current application’s NSNumericSearch)) = current application’s NSOrderedSame end compareNumerically –1D List(数値)をsort / ascOrderがtrueだと昇順ソート、falseだと降順ソート on sort1DNumList(theList, aBool) set theSet to current application’s NSSet’s setWithArray:theList set theDescriptor to current application’s NSSortDescriptor’s sortDescriptorWithKey:(missing value) ascending:aBool set sortedList to theSet’s sortedArrayUsingDescriptors:{theDescriptor} return (sortedList) end sort1DNumList |
More from my site
(Visited 37 times, 1 visits today)