Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

カテゴリー: Number

数値を指定桁でゼロパディング

Posted on 2月 24, 2018 by Takaaki Naganoya
AppleScript名:数値を指定桁でゼロパディング
— Created 2015-08-05 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aTEXT to numToZeroPaddingStr(99, 4, "0")
–>  "0099"

set aTEXT to numToZeroPaddingStr(99999999, 20, "0")
–> "00000000000099999999"

–整数の値に指定桁数ゼロパディングして文字列で返す
on numToZeroPaddingStr(aNum as integer, aDigit as integer, paddingChar as text)
  
  
set aNumForm to current application’s NSNumberFormatter’s alloc()’s init()
  
aNumForm’s setPaddingPosition:(current application’s NSNumberFormatterPadBeforePrefix)
  
aNumForm’s setPaddingCharacter:paddingChar
  
aNumForm’s setMinimumIntegerDigits:aDigit
  
  
set bNum to current application’s NSNumber’s numberWithInt:aNum
  
set aStr to aNumForm’s stringFromNumber:bNum
  
  
return aStr as text
  
end numToZeroPaddingStr

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ローマ数字エンコーダー、デコーダー v2

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ローマ数字エンコーダー、デコーダー v2
— Created 2015-11-16 by Takaaki Naganoya
— Modified 2017-05-18 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
–http://stackoverflow.com/questions/14762282/how-to-convert-a-roman-numeral-to-an-nsnumer

repeat with i from 1 to 3999
  set aRes to numberToRomanNumerals(i) of me
  
set bRes to romanNumeralsToNumber(aRes) of me
  
log {i, aRes, bRes}
end repeat

–Roman Number String Encoder
on numberToRomanNumerals(aNum)
  if (aNum < 0 or aNum > 3999) then return "" –条件が間違っていたので直した(2017/5/18)
  
  
set r_ones to {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
  
set r_tens to {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
  
set r_hunds to {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
  
set r_thouds to {"M", "MM", "MMM", "MMMM", "MMMMM", "MMMMMM", "MMMMMMM", "MMMMMMMM", "MMMMMMMMM"}
  
  
set ones to aNum mod 10
  
set tens to (aNum – ones) mod 100
  
set hundreds to (aNum – tens – ones) mod 1000
  
set thou to (aNum – hundreds – tens – ones) mod 10000
  
  
set tens to tens / 10
  
set hundreds to hundreds / 100
  
set thou to thou / 1000
  
  
set rNum to current application’s NSString’s stringWithString:""
  
  
if thou > 0 then set rNum to rNum’s stringByAppendingString:(item thou of r_thouds)
  
if hundreds > 0 then set rNum to rNum’s stringByAppendingString:(item hundreds of r_hunds)
  
if tens > 0 then set rNum to rNum’s stringByAppendingString:(item tens of r_tens)
  
if ones > 0 then set rNum to rNum’s stringByAppendingString:(item ones of r_ones)
  
  
return rNum as text
end numberToRomanNumerals

–Roman Number String Decoder
on romanNumeralsToNumber(str)
  — Piero Garzotto (http://scriptbuilders.net/files/romannumerals1.0.html)
  
local str, r, i
  
set r to 0
  
repeat with i from 1 to count str
    set r to r + (item (offset of (item i of str) in "mdclxvi") of ¬
      {1000, 500, 100, 50, 10, 5, 1}) * (((((offset of ¬
      (
item (i + 1) of (str & "@")) in "mdclxvi@") ≥ (offset of ¬
      (
item i of str) in "mdclxvi")) as integer) * 2) – 1)
  end repeat
  
return r
end romanNumeralsToNumber

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ABSを求める

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ABSを求める
set x to -44
set x to absNum(x)
–> 44

on absNum(q)
  if q is less than 0 then set q to –q
  
return q
end absNum

★Click Here to Open This Script 

Posted in Number | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

同スコアを考慮した順位決定(汎用ルーチン)

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:同スコアを考慮した順位決定(汎用ルーチン)
use AppleScript version "2.4"
use scripting additions

set rList to {{"近 装甲強化型ジム COST: 200", 60}, {"遠 ジム・キャノン COST: 160", 39}, {"近 ジム・コマンド COST: 200", 32}, {"近 ジム(WD隊) COST: 160", 26}, {"近 陸戦型ガンダム COST: 220", 23}, {"近 ジム改 COST: 240", 22}, {"遠 ガンタンク COST: 200", 21}, {"格 ジム(指揮官機) COST: 160", 20}, {"近 ジム COST: 120", 19}, {"格 陸戦型ジム COST: 120", 10}, {"近 ジム・トレーナー COST: 120", 9}, {"射 ジム・スナイパーII(WD隊) COST: 220", 8}, {"射 陸戦型ガンダム(ジム頭) COST: 200", 7}, {"格 ガンダム COST: 280", 6}, {"近 ザクII(F2) COST: 160", 6}, {"格 ジム・ストライカー COST: 180", 4}, {"近 ジム・寒冷地仕様 COST: 200", 4}, {"狙 ジム・スナイパーカスタム COST: 200", 3}, {"近 アクア・ジム COST: 160", 2}, {"遠 量産型ガンタンク COST: 160", 2}, {"格 ガンキャノン重装型 COST: 160", 1}, {"近 ジム・コマンドライトアーマー COST: 160", 1}, {"射 ガンキャノン COST: 200", 1}, {"格 ボールK型 COST: 120", 0}, {"射 デザート・ジム COST: 160", 0}}

set rRes to retRankingList(rList) of me
–> {{rank:1, aCon:"近 装甲強化型ジム COST: 200", aTimes:60}, {rank:2, aCon:"遠 ジム・キャノン COST: 160", aTimes:39}, {rank:3, aCon:"近 ジム・コマンド COST: 200", aTimes:32}, {rank:4, aCon:"近 ジム(WD隊) COST: 160", aTimes:26}, {rank:5, aCon:"近 陸戦型ガンダム COST: 220", aTimes:23}, {rank:6, aCon:"近 ジム改 COST: 240", aTimes:22}, {rank:7, aCon:"遠 ガンタンク COST: 200", aTimes:21}, {rank:8, aCon:"格 ジム(指揮官機) COST: 160", aTimes:20}, {rank:9, aCon:"近 ジム COST: 120", aTimes:19}, {rank:10, aCon:"格 陸戦型ジム COST: 120", aTimes:10}, {rank:11, aCon:"近 ジム・トレーナー COST: 120", aTimes:9}, {rank:12, aCon:"射 ジム・スナイパーII(WD隊) COST: 220", aTimes:8}, {rank:13, aCon:"射 陸戦型ガンダム(ジム頭) COST: 200", aTimes:7}, {rank:14, aCon:"格 ガンダム COST: 280", aTimes:6}, {rank:14, aCon:"近 ザクII(F2) COST: 160", aTimes:6}, {rank:16, aCon:"格 ジム・ストライカー COST: 180", aTimes:4}, {rank:16, aCon:"近 ジム・寒冷地仕様 COST: 200", aTimes:4}, {rank:18, aCon:"狙 ジム・スナイパーカスタム COST: 200", aTimes:3}, {rank:19, aCon:"近 アクア・ジム COST: 160", aTimes:2}, {rank:19, aCon:"遠 量産型ガンタンク COST: 160", aTimes:2}, {rank:21, aCon:"格 ガンキャノン重装型 COST: 160", aTimes:1}, {rank:21, aCon:"近 ジム・コマンドライトアーマー COST: 160", aTimes:1}, {rank:21, aCon:"射 ガンキャノン COST: 200", aTimes:1}, {rank:24, aCon:"格 ボールK型 COST: 120", aTimes:0}, {rank:24, aCon:"射 デザート・ジム COST: 160", aTimes:0}}

on retRankingList(rList)
  set aText to {} –出力用リスト
  
set aCount to 0
  
  
set prevPoint to -1
  
set buffCount to 0
  
  
  
repeat with i in rList
    
    
copy i to {j, bCount}
    
    
if prevPoint = bCount then
      –同じポイントが続いた場合
      
if buffCount is not equal to 0 then
        –同じポイントが複数回(2回以上)続いている場合
        
set buffCount to buffCount + 1
        
log {"case A:", buffCount, aCount, bCount, prevPoint}
      else
        –同じポイントが続いている場合(2回目)
        
set buffCount to 1
        
log {"case B:", buffCount, aCount, bCount, prevPoint}
      end if
    else
      –同じポイントが続いていない場合
      
if buffCount is not equal to 0 then
        –直前まで同じポイントが続いていた場合
        
set aCount to aCount + buffCount + 1 –バッファしておいたカウントを出力する
        
set buffCount to 0
        
log {"case C:", buffCount, aCount, bCount, prevPoint}
      else
        –通常パターン(直前まで同じポイントが続いていたりしない)
        
set buffCount to 0
        
set aCount to aCount + 1 –ランキング順位を+1
        
log {"case D:", buffCount, aCount, bCount, prevPoint}
      end if
    end if
    
    
set the end of aText to {rank:aCount, aCon:j as string, aTimes:bCount}
    
copy bCount to prevPoint
    
  end repeat
  
  
return aText
  
end retRankingList

–リストを任意のデリミタ付きでテキストに
on retArrowText(aList, aDelim)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end retArrowText

★Click Here to Open This Script 

Posted in list Number | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

平方根(SQR)を求める

Posted on 2月 23, 2018 by Takaaki Naganoya

平方根(SQR)を求める処理です。このほか、JavaScript Coreで関数計算を行わせるライブラリ「calcLibAS」を用いる方法があります。

AppleScript名:平方根(SQR)を求める

set a to getSQR(4) of me
–> 2.0

–平方根を求める
on getSQR(aNum)
  return (aNum ^ 0.5)
end getSQR

★Click Here to Open This Script 

Posted in Number | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

数値を日本語文字列に

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:数値を日本語文字列に
— Created 2016-11-13 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theValue to (current application’s NSNumberFormatter’s localizedStringFromNumber:287654321 numberStyle:(current application’s NSNumberFormatterSpellOutStyle)) as string
–>  "二億八千七百六十五万四千三百二十一"

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

日本語的数値表現エンコーダーv2

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:日本語的数値表現エンコーダーv2
–課題:オーバーフローチェックを行っていない
set a to "102320120000108220010"
set jRes to encodeJapaneseNumText(a) of japaneseNumberEncodingKit
–> "1垓232京120兆1億822万10"

script japaneseNumberEncodingKit
  –数字文字列を日本語数値表現文字列に変換
  
on encodeJapaneseNumText(aNum)
    
    
set aText to Stringify(aNum) of me
    
set aText to aText as Unicode text
    
set dotText to "." as Unicode text
    
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 Unicode text
      
set lowerDigit to characters b thru -1 of aText
      
set lowerDigit to lowerDigit as Unicode text
    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
    
log 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 Stringify(x) — for E+ numbers
    set x to x as string
    
set {tids, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, {"E+"}}
    
if (count (text items of x)) = 1 then
      set AppleScript’s text item delimiters to tids
      
return x
    else
      set {n, z} to {text item 1 of x, (text item 2 of x) as integer}
      
set AppleScript’s text item delimiters to tids
      
set i to character 1 of n
      
set decSepChar to character 2 of n — "." or ","
      
set d to text 3 thru -1 of n
      
set l to count d
      
if l > z then
        return (i & (text 1 thru z of d) & decSepChar & (text (z + 1) thru -1 of d))
      else
        repeat (z – l) times
          set d to d & "0"
        end repeat
        
return (i & d)
      end if
    end if
  end Stringify
end script

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

日本語的数値表現デコーダー v3.1

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:日本語的数値表現デコーダー v3.1
–課題:複数の小数点チェックが未実装
–set a to the clipboard
set a to "1垓232京120兆1億822万10"
set jRes to decodeJapaneseNumText(a) of japaneseNumberDecodingKit
–> "102320120000108220000"
–set the clipboard to jRes

script japaneseNumberDecodingKit
  
  
–日本語数値表現文字列を数字文字列にデコードする
  
on decodeJapaneseNumText(a)
    
    
set scaleList1 to {"万", "億", "兆", "京", "垓", "丈", "壌", "溝", "砂", "正", "載", "極", "恒河沙", "阿僧梢", "那由他", "不可思議", "無量大数"}
    
set bList to splitByDigitText(a, scaleList1) of me
    
    
    
–パースした最終アイテムにメジャー桁の桁リスト内の文字が入っているかをチェック(0〜9999までの桁を処理するため)
    
set lastItem to contents of last item of bList
    
set aRes to chkContainEachListItem(lastItem, scaleList1) of me
    
if aRes = false then
      –bListの最終アイテムが0〜9999だった場合    
      
set bbList to items 1 thru -2 of bList
    else
      –bListの最終アイテムはscaleList1の桁テキストを含んでいた(端数がなかった)
      
set bbList to bList
      
set lastItem to "0"
    end if
    
    
    
–各桁の評価を行う実装試験
    
set allFormula to {}
    
    
set scaleList2a to {"千", "百", "十"}
    
set scaleList2b to {"1000", "100", "10"}
    
    
set firstF to true
    
    
repeat with i in bbList
      
      
set anItem to contents of i as string
      
set cList to splitByDigitText(anItem, scaleList2a) of me
      
      
set tList to {"千", "百", "十"}
      
set rList to {"*1000+", "*100+", "*10+"}
      
      
set aa to repCharList(anItem, tList, rList) of me
      
set aPart to text 1 thru -2 of aa
      
set bPart to text -1 thru -1 of aa
      
if aPart ends with "+" then
        set aPart to aPart & "0"
      end if
      
      
set scaleList2 to {"10000", "100000000", "1000000000000", "10000000000000000", "100000000000000000000", "1000000000000000000000000", "10000000000000000000000000000", "100000000000000000000000000000000", "1000000000000000000000000000000000000", "10000000000000000000000000000000000000000", "100000000000000000000000000000000000000000000", "1000000000000000000000000000000000000000000000000", "10000000000000000000000000000000000000000000000000000", "100000000000000000000000000000000000000000000000000000000", "1000000000000000000000000000000000000000000000000000000000000", "10000000000000000000000000000000000000000000000000000000000000000", "100000000000000000000000000000000000000000000000000000000000000000000"}
      
      
set aFormula to "(" & aPart & ")" & "*" & repCharList(bPart, scaleList1, scaleList2) of me
      
      
if firstF then
        set allFormula to aFormula
        
set firstF to false
      else
        set allFormula to allFormula & " + " & aFormula
      end if
      
    end repeat
    
    
set allFormula to allFormula & " + " & lastItem
    
    
–allFormula
    
set cRes to calcByBC(allFormula) of me
    
return cRes
    
  end decodeJapaneseNumText
  
  
on splitByDigitText(aText, scaleList)
    set aList to words of aText
    
if (count of aList) = 1 then
      return aText
    end if
    
    
set calList to {}
    
set posCount to 1
    
set startPointer to 1
    
set iCount to length of aList
    
    
repeat with i from 1 to iCount
      set j to contents of item i of aList
      
if j is in scaleList then
        set tmpItem to items startPointer thru i of aList
        
set the end of calList to (tmpItem as string)
        
set startPointer to i + 1
      end if
    end repeat
    
    
if (i + 1) is not equal to startPointer then
      set tmpItem to items startPointer thru -1 of aList
      
set tmpItem to tmpItem as string
      
set the end of calList to tmpItem
    end if
    
    
return calList
    
  end splitByDigitText
  
  
  
–Written By Philip Aker
  
–文字置換ルーチン
  
on repChar(origText, targStr, repStr)
    set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
    
set temp to text items of origText
    
set AppleScript’s text item delimiters to repStr
    
set res to temp as text
    
set AppleScript’s text item delimiters to txdl
    
return res
  end repChar
  
  
  
on calcByBC(aFormula)
    set yC to ASCII character 92
    
set aCMD to "echo \" scale=10; " & aFormula & "\" | bc"
    
set aRes to do shell script aCMD
    
return aRes
  end calcByBC
  
  
  
–文字置換ルーチン リストにより複数のテキストを一括で置換
  
on repCharList(origText, targStrList, repStrList)
    set {tCount, rCount} to {length of targStrList, length of repStrList}
    
–targStrListとrepStrListの項目数がイコールでなければfalseを返す
    
if tCount is not equal to rCount then return false
    
    
set origT to origText
    
repeat with i from 1 to tCount
      set targStr to contents of (item i of targStrList)
      
set repStr to contents of (item i of repStrList)
      
      
set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr}
      
set temp to text items of origT
      
set AppleScript’s text item delimiters to repStr
      
set res to temp as text
      
set AppleScript’s text item delimiters to txdl
      
set origT to res
    end repeat
    
    
return res
  end repCharList
  
  
–指定文字列の中に指定リストの各アイテムが含まれているかをチェック
  
on chkContainEachListItem(chkStr, chkList)
    repeat with i in chkList
      set j to contents of i
      
if chkStr contains j then
        return true
      end if
    end repeat
    
return false
  end chkContainEachListItem
  
end script

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

NSByteCountFormatterCountStyleで数値から容量を表すテキストに変換

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:NSByteCountFormatterCountStyleで数値から容量を表すテキストに変換
— Created 2014-12-03 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

–NSByteCountFormatterCountStyle
property NSByteCountFormatterCountStyleFile : 0
property NSByteCountFormatterCountStyleMemory : 1
property NSByteCountFormatterCountStyleDecimal : 2
property NSByteCountFormatterCountStyleBinary : 3

set a to 100000

–ファイル容量
set b to current application’s NSByteCountFormatter’s stringFromByteCount:a countStyle:(current application’s NSByteCountFormatterCountStyleFile)
set c to b as string
log result
–> "100 KB"

–メモリー容量
set d to current application’s NSByteCountFormatter’s stringFromByteCount:a countStyle:(current application’s NSByteCountFormatterCountStyleMemory)
set e to b as string
log result
–> "100 KB"

–10進数
set f to current application’s NSByteCountFormatter’s stringFromByteCount:a countStyle:(current application’s NSByteCountFormatterCountStyleDecimal)
set g to b as string
log result
–> "100 KB"

–バイナリ?
set h to current application’s NSByteCountFormatter’s stringFromByteCount:a countStyle:(current application’s NSByteCountFormatterCountStyleBinary)
set i to b as string
log result
–> "100 KB"

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

NSNumberFormatterのテスト v2

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:NSNumberFormatterのテスト v2
— Created 2014-12-03 by Takaaki Naganoya
— 2014 Piyomaru Software
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"

set a to 100000
set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterNoStyle)
set c to b as string
–> "100000"

set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterDecimalStyle)
set c to b as string
–> "100,000"

set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterCurrencyStyle)
set c to b as string
–> "¥100,000"

