AppleScript NSNumber

[NSDecimalNumber]計算にNSDecimalNumberを使う


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

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

property refMe : a reference to current application


set the listRGB16bitColor to (choose color default color {50, 50, 50}) as list

###戻り値は16bitの整数をテキストに
set str16bitR to (item 1 of listRGB16bitColor) as text
set str16bitG to (item 2 of listRGB16bitColor) as text
set str16bitB to (item 3 of listRGB16bitColor) as text
###NSDecimalNumberに変換
set ocidDecR to refMe's NSDecimalNumber's alloc()'s initWithString:(str16bitR)
set ocidDecG to refMe's NSDecimalNumber's alloc()'s initWithString:(str16bitG)
set ocidDecB to refMe's NSDecimalNumber's alloc()'s initWithString:(str16bitB)
###割り算
set ocid16bit to refMe's NSDecimalNumber's alloc()'s initWithString:("65535")

set ocidFloatR to (ocidDecR's decimalNumberByDividingBy:(ocid16bit))
set ocidFloatG to (ocidDecG's decimalNumberByDividingBy:(ocid16bit))
set ocidFloatB to (ocidDecB's decimalNumberByDividingBy:(ocid16bit))

log ocidFloatR's stringValue() as text
###小数点4位まで
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundFloor)
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setMaximumFractionDigits:(10)
set ocidFloat4R to ocidFormatter's stringFromNumber:(ocidFloatR)
set ocidFloat4G to ocidFormatter's stringFromNumber:(ocidFloatG)
set ocidFloat4B to ocidFormatter's stringFromNumber:(ocidFloatB)
log ocidFloat4R as text

###掛け算
set ocid8bit to refMe's NSDecimalNumber's alloc()'s initWithString:("255")

set ocid8BitR to (ocidFloatR's decimalNumberByMultiplyingBy:(ocid8bit))
set ocid8BitG to (ocidFloatG's decimalNumberByMultiplyingBy:(ocid8bit))
set ocid8BitB to (ocidFloatB's decimalNumberByMultiplyingBy:(ocid8bit))

log ocid8BitR's stringValue() as text
###小数点以下切り捨て
(*  
NSNumberFormatterRoundCeiling
NSNumberFormatterRoundFloor
NSNumberFormatterRoundDown
NSNumberFormatterRoundUp
NSNumberFormatterRoundHalfEven
NSNumberFormatterRoundHalfDown
NSNumberFormatterRoundHalfUp
*)
#切り捨て
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundUp)
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setMaximumFractionDigits:(0)
set str8BitStrR to (ocidFormatter's stringFromNumber:(ocid8BitR)) as text
set str8BitStrG to (ocidFormatter's stringFromNumber:(ocid8BitG)) as text
set str8BitStrB to (ocidFormatter's stringFromNumber:(ocid8BitB)) as text


set strRhex to getHex(str8BitStrR) as text
set strGhex to getHex(str8BitStrG) as text
set strBhex to getHex(str8BitStrB) as text


#を付けてHTML用のHEXカラーコードに
set strHex to ("#" & strRhex & strGhex & strBhex) as text
set strRGBA to ("-color-rgba(" & str8BitStrR & ", " & str8BitStrG & ", " & str8BitStrB & ", " & 0 & ")") as text

set strDisptext to (strHex & "\n" & strRGBA) as text
#RGBカラー値を整形
set theRGB8bitColor to ("R:" & str8BitStrR & " G:" & str8BitStrG & " B:" & str8BitStrB & "\n" & strHex & "\n")

##############################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if

###ダイアログ
set aliasIconPath to POSIX file "/System/Applications/Utilities/Digital Color Meter.app/Contents/Resources/AppIcon.icns" as alias
set strTitle to ("コードをコピーしてください") as text
set strMes to ("" & theRGB8bitColor & "Colorが\n出来ました\nコピーして使って下さい。") as text
set recordResult to (display dialog strMes default answer the strDisptext with icon aliasIconPath with title strTitle buttons {"クリップボードにコピー", "OK", "終了"} default button "OK" cancel button "終了" giving up after 20 without hidden answer) as record
###クリップボードコピー
if button returned of recordResult is "クリップボードにコピー" then
  try
    set strText to text returned of recordResult as text
    ####ペーストボード宣言
    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
    set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
  on error
    tell application "Finder"
      set the clipboard to strTitle as text
    end tell
  end try
end if

##############################
#サブ
##############################
to getHex(arg8BitNum)
  set record8Bit2Hex to {|0|:"00", |1|:"01", |2|:"02", |3|:"03", |4|:"04", |5|:"05", |6|:"06", |7|:"07", |8|:"08", |9|:"09", |10|:"0A", |11|:"0B", |12|:"0C", |13|:"0D", |14|:"0E", |15|:"0F", |16|:"10", |17|:"11", |18|:"12", |19|:"13", |20|:"14", |21|:"15", |22|:"16", |23|:"17", |24|:"18", |25|:"19", |26|:"1A", |27|:"1B", |28|:"1C", |29|:"1D", |30|:"1E", |31|:"1F", |32|:"20", |33|:"21", |34|:"22", |35|:"23", |36|:"24", |37|:"25", |38|:"26", |39|:"27", |40|:"28", |41|:"29", |42|:"2A", |43|:"2B", |44|:"2C", |45|:"2D", |46|:"2E", |47|:"2F", |48|:"30", |49|:"31", |50|:"32", |51|:"33", |52|:"34", |53|:"35", |54|:"36", |55|:"37", |56|:"38", |57|:"39", |58|:"3A", |59|:"3B", |60|:"3C", |61|:"3D", |62|:"3E", |63|:"3F", |64|:"40", |65|:"41", |66|:"42", |67|:"43", |68|:"44", |69|:"45", |70|:"46", |71|:"47", |72|:"48", |73|:"49", |74|:"4A", |75|:"4B", |76|:"4C", |77|:"4D", |78|:"4E", |79|:"4F", |80|:"50", |81|:"51", |82|:"52", |83|:"53", |84|:"54", |85|:"55", |86|:"56", |87|:"57", |88|:"58", |89|:"59", |90|:"5A", |91|:"5B", |92|:"5C", |93|:"5D", |94|:"5E", |95|:"5F", |96|:"60", |97|:"61", |98|:"62", |99|:"63", |100|:"64", |101|:"65", |102|:"66", |103|:"67", |104|:"68", |105|:"69", |106|:"6A", |107|:"6B", |108|:"6C", |109|:"6D", |110|:"6E", |111|:"6F", |112|:"70", |113|:"71", |114|:"72", |115|:"73", |116|:"74", |117|:"75", |118|:"76", |119|:"77", |120|:"78", |121|:"79", |122|:"7A", |123|:"7B", |124|:"7C", |125|:"7D", |126|:"7E", |127|:"7F", |128|:"80", |129|:"81", |130|:"82", |131|:"83", |132|:"84", |133|:"85", |134|:"86", |135|:"87", |136|:"88", |137|:"89", |138|:"8A", |139|:"8B", |140|:"8C", |141|:"8D", |142|:"8E", |143|:"8F", |144|:"90", |145|:"91", |146|:"92", |147|:"93", |148|:"94", |149|:"95", |150|:"96", |151|:"97", |152|:"98", |153|:"99", |154|:"9A", |155|:"9B", |156|:"9C", |157|:"9D", |158|:"9E", |159|:"9F", |160|:"A0", |161|:"A1", |162|:"A2", |163|:"A3", |164|:"A4", |165|:"A5", |166|:"A6", |167|:"A7", |168|:"A8", |169|:"A9", |170|:"AA", |171|:"AB", |172|:"AC", |173|:"AD", |174|:"AE", |175|:"AF", |176|:"B0", |177|:"B1", |178|:"B2", |179|:"B3", |180|:"B4", |181|:"B5", |182|:"B6", |183|:"B7", |184|:"B8", |185|:"B9", |186|:"BA", |187|:"BB", |188|:"BC", |189|:"BD", |190|:"BE", |191|:"BF", |192|:"C0", |193|:"C1", |194|:"C2", |195|:"C3", |196|:"C4", |197|:"C5", |198|:"C6", |199|:"C7", |200|:"C8", |201|:"C9", |202|:"CA", |203|:"CB", |204|:"CC", |205|:"CD", |206|:"CE", |207|:"CF", |208|:"D0", |209|:"D1", |210|:"D2", |211|:"D3", |212|:"D4", |213|:"D5", |214|:"D6", |215|:"D7", |216|:"D8", |217|:"D9", |218|:"DA", |219|:"DB", |220|:"DC", |221|:"DD", |222|:"DE", |223|:"DF", |224|:"E0", |225|:"E1", |226|:"E2", |227|:"E3", |228|:"E4", |229|:"E5", |230|:"E6", |231|:"E7", |232|:"E8", |233|:"E9", |234|:"EA", |235|:"EB", |236|:"EC", |237|:"ED", |238|:"EE", |239|:"EF", |240|:"F0", |241|:"F1", |242|:"F2", |243|:"F3", |244|:"F4", |245|:"F5", |246|:"F6", |247|:"F7", |248|:"F8", |249|:"F9", |250|:"FA", |251|:"FB", |252|:"FC", |253|:"FD", |254|:"FE", |255|:"FF"} as record
  
  set ocid8Bit2HexDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocid8Bit2HexDict's setDictionary:(record8Bit2Hex)
  set str8BitNum to arg8BitNum as string
  set ocidValue to (ocid8Bit2HexDict's valueForKey:(str8BitNum))
  set strValue to ocidValue as text
return strValue
end getHex

|

[integerValue]as integerを使わない場合

#!/usr/bin/env osascript
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
set ocidWidth to (refMe's NSNumber's numberWithInteger:1280)'s integerValue
set ocidHeight to (refMe's NSNumber's numberWithInteger:720)'s integerValue
set ocidXDimension to (refMe's NSNumber's numberWithInteger:0)'s integerValue
set ocidYDimension to (refMe's NSNumber's numberWithInteger:0)'s integerValue

|

[boolValue] as booleanを使わない場合


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

#!/usr/bin/env osascript
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


set ocidFalse to (refMe's NSNumber's numberWithBool:false)
-->false

set ocidTrue to (refMe's NSNumber's numberWithBool:true)
-->true

set ocidFalse to (refMe's NSNumber's numberWithBool:false)'s boolValue
-->false

set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s boolValue
-->true

set ocidTrue to (refMe's NSNumber's numberWithBool:false)'s integerValue
-->0

set ocidTrue to (refMe's NSNumber's numberWithBool:true)'s integerValue
-->1

|

[NSNumberFormatter] NSDecimalNumber 小数点以下の桁揃え

#!/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 objMe : a reference to current application
property objNSString : a reference to objMe's NSString

####小数点以下の数字をテキストにしておいて
set numNumber to 29.970029830933 as text

####NSDecimalNumber
set ocidDecimalNumber to objMe's NSDecimalNumber's decimalNumberWithString:numNumber
log ocidDecimalNumber as text
log className() of ocidDecimalNumber as text

###NSNumberFormatterの初期化
set ocidNSNumberFormatter to objMe's NSNumberFormatter's alloc()'s init()
log ocidNSNumberFormatter
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

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes 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 Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray 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 NSFont AppleScript NSFontManager 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 NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange 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 NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events 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 Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV 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 SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom