AppleScript Zero Suppress

[小数点]小数点いかの桁揃え


#############################
###小数点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


|

3桁ゼロサプレス

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

set numCnt to 0 as number

repeat 100 times
  set numCnt to numCnt + 1 as number
  set strZeroSup to "000" as text
  set strSeroSup to (strZeroSup & (numCnt as text)) as text
  set strSeroSup to (text -3 through -1 of strSeroSup) as text
  
  log strSeroSup
end repeat

|

整数のゼロサプレス

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]ゼロサプレス(最大値指定)

ダウンロード - e69c80e5a4a7e580a4e3818be38299e3828fe3818be3828be5a0b4e59088.scpt.zip


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# ゼロサプレス
#doZeroSupp(, 桁数)
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.4"
use scripting additions


###doZeroSupp(, 最大値)
set theZeoSuppNumber to doZeroSuppress(1234, 12343)
log theZeoSuppNumber
theZeoSuppNumber

###doZeroSupp(, 桁数)
on doZeroSuppress(numNo, numMaxNo)
####数字をテキストに
set strNo to numNo as text
####桁数を数えて
set numCntNo to length of strNo as number
####最大値も桁数をテキストにしておく
set strMaxNo to numMaxNo as text
####最大値の桁数を数えて
set numCntMaxNo to length of strMaxNo as number
####桁数が同じになるまで繰り返し
repeat while numCntNo < numCntMaxNo
###元数字の前に0を足す
set strNo to ("0" & strNo) as text
###桁数をカウントアップ
set numCntNo to (numCntNo + 1) as text
end repeat
###桁数が最大桁数と同じになった所で0足したテキストを戻す
return strNo
end doZeroSuppress

|

[Padding]ゼロサプレス(桁数指定)

ダウンロード - e6a181e695b0e68c87e5ae9a.scpt.zip


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# ゼロサプレス
#doZeroSupp(, 桁数)
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.4"
use scripting additions


###doZeroSupp(, 桁数)
set theZeoSuppNumber to doZeroSuppress(1234, 5)


###doZeroSupp(, 桁数)
on doZeroSuppress(numNo, numPadNo)
####数字をテキストに
set strNo to numNo as text
####桁数を数えて
set numCntNo to length of strNo as number
####桁数が同じになるまで繰り返し
repeat while numCntNo < numPadNo
###元数字の前に0を足す
set strNo to ("0" & strNo) as text
###桁数をカウントアップ
set numCntNo to (numCntNo + 1) as text
end repeat
###桁数が最大桁数と同じになった所で0足したテキストを戻す
return strNo
end doZeroSuppress

|

[Padding]ゼロサプレス(桁数固定)

ダウンロード - e382bbe38299e383ade382b5e38395e3829ae383ace382b9.zip

2桁のゼロサプレス

set strMon to 1

if strMon < 10 then
###文字列に00を足して最低3桁以上になるようにする
set strZeroAdd to ("00" & strMon) as text
###マイナスなので右から2番目と右から1番目を取得
set strMon to text -2 through -1 of strZeroAdd as text
(*要は
1--> 001 ---> 0"01" --> "01"
10 --> 0010 --> 00"10" --> "10"
といった処理でゼロサプレスです
*)

end if

log strMon
strMon


set listDate to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31} as list
set listTextDate to {} as list

repeat with objDay in listDate
###if判定用にこの時点では数値としておく
set strDay to objDay as number
###10より少ない場合にゼロ付与する
if strDay < 10 then
###文字列に00を足して最低3桁以上になるようにする
###この時点で数値-->テキストにする
set strZeroAdd to ("00" & strDay) as text
###マイナスなので右から2番目と右から1番目を取得
set strDay to text -2 through -1 of strZeroAdd as text
end if
###リストにしていく
set end of listTextDate to strDay
log strDay
end repeat


log listTextDate

|

その他のカテゴリー

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