set aa to 0.1
set b to current application’s NSNumberFormatter’s localizedStringFromNumber:aa numberStyle:(current application’s NSNumberFormatterPercentStyle)
set c to b as string
–> "10%"

set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterScientificStyle)
set c to b as string
–> "1E5"

set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterSpellOutStyle)
set c to b as string
–> "十万"

set a to 111111111
set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterSpellOutStyle)
set c to b as string
–> "一億千百十一万千百十一"

set a to 9.11111111111E+16
set b to current application’s NSNumberFormatter’s localizedStringFromNumber:a numberStyle:(current application’s NSNumberFormatterSpellOutStyle)
set c to b as string
–> "一京八千十四兆三千九百八十五億九百四十八万千九百八十四"

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy NSNumberFormatter | Leave a comment

DDUnitConverterで時速を秒速に変換

Posted on 2月 23, 2018 by Takaaki Naganoya

–> ddUnitConversionKit.framework

AppleScript名:DDUnitConverterで時速を秒速に変換
— Created 2015-11-19 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "ddUnitConversionKit" –https://github.com/davedelong/DDUnitConverter

–enums in DDVelocityUnitConverter.h
property DDVelocityUnitCentimetersPerHour : 0
property DDVelocityUnitCentimetersPerMinute : 1
property DDVelocityUnitCentimetersPerSecond : 2
property DDVelocityUnitFeetPerHour : 3
property DDVelocityUnitFeetPerMinute : 4
property DDVelocityUnitFeetPerSecond : 5
property DDVelocityUnitInchesPerHour : 6
property DDVelocityUnitInchesPerMinute : 7
property DDVelocityUnitInchesPerSecond : 8
property DDVelocityUnitKilometersPerHour : 9
property DDVelocityUnitKilometersPerMinute : 10
property DDVelocityUnitKilometersPerSecond : 11
property DDVelocityUnitKnots : 12
property DDVelocityUnitLight : 13
property DDVelocityUnitMach : 14
property DDVelocityUnitMetersPerHour : 15
property DDVelocityUnitMetersPerMinute : 16
property DDVelocityUnitMetersPerSecond : 17
property DDVelocityUnitMilesPerHour : 18
property DDVelocityUnitMilesPerMinute : 19
property DDVelocityUnitMilesPerSecond : 20
property DDVelocityUnitFurlongsPerFortnight : 21

–時速100kmを秒速kmに変換
set aVal to ((current application’s DDUnitConverter’s velocityUnitConverter())’s convertNumber:100 fromUnit:(DDVelocityUnitKilometersPerHour) toUnit:(DDVelocityUnitKilometersPerSecond)) as real
–>  0.027777777778

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ポイント数値のテキストをmmに書き換える v4

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ポイント数値のテキストをmmに書き換える v4
set aRes to " 772.4 x 1082.8 pts"
set sizeRes to changePTStoMM(aRes) of me
log result
–> {strDat:"272.6572 x 382.2284 mm", numDat:{272.6572, 382.2284}}

set aRes to "595 x 842 pts (A4)"
set sizeRes to changePTStoMM(aRes) of me
log result
–> {strDat:"210.035 x 297.226 mm", numDat:{210.035, 297.226}}

set aRes to " 2242.2 x 1618.58 pts"
set sizeRes to changePTStoMM(aRes) of me
log result
–> {strDat:"210.035 x 297.226 mm", numDat:{210.035, 297.226}}

–ポイント数値のテキストをmmに書き換える v4
–問題が見つかったので書き換え(2010/3/12)
–超泥縄式の対応だったので、「(A4)」の判型表示が行われないケースにはきちんと対応できていなかった
–慌てて直した。ちゃんと文字と数字のアトリビュートを取得しているのでまじめに活用してみた
on changePTStoMM(aText)
  set wList to words of aText
  
  
