AppleScript Math

奇数偶数判定


AppleScript サンプルコード

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

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003# com.cocolog-nifty.quicktimer.icefloe
004#
005#
006----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
007use AppleScript version "2.8"
008use scripting additions
009
010
011#奇数偶数判定
012#値が整数の場合
013set numCnkNo to 1234567 as integer
014
015set numChkPageOddEven to (numCnkNo mod 2) as integer
016if numChkPageOddEven = 1 then
017  log "奇数"
018else if numChkPageOddEven = 0 then
019  log "偶数"
020end if
021
022
023set numCnkNo to 12345678 as integer
024
025set numChkPageOddEven to (numCnkNo mod 2) as integer
026if numChkPageOddEven = 1 then
027  log "奇数"
028else if numChkPageOddEven = 0 then
029  log "偶数"
030end if
031
032
033#値が小数点以下の値を持っている場合
034set numCnkNo to 11.7 as number
035
036set numChkPageOddEven to (round of (numCnkNo mod 2) rounding down) as integer
037if numChkPageOddEven = 1 then
038  log "奇数"
039else if numChkPageOddEven = 0 then
040  log "偶数"
041end if
042
043
044set numCnkNo to 12.7 as number
045
046set numChkPageOddEven to (round of (numCnkNo mod 2) rounding down) as integer
047if numChkPageOddEven = 1 then
048  log "奇数"
049else if numChkPageOddEven = 0 then
050  log "偶数"
051end if
052
AppleScriptで生成しました

|

小数点以下桁揃え

計算する場合

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




log doRound2Dec({42.56834, 2, "up"})
log doRound2Dec({42.56894, 3, "down"})

##########################################
# 小数点以下桁揃え  数値 桁数 四捨五入 up or down
##########################################
to doRound2Dec({argNumNumber, argNumDecNo, argStrRound})
  set numNumber to argNumNumber as number
  set numDecNo to argNumDecNo as integer
  set strRound to argStrRound as text
  # 10のx乗 
  set numDec to (10 ^ numDecNo) as integer
  # ↑の数桁数だけ元の数値の桁数を増やして
  set numStep1 to (numNumber * numDec) as number
  # 数値の丸め 繰り上がりup と繰り下がりdownの処理
  if strRound is "up" then
    set numStep2 to (round of numStep1 rounding up) as integer
  else if strRound is "down" then
    set numStep2 to (round of numStep1 rounding down) as integer
  end if
  # 最後の桁数を戻して
  set numStep3 to (numStep2 / numDec) as number
  # テキストで戻す場合
  # set strStep3 to (numStep2 / numDec) as text
  # 数値で戻す
return numStep3
end doRound2Dec

テキストとして処理する場合

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

set numNumber to 1024.126683 as number

log doRound2Dec(numNumber)

to doRound2Dec(argNumber)
  set numNumber to argNumber as number
  set strNumber to numNumber as text
  set AppleScript's text item delimiters to "."
  set listArgNumbar to every text item of strNumber as list
  set AppleScript's text item delimiters to ""
  #
  set numIntNo to text item 1 of listArgNumbar as integer
  set strIntNo to numIntNo as text
  #
  if (count of listArgNumbar) = 1 then
    set strDecNo to "00" as text
  else
    set numDec to numNumber - numIntNo as number
    set strDecNo to text item 2 of listArgNumbar as text
  end if
  set strDecimal to (text 1 through 2 of (strDecNo & "00")) as text
  #
  set strReturnNo to (strIntNo & "." & strDecimal) as text
  set strReturnNo to strReturnNo as text
return strReturnNo
end doRound2Dec

|

ファイルサイズ表記


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


log doGetFileSize(6410)
log doGetFileSize("916,564,470")
log doGetFileSize("1,504,749,906")

####################################
# ファイルサイズ表記 入力は小数点の無いバイト
####################################
to doGetFileSize(argNumByteSize)
  #念の為カンマを取る
  set strNumber to doDelComma2Text(argNumByteSize) as text
  #受け取り値を数値で確定
  set numFileSize to strNumber as integer
  # バイト単位 10.5以前やWindowsターゲットの場合は1024に
  set numByteUnits to 1000 as integer
  # 単位分岐
  if numFileSize < numByteUnits then
    # log "b:バイト単位"
    set strFileSize to doRound2Dec(numFileSize) as text
    set strFileSize to (strFileSize & " B") as text
  else if numFileSize < (numByteUnits * numByteUnits) then
    # log "kb:キロバイト単位"
    set strFileSize to doRound2Dec(numFileSize / numByteUnits) as text
    set strFileSize to (strFileSize & " KB") as text
  else if numFileSize < (numByteUnits * numByteUnits * numByteUnits) then
    # log "mb:メガバイト単位"
    set strFileSize to doRound2Dec(numFileSize / (numByteUnits * numByteUnits)) as text
    set strFileSize to (strFileSize & " MB") as text
  else if numFileSize < (numByteUnits * numByteUnits * numByteUnits * numByteUnits) then
    # log "gb:ギガバイト単位"
    set strFileSize to doRound2Dec(numFileSize / (numByteUnits * numByteUnits * numByteUnits)) as text
    set strFileSize to (strFileSize & " GB") as text
  end if
