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

最大公約数(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

数値に対して、小数点以下の数値の切り上げ、切り下げ

Posted on 2月 21, 2018 by Takaaki Naganoya
AppleScript名:数値に対して、小数点以下の数値の切り上げ、切り下げ
— Created 2017-08-12 by Takaaki Naganoya
— Modified 2017-08-28 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSString : a reference to current application’s NSString
property NSNumber : a reference to current application’s NSNumber
property NSNumberFormatter : a reference to current application’s NSNumberFormatter
property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp
property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown

set a to 0.909661
set a1Res to roundingDownNumStr(a, 2) of me
–> 0.9

on roundingDownNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s doubleValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown)
  
set aStr to aFormatter’s stringFromNumber:aa
  
return (aStr as text) as real
end roundingDownNumStr

on roundingUpNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s doubleValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp)
  
set aStr to aFormatter’s stringFromNumber:aa
  
return (aStr as text) as real
end roundingUpNumStr

★Click Here to Open This Script 

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

数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2

Posted on 2月 21, 2018 by Takaaki Naganoya
AppleScript名:数値文字列に対して、少数点以下の数値の切り上げ、切り下げ v2
— Created 2017-08-12 by Takaaki Naganoya
— Modified 2017-08-28 by Shane Stanley
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

property NSString : a reference to current application’s NSString
property NSNumber : a reference to current application’s NSNumber
property NSNumberFormatter : a reference to current application’s NSNumberFormatter
property NSNumberFormatterRoundUp : a reference to current application’s NSNumberFormatterRoundUp
property NSNumberFormatterRoundDown : a reference to current application’s NSNumberFormatterRoundDown

set a to "0.9096617698669434"
set a1Res to roundingDownNumStr(a, 2) of me

set b to "0.0001830748806241899"
set b1Res to roundingDownNumStr(b, 2) of me

return {a1Res, b1Res}
–>  {​​​​​0.9, ​​​​​0.0​​​}

on roundingDownNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s doubleValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundDown)
  
set aStr to aFormatter’s stringFromNumber:aa
  
return (aStr as text) as real
end roundingDownNumStr

on roundingUpNumStr(aNum as string, aDigit as integer)
  set a to NSString’s stringWithString:aNum
  
set aa to a’s doubleValue()
  
set aFormatter to NSNumberFormatter’s alloc()’s init()
  
aFormatter’s setMaximumFractionDigits:aDigit
  
aFormatter’s setRoundingMode:(NSNumberFormatterRoundUp)
  
set aStr to aFormatter’s stringFromNumber:aa
  
return (aStr as text) as real
end roundingUpNumStr

★Click Here to Open This Script 

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

数値書式関連

Posted on 2月 21, 2018 by Takaaki Naganoya
AppleScript名:数値書式関連
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set theNumber to 1.234567890123E+12
set theResult to formatNumber_(theNumber)
–> "1,234,567,890,123"

set aRes to textFromNumber_forLocale_(1.2345678E+4, "fr")
–> "douze-mille-trois-cent-quarante-cinq virgule six sept huit"

set aRes to textFromNumber_forLocale_(1.2345678E+4, "ja")
–> "一万二千三百四十五・六七八"

set aRes to formatNumber_usingFormat_(1.2345678E+4, "#,###.00;0.00;(#,##0.00)")
–> "12,345.68"

on formatNumber:theNumber
  set theFormatter to current application’s NSNumberFormatter’s new()
  
theFormatter’s setNumberStyle:(current application’s NSNumberFormatterDecimalStyle)
  
set theResult to theFormatter’s stringFromNumber:theNumber
  
return theResult as text
end formatNumber:

on textFromNumber:theNumber forLocale:localeString
  set theFormatter to current application’s NSNumberFormatter’s new()
  
theFormatter’s setNumberStyle:(current application’s NSNumberFormatterSpellOutStyle)
  
set theLocale to current application’s NSLocale’s localeWithLocaleIdentifier:localeString
  
theFormatter’s setLocale:theLocale
  
set theResult to theFormatter’s stringFromNumber:theNumber
  
return theResult as text
end textFromNumber:forLocale:

on formatNumber:theNumber usingFormat:formatString
  set theFormatter to current application’s NSNumberFormatter’s new()
  
theFormatter’s setFormat:formatString
  
theFormatter’s setLocalizesFormat:false
  
set theResult to theFormatter’s stringFromNumber:theNumber
  
return theResult as text
end formatNumber:usingFormat:

★Click Here to Open This Script 

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

大きな数値同士を足し算する

Posted on 2月 21, 2018 by Takaaki Naganoya

巨大な数値同士を加算するAppleScriptです。

AppleScriptの数値型データが±99999999程度で指数表示になってしまうため、巨大な数値を扱うのには向いていないのですが、直接数値で扱わずに文字列として保持すれば問題はありません。

数値文字同士の計算についても、bcコマンドを使うことで四則演算+α程度は行えるので、あまり問題はないでしょう。bcコマンドを呼び出すオーバーヘッドはそれなりにあるので、すべての数値を文字列で保持したりするのは得策ではありません。必要な箇所だけ使う、といったところです。

AppleScript名:大きな数値同士を足し算する
set a to "100000000000000000"
set b to "200000000000000000"

set c to addLargeNumber(a, b) of me

on addLargeNumber(aNum, bNum)
  set aNumStr to aNum as string
  
set bNumStr to bNum as string
  
  
set aCMD to "echo \" scale=10; " & aNumStr & "+" & bNumStr & " \" | bc"
  
set aRes to do shell script aCMD
  
return aRes
end addLargeNumber

★Click Here to Open This Script 

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

与えられた数が素数かどうかチェック(PrimeNumberHelper)

Posted on 2月 21, 2018 by Takaaki Naganoya

与えられた数値が素数かどうかチェックを行うAppleScriptです。

素数チェックには、外部フレームワーク「primeNumKit.framework」を呼び出しています。

素数チェックや素因数分解は意外と実務でも必要になる機会が多く、高速な計算ルーチンが使えると便利です。

–> primeNumKit.framework

AppleScript名:与えられた数が素数かどうかチェック(PrimeNumberHelper)
— Created 2017-05-18 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 aPrime to current application’s PrimeNumberHelper’s alloc()’s init()
set a to 75777773
set aRes to (aPrime’s isPrime:a) as boolean

★Click Here to Open This Script 

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

簡単な素因数分解v5

Posted on 2月 21, 2018 by Takaaki Naganoya

与えられた数値の素因数分解を行うAppleScriptです。

素数チェックには、数値が小さい場合にはOLD Style AppleScriptの計算ルーチンを呼び出し、数値が大きい(=計算に時間がかかる)場合には外部フレームワーク「primeNumKit.framework」を呼び出すようにしています。

AppleScriptからのCocoaの機能呼び出しにも時間がかかるので、細かい処理を頻繁に呼び出すと意外とAppleScriptだけで処理したほうが速かったりします(素数チェックを行うためにループで割り算を行いますが、最大値は2から目的値の1/2まででよく、こうした地道な改善が予想外に効いています)。

そのため、計算量を見てどちらで処理するかを判定しています。

–> primeNumKit.framework

AppleScript名:簡単な素因数分解v5
— Created 2017-05-18 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "primeNumKit" –https://github.com/NazarYavornytskyy/prime-number-objc

set aRes to getPrimeFactor(56) of me
–> {2, 2, 2, 7}

set aRes to getPrimeFactor(54) of me
–> {2, 3, 3, 3}

set aRes to getPrimeFactor(9999991) of me
–> {9999991} –素数

–与えられた数を素因数分解する
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

★Click Here to Open This Script 

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

Post navigation

  • Newer posts

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

Google Search

Popular posts

  • macOS 26, Tahoe
  • Script Debuggerの開発と販売が2025年に終了
  • 【続報】macOS 15.5で特定ファイル名パターンのfileをaliasにcastすると100%クラッシュするバグ
  • NSObjectのクラス名を取得 v2.1
  • 2024年に書いた価値あるAppleScript
  • Xcode上のAppleScriptObjCのプログラムから、Xcodeのログ欄へのメッセージ出力を実行
  • AppleScript Dropletのバグっぽい動作が「復活」(macOS 15.5β)
  • Script Debuggerがフリーダウンロードで提供されることに
  • macOS 26, 15.5でShortcuts.app「AppleScriptを実行」アクションのバグが修正される
  • 指定フォルダ以下の画像のMD5チェックサムを求めて、重複しているものをピックアップ
  • 執筆中:AppleScript最新リファレンスver2.8対応(macOS 15対応アップデート)
  • Dock Menu
  • macOS 15.5beta5(24F74)でaliasのキャスティングバグが修正された???
  • Claris FileMaker Pro 2025(v22)がリリースされた
  • 複数の重複検出ルーチンを順次速度計測
  • Applicationのactivateを記録する v2
  • Numbersで選択範囲のdateの年を+1する
  • シンプルな文字置換
  • Appleに買収されたPixelmator ProがAppleとしての初アップデート
  • Excel 指定範囲のセルの上に画像を配置

Tags

10.11savvy (1101) 10.12savvy (1242) 10.13savvy (1391) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (212) 13.0savvy (204) 14.0savvy (159) 15.0savvy (165) 26.0savvy (33) CotEditor (67) Finder (53) Keynote (120) 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 (76) Pages (56) 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
  • date
  • 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
  • Newt On Project
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • parallel processing
  • PDF
  • Peripheral
  • process
  • 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
  • Scripting Additions
  • 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)
  • 未分類

アーカイブ

  • 2026年1月
  • 2025年12月
  • 2025年11月
  • 2025年10月
  • 2025年9月
  • 2025年8月
  • 2025年7月
  • 2025年6月
  • 2025年5月
  • 2025年4月
  • 2025年3月
  • 2025年2月
  • 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