set attrList to {}
  
repeat with i in wList
    set the end of attrList to numChk(i) of me
  end repeat
  
  
set aCount to length of wList
  
  
set aReNumList to {}
  
repeat with i from 1 to (aCount)
    set anItem to item i of wList
    
set anAttr to item i of attrList
    
if anAttr = true then
      set the end of aReNumList to (point2mm(anItem as number) of me)
    end if
  end repeat
  
  
–エラー時の対応(多分ないと思うが、一応)
  
if length of aReNumList is not equal to 2 then return false
  
  
–まじめに数値データから文字列を組み立てて返す  
  
set num1 to contents of item 1 of aReNumList
  
set num2 to contents of item 2 of aReNumList
  
set aText to ((num1 as string) & " x " & num2 as string) & " mm"
  
  
return {strDat:aText, numDat:aReNumList}
  
end changePTStoMM

–リストを任意のデリミタ付きでテキストに
on makeStrFromListWithDelim(aDelim, aList)
  set aText to ""
  
set curDelim to AppleScript’s text item delimiters
  
set AppleScript’s text item delimiters to aDelim
  
set aText to aList as text
  
set AppleScript’s text item delimiters to curDelim
  
return aText
end makeStrFromListWithDelim

–ポイントからmmへの変換
on point2mm(a)
  return (a * 0.353) –1ポイント=0.353mm
end point2mm

–数値かどうか調べる
on numChk(aNum)
  set aClass to (class of aNum) as string
  
if aClass = "number" or aClass = "double" or aClass = "integer" or aClass = "real" then
    return true
  else if aClass = "string" or aClass = "text" or aClass = "unicode text" then
    try
      set bNum to aNum as number
      
return true
    on error
      return false
    end try
  end if
end numChk

on trimStrFromTo(aStr, fromStr, toStr)
  –fromStrは前から探す
  
if fromStr is not equal to "" then
    set sPos to (offset of fromStr in aStr) + 1
  else
    set sPos to 1
  end if
  
  
–toStrは後ろから探す
  
if toStr is not equal to "" then
    set b to (reverse of characters of aStr) as string
    
set ePos to (offset of toStr in b)
    
set ePos to ((length of aStr) – ePos)
  else
    set ePos to length of aStr
  end if
  
set aRes to text sPos thru ePos of aStr
  
return aRes
end trimStrFromTo

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ローカライズドな期間表記

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ローカライズドな期間表記
— Created 2016-01-14 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–Date Interval

set engNSDateIntervalFormatter to current application’s NSDateIntervalFormatter’s alloc()’s init()
engNSDateIntervalFormatter’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"en-US")
engNSDateIntervalFormatter’s setDateStyle:(current application’s NSDateIntervalFormatterLongStyle)
engNSDateIntervalFormatter’s setDateTemplate:(current application’s NSDateFormatter’s alloc()’s init()’s setDateFormat:"yyyy-MM-dd HH:mm")
engNSDateIntervalFormatter’s setTimeStyle:(current application’s NSDateIntervalFormatterNoStyle)
engNSDateIntervalFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:"JST")
engNSDateIntervalFormatter’s setCalendar:(current application’s NSCalendar’s currentCalendar())

set startDate to current application’s NSDate’s |date|()
set endDate to current application’s NSDate’s dateWithTimeInterval:86400 sinceDate:startDate
set outEString to (engNSDateIntervalFormatter’s stringFromDate:startDate toDate:endDate) as string
–>  "1/14/16 – 1/15/16"–ShortStyle
–>  "Jan 14 – 15, 2016"–Medium Style
–>  "January 14 – 15, 2016"–Long Style
–>  "Thursday, January 14 – Friday, January 15, 2016"–Full Style

set jpnNSDateIntervalFormatter to current application’s NSDateIntervalFormatter’s alloc()’s init()
jpnNSDateIntervalFormatter’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"ja")
jpnNSDateIntervalFormatter’s setDateStyle:(current application’s NSDateIntervalFormatterFullStyle)
jpnNSDateIntervalFormatter’s setDateTemplate:(current application’s NSDateFormatter’s alloc()’s init()’s setDateFormat:"yyyy-MM-dd HH:mm")
jpnNSDateIntervalFormatter’s setTimeStyle:(current application’s NSDateIntervalFormatterNoStyle)
jpnNSDateIntervalFormatter’s setTimeZone:(current application’s NSTimeZone’s timeZoneWithAbbreviation:"JST")
jpnNSDateIntervalFormatter’s setCalendar:(current application’s NSCalendar’s currentCalendar())

set startDate to current application’s NSDate’s |date|()
set endDate to current application’s NSDate’s dateWithTimeInterval:86400 sinceDate:startDate
set outJString to (jpnNSDateIntervalFormatter’s stringFromDate:startDate toDate:endDate) as string
–>  "2016/01/14~2016/01/15"–ShortStyle
–>  "2016/01/14~2016/01/15"–Medium Style
–>  "2016/01/14~2016/01/15"–Long Style
–>  "2016/01/14(木曜日)~2016/01/15(金曜日)"–Full Style

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ローカライズドな度量衡表示(エネルギー)

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ローカライズドな度量衡表示(エネルギー)
— Created 2015-11-20 by Shane Stanley
— Modified 2016-01-14 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–Energy

set engEFormatter to current application’s NSEnergyFormatter’s alloc()’s init()
engEFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"en-US")
engEFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong)

engEFormatter’s setForFoodEnergyUse:true
set a1Res to (engEFormatter’s stringFromJoules:85) as string
–>  "20.315 calories"

engEFormatter’s setForFoodEnergyUse:false
set a1Res to (engEFormatter’s stringFromJoules:85) as string
–>  "20.315 calories"

set a2Res to (engEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitJoule)) as string –joule
–>  "1 joule"
set a3Res to (engEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitKilojoule)) as string –k joule
–>  "1 kilojoule"
set a4Res to (engEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitCalorie)) as string –Cal
–>  "1 calorie"
set a5Res to (engEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitKilocalorie)) as string –K Cal
–>  "1 kilocalorie"

set jpnEFormatter to current application’s NSEnergyFormatter’s alloc()’s init()
jpnEFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"ja")
jpnEFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong)

jpnEFormatter’s setForFoodEnergyUse:true
set j1Res to (jpnEFormatter’s stringFromJoules:85) as string
–>  "85ジュール"

jpnEFormatter’s setForFoodEnergyUse:false
set j1Res to (jpnEFormatter’s stringFromJoules:85) as string
–>  "85ジュール"

set j2Res to (jpnEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitJoule)) as string –joule
–>  "1ジュール"
set j3Res to (jpnEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitKilojoule)) as string –k joule
–>  "1キロジュール"
set j4Res to (jpnEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitCalorie)) as string –Cal
–>  "1熱力学カロリー"
set j5Res to (jpnEFormatter’s stringFromValue:1 unit:(current application’s NSEnergyFormatterUnitKilocalorie)) as string –K Cal
–>  "1キロカロリー"

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ローカライズドな度量衡表示(重さ)

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ローカライズドな度量衡表示(重さ)
— Created 2015-11-20 by Shane Stanley
— Modified 2016-01-14 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–Weight

set engMFormatter to current application’s NSMassFormatter’s alloc()’s init()
engMFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"en-US")
engMFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) –NSFormattingUnitStyleShort, NSFormattingUnitStyleMedium, NSFormattingUnitStyleLong

set a1Res to (engMFormatter’s stringFromKilograms:85) as string –Kg
–>  "187.393 pounds"
set a2Res to (engMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitGram)) as string –g
–>  "1 gram"
set a3Res to (engMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitOunce)) as string –オンス
–>  "1 ounce"
set a4Res to (engMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitStone)) as string –ストーン(=14 pounds)
–>  "1 stone"

set jpnMFormatter to current application’s NSMassFormatter’s alloc()’s init()
jpnMFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"ja")
jpnMFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) –NSFormattingUnitStyleShort, NSFormattingUnitStyleMedium, NSFormattingUnitStyleLong

set j1Res to (jpnMFormatter’s stringFromKilograms:85) as string –Kg
–>  "85キログラム"
set j2Res to (jpnMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitGram)) as string –g
–>  "1グラム"
set j3Res to (jpnMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitOunce)) as string –オンス
–>  "1オンス"
set j4Res to (jpnMFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitStone)) as string –ストーン(=14 pounds)
–>  "1ストーン

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ローカライズドな度量衡表示(長さ)

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ローカライズドな度量衡表示(長さ)
— Created 2015-11-20 by Shane Stanley
— Modified 2016-01-14 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

–Length

set engLFormatter to current application’s NSLengthFormatter’s alloc()’s init()
engLFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"en-US")
engLFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) –NSFormattingUnitStyleShort, NSFormattingUnitStyleMedium, NSFormattingUnitStyleLong

set a1Res to (engLFormatter’s stringFromMeters:85) as string –m
–>  "92.956 yards"
set a2Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMillimeter)) as string –mm
–>  "1 millimeter"
set a3Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitCentimeter)) as string –cm
–>  "1 centimeter"
set a4Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMeter)) as string –m
–>  "1 meter"
set a5Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitKilometer)) as string –Km
–>  "1 kilometer"
set a6Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitInch)) as string –inch
–>  "1 inch"
set a7Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitFoot)) as string –feet
–>  "1 foot"
set a8Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitYard)) as string –Yard
–>  "1 yard"
set a9Res to (engLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMile)) as string –Mile
–>  "1 mile"

set jpnLFormatter to current application’s NSLengthFormatter’s alloc()’s init()
jpnLFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"ja")
jpnLFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong) –NSFormattingUnitStyleShort, NSFormattingUnitStyleMedium, NSFormattingUnitStyleLong

set j1Res to (jpnLFormatter’s stringFromMeters:85) as string –m
–>  "85メートル"
set j2Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMillimeter)) as string –mm
–>  "1ミリメートル"
set j3Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitCentimeter)) as string –cm
–>  "1センチメートル"
set j4Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMeter)) as string –m
–>  "1メートル"
set j5Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitKilometer)) as string –Km
–>  "1キロメートル"
set j6Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitInch)) as string –inch
–>  "1インチ"
set j7Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitFoot)) as string –feet
–>  "1フィート"
set j8Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitYard)) as string –Yard
–>  "1ヤード"
set j9Res to (jpnLFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitMile)) as string –Mile
–>  "1マイル"

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

ローカライズドな度量衡変換(lengthFormatter)

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:ローカライズドな度量衡変換(lengthFormatter)
— Created 2015-11-20 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set theNSMassFormatter to current application’s NSMassFormatter’s alloc()’s init()
theNSMassFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"en-US")
theNSMassFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong)
(
theNSMassFormatter’s stringFromKilograms:85) as list of string or string
log result
–> "187.393 pounds"
(
theNSMassFormatter’s stringFromValue:1 unit:(current application’s NSMassFormatterUnitGram)) as list of string or string
log result
–>  "1 gram"

set theNSLengthFormatter to current application’s NSLengthFormatter’s alloc()’s init()
theNSLengthFormatter’s numberFormatter()’s setLocale:(current application’s NSLocale’s localeWithLocaleIdentifier:"ja")
theNSLengthFormatter’s setUnitStyle:(current application’s NSFormattingUnitStyleLong)
(
theNSLengthFormatter’s stringFromMeters:1) as list of string or string
log result
–>  "100センチメートル"–We usually use "100cm" in Japanese.
(
theNSLengthFormatter’s stringFromValue:1 unit:(current application’s NSLengthFormatterUnitFoot)) as list of string or string
log result
–>  "1フィート"

★Click Here to Open This Script 

Posted in Number Text | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

液体の容積の単位変換

Posted on 2月 23, 2018 by Takaaki Naganoya
AppleScript名:液体の容積の単位変換
–液体の容積の単位変換
set a to 10 as liters
–> liters 10.0

set b to a as gallons
–> gallons 2.641720372842

set c to a as quarts
–> quarts 10.566881491367

★Click Here to Open This Script 

Posted in Number | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

最小公倍数(LCM)を求める v3

Posted on 2月 23, 2018 by Takaaki Naganoya

–> primeNumKit.framework

AppleScript名:最小公倍数を求める v3
— Created 2017-05-20 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "primeNumKit"
–https://github.com/NazarYavornytskyy/prime-number-objc

set a1Num to 75
set b1Num to 315
set cRes to getLCMbetweenTwoNums(a1Num, b1Num) of me
–> 1575

–2つの数の最小公倍数を求める
on getLCMbetweenTwoNums(a1Num, b1Num)
  set aGCD to getGCDbetweenTwoNums(a1Num, b1Num) of me
  
set a2Num to a1Num div aGCD
  
set b2Num to b1Num div aGCD
  
return (aGCD * a2Num * b2Num)
end getLCMbetweenTwoNums

–2つの数の最大公約数を求める
on getGCDbetweenTwoNums(aNum, bNum)
  set aRes to getPrimeFactor(aNum) of me
  
set bRes to getPrimeFactor(bNum) of me
  
  
–2つのリストを集合として扱い、積集合を求める
  
set aSet to current application’s NSMutableSet’s setWithArray:aRes
  
set bSet to current application’s NSMutableSet’s setWithArray:bRes
  
aSet’s intersectSet:bSet
  
set cRes to aSet’s allObjects() as list
  
  
set dRes to multipleListElements(cRes) of me
  
return dRes
end getGCDbetweenTwoNums

–与えられた数を素因数分解する
on getPrimeFactor(aTargNum)
  copy aTargNum to a
  
set pFactList to {}
  
  
repeat
    set pRes to checkPrimeNumber(a) of me
    
if pRes = true then
      set the end of pFactList to a
      
return pFactList
    end if
    
    
set aFactor to checkFactor(a) of me
    
set the end of pFactList to aFactor
    
set a to a div aFactor
  end repeat
end getPrimeFactor

–素数チェック
on checkPrimeNumber(aNum)
  if aNum < 10000 then –0.25秒以上かかりそうな処理はCocoaで処理(実測値ベース)
    return checkPrimeNumberSub1(aNum) of me
  else
    set aPrime to current application’s PrimeNumberHelper’s alloc()’s init()
    
return (aPrime’s isPrime:aNum) as boolean
  end if
end checkPrimeNumber

–小さい数の場合の素数検出
on checkPrimeNumberSub1(aNum)
  repeat with i from 2 to (aNum div 2)
    if aNum mod i is equal to 0 then
      return i –false
    end if
  end repeat
  
return true
end checkPrimeNumberSub1

–素因数チェック
on checkFactor(aNum)
  repeat with i from 2 to (aNum div 2)
    if aNum mod i is equal to 0 then
      return i
    end if
  end repeat
end checkFactor

–リスト内の要素をすべて乗算する
on multipleListElements(aList)
  set aNum to 1
  
set aRes to 1
  
