Leading Zero

汎用ゼロパディング(桁数指定) Applescript


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002
003##最大番号
004set strMexNO to 9999 as integer
005##テキストにして
006set strCntNo to strMexNO as text
007##桁数を調べ
008set numLength to (length of strCntNo) as integer
009##桁数をマイナスの値にしておく
010set numThrough to (numLength * -1) as number
011#ゼロを追加するテキスト初期化
012set strZeroPadd to ("") as text
013##桁数分ゼロを追加
014repeat numLength times
015  set strZeroPadd to (strZeroPadd & "0") as text
016end repeat
017
018set numCntNo to 0 as integer
019repeat 100 times
020  ##数値をテキストにして
021  set strCntNo to numCntNo as text
022  ##桁数分の0のテキストに数値を追加
023  set strZeroPaddNo to (strZeroPadd & strCntNo) as text
024  ##必要な桁数分だけ取り出す
025  set strPrintPageNO to (text numThrough through -1 of strZeroPaddNo) as text
026  log strPrintPageNO
027  
028  
029  set numCntNo to numCntNo + 11 as integer
030end repeat
AppleScriptで生成しました

|

小数点以下 X桁での丸め


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004# com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006##自分環境がos12なので2.8にしているだけです
007use AppleScript version "2.8"
008use framework "Foundation"
009use scripting additions
010
011property refMe : a reference to current application
012
013set numNumber to (29.970029830933) as number
014
015set ocidDecimalNumber to refMe's NSDecimalNumber's decimalNumberWithString:(numNumber as text)
016set appNumberFormatter to refMe's NSNumberFormatter's alloc()'s init()
017appNumberFormatter's setPositiveFormat:("#,##0.00")
018set ocidDecimalNumber to appNumberFormatter's stringFromNumber:(ocidDecimalNumber)
019
020log ocidDecimalNumber as text
021log ocidDecimalNumber's doubleValue() as number
022-->(*29.97*)
AppleScriptで生成しました

|

汎用ゼロパディング(桁数指定)


サンプルコード

サンプルソース(参考)
行番号ソース
001
002log doAddLeadingZeros(1, 3)
003log doAddLeadingZeros(99, 3)
004log doAddLeadingZeros(100, 3)
005log doAddLeadingZeros(1, 4)
006log doAddLeadingZeros(99, 4)
007log doAddLeadingZeros(100, 4)
008log doAddLeadingZeros(1000, 4)
009
010
011
012
013on doAddLeadingZeros(argNo, argAddZeros)
014  #渡された数値
015  set numArgNo to argNo as integer
016  set strArgNo to argNo as text
017  #渡された桁数に10のべき乗
018  set numArgAddZeros to argAddZeros as integer
019  set numThresholdDigits to (10 ^ argAddZeros) as integer
020  #渡された数値が小さければ0を付与する処理をする
021  if numArgNo < numThresholdDigits then
022    #戻し値用のテキストを初期化
023    set strLeadingZeros to ("") as text
024    #渡された数値の桁数を数える
025    set numCntLength to (length of strArgNo) as integer
026    #追加する0の数
027    set numAddZero to (numArgAddZeros - numCntLength) as integer
028    #戻し値用のテキストに↑の数だけ0を追加
029    repeat numAddZero times
030      set strLeadingZeros to (strLeadingZeros & "0") as text
031    end repeat
032    #戻し値用の0に渡された値を並べてテキストで戻す
033    set strArgNo to (strLeadingZeros & strArgNo) as text
034    return strArgNo
035  else
036    #処理不要ならそのままテキストで戻す
037    return strArgNo as text
038  end if
039end doAddLeadingZeros
AppleScriptで生成しました

|

[NSNumberFormatter]数値→ゼロパディングされたテキスト


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application


set numNo to 1 as integer
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setMinimumIntegerDigits:(3)
ocidFormatter's setMaximumIntegerDigits:(3)
set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
log ocidDecStr as text
-->(*001*)

set numNo to 99 as integer
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setMinimumIntegerDigits:(3)
ocidFormatter's setMaximumIntegerDigits:(3)
set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
log ocidDecStr as text
-->(*099*)

set numNo to 120 as integer
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setMinimumIntegerDigits:(3)
ocidFormatter's setMaximumIntegerDigits:(3)
set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
log ocidDecStr as text
-->(*120*)


|

3桁ゼロパディング


サンプルコード

サンプルソース(参考)
行番号ソース
001set numCnt to 0 as number
002
003repeat 100 times
004  set numCnt to numCnt + 1 as number
005  set strZeroPadd to "000" as text
006  set strZeroPadd to (strZeroPadd & (numCnt as text)) as text
007  set strZeroPadd to (text -3 through -1 of strZeroPadd) as text
008  
009  log strZeroPadd
010end repeat
AppleScriptで生成しました

|

整数のゼロパディング

stringByPaddingToLengthからの
substringFromIndex

【スクリプトエディタで開く】

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application

##元になる整数
set numInteger to 10 as integer

####指定桁数
set numZeroSuppCnt to 6 as integer
####出力用テキスト初期化
set ocidZeroSupText to refMe's NSMutableString's stringWithCapacity:0
####0ゼロを指定桁数だけ並べたテキストを作る
set ocidZeroSupText to ocidZeroSupText's stringByPaddingToLength:numZeroSuppCnt withString:"0" startingAtIndex:0
####後ろに値をテキストで追加する
ocidZeroSupText's appendString:(numInteger as text)
####追加した数値の桁数
set nimIntLength to (numInteger as text)'s length as integer
####桁数分減らしたテキストを取り出す
set ocidZeroSupStr to ocidZeroSupText's substringFromIndex:nimIntLength
####戻り値
log ocidZeroSupStr as text



|

[AppleScript]小数点以下桁揃え(切り捨て)

set numMM to 3 as number

set numPt to numMM * 2.8346456693 as number
-->(*8.5039370079*)

set strPt to (numPt as text)
-->(*8.5039370079*)
set AppleScript's text item delimiters to "."
set listPt to every text item of strPt as list
set AppleScript's text item delimiters to ""
-->{8, 5039370079}
set strPtInt to text item 1 of listPt as text
-->(*8*)
set strPtDecimal to text item 2 of listPt as text
-->(*5039370079*)
set strPtDecimal to (text 1 through 2 of (strPtDecimal & "00")) as text
-->(*5*)
set numPt to ("" & strPtInt & "." & strPtDecimal & "") as number
-->(*8.5*)

|

[Padding]ゼロパディング(日時)

日付




set strCommandText to ("/bin/date +%Y%m%d") as text
set strDate to (do shell script strCommandText) as text
-->"20220401"



###年を取り出して
set numYear to (year of (current date)) as number
###テキストにして
set strYear to numYear as text


###月を取り出して
set numMonth to (month of (current date)) as number
###テキストにして
set strMonth to numMonth as text
###数値に00を足して最大4桁にして後ろの2桁取る
set strMonth to (text -2 through -1 of ("00" & strMonth)) as text


###日を取り出して
set strDays to (day of (current date)) as text
###数値に00を足して最大4桁にして後ろの2桁取る
set strDays to (text -2 through -1 of ("00" & strDays)) as text

###順番に並べる
set strDate to (strYear & strMonth & strDays) as text



時間
set strCommandText to ("/bin/date +%H%M%S") as text
set strDate to (do shell script strCommandText) as text
-->"090008"



###時間を取り出して
set numTime to (time of (current date)) as integer
###時だけ取り出し
set theHours to numTime div hours
###数値に00を足して最大4桁にして後ろの2桁取る
set theHours to (text -2 through -1 of ("00" & theHours)) as text
###分だけ取り出し
set theMinutes to (numTime - (theHours) * hours) div minutes
###数値に00を足して最大4桁にして後ろの2桁取る
set theMinutes to (text -2 through -1 of ("00" & theMinutes)) as text
###秒取り出して
set theSeconds to numTime mod minutes
###数値に00を足して最大4桁にして後ろの2桁取る
set theSeconds to (text -2 through -1 of ("00" & theSeconds)) as text
###順番に並べる
set theTime to (theHours & theMinutes & theSeconds) as text

|

[Padding]ゼロパディング(最大値指定)


サンプルコード


サンプルソース(参考)
行番号ソース
001
002###doZeroSupp(値, 最大値)
003set theZeroPaddNumber to doZeroPadd(1234, 12343)
004log theZeroPaddNumber
005theZeroPaddNumber
006
007###doZeroPadd(値, 桁数)
008on doZeroPadd(numNo, numMaxNo)
009####数字をテキストに
010set strNo to numNo as text
011####桁数を数えて
012set numCntNo to length of strNo as number
013####最大値も桁数をテキストにしておく
014set strMaxNo to numMaxNo as text
015####最大値の桁数を数えて
016set numCntMaxNo to length of strMaxNo as number
017####桁数が同じになるまで繰り返し
018repeat while numCntNo < numCntMaxNo
019###元数字の前に0を足す
020set strNo to ("0" & strNo) as text
021###桁数をカウントアップ
022set numCntNo to (numCntNo + 1) as text
023end repeat
024###桁数が最大桁数と同じになった所で0足したテキストを戻す
025return strNo
026end doZeroPadd
AppleScriptで生成しました

|

[Padding]ゼロパディング(桁数指定)


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#
005# com.cocolog-nifty.quicktimer.icefloe
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.4"
008use scripting additions
009
010
011###doZeroSupp(値, 桁数)
012set theZeoSuppNumber to doZeroPadd(1234, 5)
013log theZeoSuppNumber
014
015###doZeroPadd(値, 桁数)
016on doZeroPadd(numNo, numPadNo)
017####数字をテキストに
018set strNo to numNo as text
019####桁数を数えて
020set numCntNo to length of strNo as number
021####桁数が同じになるまで繰り返し
022repeat while numCntNo < numPadNo
023###元数字の前に0を足す
024set strNo to ("0" & strNo) as text
025###桁数をカウントアップ
026set numCntNo to (numCntNo + 1) as text
027end repeat
028###桁数が最大桁数と同じになった所で0足したテキストを戻す
029return strNo
030end doZeroPadd
AppleScriptで生成しました

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom