Archive for the 'NSDecimalNumber' Category

2015/04/01 DiskSpaceを求める

These scripts get free space of storage. Finder’s “free space” command returns specified disk’s free space by bytes. Cocoa’s NSFileSystemFreeSize return percentage of used space. I like the former. But ASOC in Xcode, sometimes we can not use Finder command. So I checked the cocoa way.

通常のAppleScriptとASOCのscriptでディスクの空きを求めるものです。Finderの「free space」コマンドは指定ディスクの空き容量をバイト単位で返します。CocoaのNSFileSystemFreeSizeは使用ずみ容量をパーセントで返します。どちらかといえば、前者の方が好みですが・・・ASOC in XcodeではFinderのコマンドが通じないケースが出てくるため、Cocoa風のやり方を調べておきました。

AppleScript名:Pure ASでDiskSpace(GBytes)を求める
– Created 2015-04-01 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions

tell application “Finder”
  set aFree to (free space of startup disk) / 1.0E+9
end tell
–>  102.829194753

★Click Here to Open This Script 

AppleScript名:CocoaでDiskSpace(%)を求める
– Created 2015-04-01 by Takaaki Naganoya
– 2015 Piyomaru Software
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

set aPath to current application’s NSString’s stringWithString:“/”
set fileAttr to current application’s NSFileManager’s defaultManager()’s attributesOfFileSystemForPath:aPath |error|:(missing value)
set fRes to (fileAttr’s objectForKey:(current application’s NSFileSystemFreeSize)) as string

set aDecNum to current application’s NSDecimalNumber’s decimalNumberWithString:fRes
set aFreeNum to aDecNum’s decimalNumberByDividingBy:(current application’s NSDecimalNumber’s decimalNumberWithString:“1000000000″) –”G” Bytes for Storage
set bFreeNum to aFreeNum as real
–> 84.058387756348

★Click Here to Open This Script