repeat with i in aList
    set aRes to aRes * (aNum * i)
  end repeat
  
return aRes
end multipleListElements

★Click Here to Open This Script 

Posted in Number | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

最大公約数(GCD)を求めるv4

Posted on 2月 23, 2018 by Takaaki Naganoya

–> primeNumKit.framework

AppleScript名:最大公約数を求めるv4
— Created 2017-05-20 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "primeNumKit" –https://github.com/NazarYavornytskyy/prime-number-objc

set aRes to getGCDbetweenTwoNums(75, 315) of me
–> 15

–2つの数の最大公約数を求める
on getGCDbetweenTwoNums(aNum, bNum)
  set aRes to getPrimeFactor(aNum) of me
  
set bRes to getPrimeFactor(bNum) of me
  
  
–2つのリストを集合として扱い、積集合を求める
  
set aSet to current application’s NSMutableSet’s setWithArray:aRes
  
set bSet to current application’s NSMutableSet’s setWithArray:bRes
  
aSet’s intersectSet:bSet
  
set cRes to aSet’s allObjects() as list
  
  
set dRes to multipleListElements(cRes) of me
  
return dRes
end getGCDbetweenTwoNums

–与えられた数を素因数分解する
on getPrimeFactor(aTargNum)
  copy aTargNum to a
  
set pFactList to {}
  
  
repeat
    set pRes to checkPrimeNumber(a) of me
    
if pRes = true then
      set the end of pFactList to a
      
return pFactList
    end if
    
    
set aFactor to checkFactor(a) of me
    
set the end of pFactList to aFactor
    
set a to a div aFactor
  end repeat
end getPrimeFactor

–素数チェック
on checkPrimeNumber(aNum)
  if aNum < 10000 then –0.25秒以上かかりそうな処理はCocoaで処理(実測値ベース)
    return checkPrimeNumberSub1(aNum) of me
  else
    set aPrime to current application’s PrimeNumberHelper’s alloc()’s init()
    
return (aPrime’s isPrime:aNum) as boolean
  end if
end checkPrimeNumber

–小さい数の場合の素数検出
on checkPrimeNumberSub1(aNum)
  repeat with i from 2 to (aNum div 2)
    if aNum mod i is equal to 0 then
      return i –false
    end if
  end repeat
  
return true
end checkPrimeNumberSub1

–素因数チェック
on checkFactor(aNum)
  repeat with i from 2 to (aNum div 2)
    if aNum mod i is equal to 0 then
      return i
    end if
  end repeat
end checkFactor

–リスト内の要素をすべて乗算する
on multipleListElements(aList)
  set aNum to 1
  
set aRes to 1
  
repeat with i in aList
    set aRes to aRes * (aNum * i)
  end repeat
  
return aRes
end multipleListElements

★Click Here to Open This Script 

Posted in Number | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

Post navigation

  • Older posts
  • Newer posts

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • 開発機としてM2 Mac miniが来たのでガチレビュー
  • macOS 13.6.5 AS系のバグ、一切直らず
  • CotEditorで2つの書類の行単位での差分検出
  • Apple純正マウス、キーボードのバッテリー残量取得
  • macOS 15, Sequoia
  • 初心者がつまづきやすい「log」コマンド
  • 指定のWordファイルをPDFに書き出す
  • Adobe AcrobatをAppleScriptから操作してPDF圧縮
  • メキシカンハットの描画
  • Pages本執筆中に、2つの書類モード切り替えに気がついた
  • 与えられた文字列の1D Listのすべての順列組み合わせパターン文字列を返す v3(ベンチマーク用)
  • 2023年に書いた価値あるAppleScript
  • Pixelmator Pro v3.6.4でAppleScriptからの操作時の挙動に違和感が
  • AdobeがInDesign v19.4からPOSIX pathを採用
  • Safariで「プロファイル」機能を使うとAppleScriptの処理に影響
  • Cocoa Scripting Course 続刊計画
  • Numbersで選択範囲のセルの前後の空白を削除
  • macOS 14.xでScript Menuの実行速度が大幅に下がるバグ
  • NaturalLanguage.frameworkでNLEmbeddingの処理が可能な言語をチェック
  • AppleScriptによる並列処理

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1392) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (208) 13.0savvy (179) 14.0savvy (129) 15.0savvy (106) CotEditor (64) Finder (51) iTunes (19) Keynote (115) NSAlert (61) NSArray (51) NSBitmapImageRep (20) NSBundle (20) NSButton (34) NSColor (53) NSDictionary (28) NSFileManager (23) NSFont (21) NSImage (41) NSJSONSerialization (21) NSMutableArray (63) NSMutableDictionary (22) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (119) NSURL (98) NSURLRequest (23) NSUTF8StringEncoding (30) NSView (33) NSWorkspace (20) Numbers (75) Pages (54) Safari (44) Script Editor (27) WKUserContentController (21) WKUserScript (20) WKWebView (23) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • 3D
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Beginner
  • Benchmark
  • beta
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • check sum
  • Clipboard
  • Cocoa-AppleScript Applet
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • diff
  • drive
  • Droplet
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • Hex
  • History
  • How To
  • iCloud
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • Library
  • list
  • Locale
  • Localize
  • Machine Learning
  • Map
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • rectangle
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2025年1月
  • 2024年12月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年6月
  • 2024年5月
  • 2024年4月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年6月
  • 2023年5月
  • 2023年4月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月
  • 2022年11月
  • 2022年10月
  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC