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月 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

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

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1392) 10.14savvy (587) 10.15savvy (438) 11.0savvy (283) 12.0savvy (207) 13.0savvy (177) 14.0savvy (127) 15.0savvy (104) 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 (74) 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