[小数点]小数点いかの桁揃え
#############################
###小数点2桁にテキスト整形する
#############################
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setMinimumFractionDigits:(2)
ocidFormatter's setMaximumFractionDigits:(2)
set strSizeWidthMm2Digi to (ocidFormatter's stringFromNumber:(numSizeWidthMm)) as text
set strSizeHeightMm2Digi to (ocidFormatter's stringFromNumber:(numSizeHeightMm)) as text
log strSizeWidthMm2Digi as text
log strSizeHeightMm2Digi as text
#!/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
####ドキュメントを開いているか?チェック
tell application "Preview"
set numWindow to (count of window) as integer
if numWindow = 0 then
log "ドキュメントを開いていません"
##前面に出す
set strAppName to (name of current application) as text
if strAppName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
display alert "エラー:Windowがありません" buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 2
return "ドキュメントを開いていません"
end if
set numDoc to (count of every document) as integer
if numDoc = 0 then
log "ドキュメントを開いていません"
##前面に出す
set strAppName to (name of current application) as text
if strAppName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
display alert "エラー:Documentを開いていません" buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 2
return "ドキュメントを開いていません"
end if
end tell
####
tell application "Preview"
set boolFrontMost to frontmost as boolean
if boolFrontMost = false then
activate
end if
tell front document
set strFilePath to path as text
set strName to name as text
end tell
end tell
####
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
##種類判定(PDFを除外する)
set aliasFilePath to (ocidFilePathURL's absoluteURL()) as alias
tell application "Finder"
tell file aliasFilePath
set strKind to kind as text
end tell
end tell
###
if (strKind contains "画像") is false then
log "画像ファイルではありません"
##前面に出す
set strAppName to (name of current application) as text
if strAppName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
display alert "エラー:画像ファイルではありません" buttons {"OK", "キャンセル"} default button "OK" as informational giving up after 2
return "画像ファイルではありません"
end if
###########################
##NSIMAGEに読み込む
set ocidReadImage to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFilePathURL))
##サイズ取得 この時点はポイント単位
set recordImageSize to ocidReadImage's |size|()
log recordImageSize as record
set numSizeWidthPt to (width of recordImageSize) as integer
set numSizeHeightPt to (height of recordImageSize) as integer
###サイズ計算 ここでmmに変換
set numPoint2mm to 0.352778 as number
set numSizeWidthMm to (numSizeWidthPt * numPoint2mm) as number
set numSizeHeightMm to (numSizeHeightPt * numPoint2mm) as number
#############################
###小数点2桁にテキスト整形する
#############################
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setMinimumFractionDigits:(2)
ocidFormatter's setMaximumFractionDigits:(2)
set strSizeWidthMm2Digi to (ocidFormatter's stringFromNumber:(numSizeWidthMm)) as text
set strSizeHeightMm2Digi to (ocidFormatter's stringFromNumber:(numSizeHeightMm)) as text
log strSizeWidthMm2Digi as text
log strSizeHeightMm2Digi as text
##BitMapRepに変換
set ocidReadImageRepArray to ocidReadImage's representations()
set ocidReadImageRep to (ocidReadImageRepArray's objectAtIndex:0)
##ピクセルサイズ取得
set numPixelsWidth to ocidReadImageRep's pixelsWide()
set numPixelsHeight to ocidReadImageRep's pixelsHigh()
log numPixelsWidth
log numPixelsHeight
##解像度計算
set strResolution to ((numPixelsWidth / numSizeWidthPt) * 72) as text
log strResolution
###########################
##ダイアログ用テキスト
set strDefaultAnswer to ("ファイル名 : " & strName & "\nパス : " & strFilePath & "\n解像度 : " & strResolution & " ppi\n縦mm : " & numSizeHeightMm & "\n横mm : " & numSizeWidthMm & "\n横x縦mm : " & strSizeWidthMm2Digi & "x" & strSizeHeightMm2Digi & "\n横x縦pt : " & numSizeWidthPt & "x" & numSizeHeightPt & "\n横x縦px : " & numPixelsWidth & "x" & numPixelsHeight & "\n") as text
##############################
#####ダイアログ
##############################
##前面に出す
set strAppName to (name of current application) as text
if strAppName is "osascript" then
tell application "Finder" to activate
else
tell current application to activate
end if
###アイコンパス
set aliasIconPath to POSIX file "/System/Applications/Preview.app/Contents/Resources/AppIcon.icns" as alias
set strMes to ("ファイル名 : " & strName & "\nパス : " & strFilePath & "") as text
set recordResult to (display dialog strMes with title "選んでください" default answer strDefaultAnswer buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer)
###クリップボードコピー
if button returned of recordResult is "クリップボードにコピー" then
set strText to (text returned of recordResult) as text
try
####ペーストボード宣言
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 strText as text
end tell
end try
end if
#!/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.335 as number
##指定無し-->だいたい四捨五入される
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setMinimumFractionDigits:(2)
ocidFormatter's setMaximumFractionDigits:(2)
set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
log ocidDecStr as text
-->(*1.34*)
set numNo to 1.331 as number
#NSNumberFormatterRoundCeiling 切り上げ
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundCeiling)
ocidFormatter's setMinimumFractionDigits:(2)
ocidFormatter's setMaximumFractionDigits:(2)
set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
log ocidDecStr as text
-->(*1.34*)
set numNo to 1.331 as number
#NSNumberFormatterRoundFloor 切り捨て
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundFloor)
ocidFormatter's setMinimumFractionDigits:(2)
ocidFormatter's setMaximumFractionDigits:(2)
set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
log ocidDecStr as text
-->(*1.33*)
set numNo to 1.335 as number
#NSNumberFormatterRoundHalfDown 5捨6入
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundHalfDown)
ocidFormatter's setMinimumFractionDigits:(2)
ocidFormatter's setMaximumFractionDigits:(2)
set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
log ocidDecStr as text
-->(*1.33*)
set numNo to 1.334 as number
#NSNumberFormatterRoundHalfUp 4捨5入
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundHalfUp)
ocidFormatter's setMinimumFractionDigits:(2)
ocidFormatter's setMaximumFractionDigits:(2)
set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
log ocidDecStr as text
-->(*1.34*)
set numNo to 1.326 as number
#NSNumberFormatterRoundHalfEven
set ocidFormatter to refMe's NSNumberFormatter's alloc()'s init()
ocidFormatter's setNumberStyle:(refMe's NSNumberFormatterDecimalStyle)
ocidFormatter's setRoundingMode:(refMe's NSNumberFormatterRoundHalfEven)
ocidFormatter's setMinimumFractionDigits:(2)
ocidFormatter's setMaximumFractionDigits:(2)
set ocidDecStr to (ocidFormatter's stringFromNumber:(numNo))
log ocidDecStr as text
-->(*1.32*)
return
| 固定リンク
「NSNumber」カテゴリの記事
- NSNumberのValueの考え方(2024.09.03)
- [AppleScript]負の数値を整数にする(2024.09.02)
- [NSDecimalNumber]計算にNSDecimalNumberを使う(2024.03.26)
- [小数点]小数点いかの桁揃え(2023.11.13)
- [integerValue]as integerを使わない場合(2023.01.12)