return strFileSize
end doGetFileSize

####################################
# 小数点以下桁数
####################################
to doRound2Dec(argNumber)
  ## 100で小数点2位 10で1位 1000で3位まで計算
  set numDec to 100 as integer
  set strDecNo to ((round (argNumber * numDec)) / numDec) as text
return strDecNo
end doRound2Dec

####################################
# 数値のカンマ表記を取る
####################################
to doDelComma2Text(argStrNumber)
  set strNumber to argStrNumber as text
  set AppleScript's text item delimiters to ","
  set listDelim to every text item of strNumber
  set AppleScript's text item delimiters to ""
  set strNumber to (listDelim) as text
return strNumber
end doDelComma2Text



|

小数点以下2桁を数値で


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

set numVar to 72 as number
set numIndex to 1.3426789 as number

###小数点以下2桁 切り捨て
set numStep1 to (numVar * numIndex) as number
log numStep1
set numStep2 to numStep1 * 100 as number
log numStep2
set intStep3 to (round of numStep2 rounding down) as integer
log intStep3
set numStep4 to intStep3 / 100 as number
log numStep4
-->(*96.67*)

###小数点3桁以降切り捨て式
set numAns to ((round of ((numVar * numIndex) * 100) rounding down) / 100) as number
log numAns
--> (*96.67*)

###小数点3桁以降切り上げ式
set numAns to ((round of ((numVar * numIndex) * 100) rounding up) / 100) as number
log numAns
--> (*96.68*)

###ままの値
set numStep1 to (numVar * numIndex) as number
log numStep1
--> (*96.6728808*)

|

[Decimal to Octal]10進数→8進数 (パーミンション番号用)


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

#!/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.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


log doOct2Dec(770)
log doDec2Oct(493)

###################################
## パーミッション 8進→10進
## Octal to Decimal
###################################

to doOct2Dec(argOctNo)
  set strOctalText to argOctNo as text
  set num3Line to (first item of strOctalText) as number
  set num2Line to (2nd item of strOctalText) as number
  set num1Line to (last item of strOctalText) as number
  set numDecimal to (num3Line * 64) + (num2Line * 8) + (num1Line * 1) as number
return numDecimal as number
end doOct2Dec

###################################
## パーミッション10進数 →8進数  
## Decimal to Octal
###################################
to doDec2Oct(argDecNo)
  set numDecNo to argDecNo as number
  set numDiv1 to (numDecNo div 8) as number
  set numMod1 to (numDecNo mod 8) as number
  set numDiv2 to (numDiv1 div 8) as number
  set numMod2 to (numDiv1 mod 8) as number
  set numDiv3 to (numDiv2 div 8) as number
  set numMod3 to (numDiv2 mod 8) as number
  set strOctal to (numMod3 & numMod2 & numMod1) as text
  set numOctal to strOctal as number
return numOctal as number
  
end doDec2Oct





|

ランダム番号生成


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

set strStartNo to "10000000" as text
set strEndNo to "99999999" as text
set theParagraph to (random number from (strStartNo as integer) to (strEndNo as integer)) as integer


|

mod 割り算のあまり

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


set numCntLine to 0 as integer
set numCntRow to 0 as integer

repeat 25 times
  log numCntLine
  log numCntRow
  set numCntLine to numCntLine + 1 as integer
  ###5で割ってあまりが0の時だけ処理
  if (numCntLine mod 5) = 0 then
    set numCntRow to numCntRow + 1 as integer
  end if
end repeat

|

[8進数]8進数を10進数に

他にいい方法が思いつかない…
出来そうなのになぁ

#!/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 strOctalText to "777" as text

set num3Line to first item of strOctalText as number
set num2Line to 2nd item of strOctalText as number
set num1Line to last item of strOctalText as number

set numDecimal to (num3Line * 64) + (num2Line * 8) + (num1Line * 1)

|

[四捨五入]小数点以下の桁数指定

set numDecNo to "123.45678" as number

####小数点以下1桁
set strDecNo to ((round (numDecNo * 10)) / 10) as text
-->"123.5"

####小数点以下2桁
set strDecNo to ((round (numDecNo * 100)) / 100) as text
-->"123.46"

####小数点以下3桁
set strDecNo to ((round (numDecNo * 1000)) / 1000) as text
-->"123.457"

|

[四捨五入]小数点以下の桁数(テキストとして扱う)

#!/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.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

set numDecNo to "123.45678" as text

set ocidDecimalNumber to (refMe's NSDecimalNumber's decimalNumberWithString:numDecNo)
log ocidDecimalNumber as text
log className() of ocidDecimalNumber as text

###NSNumberFormatterの初期化
set ocidNSNumberFormatter to refMe's NSNumberFormatter's alloc()'s init()
log className() of ocidNSNumberFormatter as text
###小数点以下2桁のフォーマット
(ocidNSNumberFormatter's setPositiveFormat:"##0.00")
####フォーマットを適応
set ocidDecimalNumber to (ocidNSNumberFormatter's stringFromNumber:ocidDecimalNumber)
log ocidDecimalNumber as text
log className() of ocidDecimalNumber as text
log ocidDecimalNumber as text
-->(*123.46*)

|

より以前の記事一覧

その他のカテゴリー

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