Archive for the 'NSFileSystemFreeSize' Category

2015/04/02 CocoaでDisk Free Space (Bytes) を求める

Shane wrote me various methods to get free space in Cocoa way. NSByteCountFormatter seems very useful.

“usedSpaceLongString” is good because they take the user’s number formatting and language into account (Shane said).

ShaneがCocoaでDiskの空き容量を(Byte単位で)返すscriptを書いてくれました(Thanks!)。NSByteCountFormatterが非常に使い勝手がありそうです。

“usedSpaceLongString”は実行時のユーザー環境の数値フォーマット形式と言語設定を反映させるため有用です(Shane談)。

あれ??? このScript、使用率が%で返ってきているものをただ文字フォーマットしているだけかも、、、

AppleScript名:CocoaでDiskSpace(Bytes)を求める
– Created 2015-04-02 by Shane Stanley
use AppleScript version “2.4″
use scripting additions
use framework “Foundation”

usedSpace(“/”)
–>  4.5772857344E+10

usedSpace2(“/”)
–>  4.5772857344E+10

usedSpaceString(“/”)
–>  ”45.77 GB”

usedSpaceLongString(“/”)
–>  ”45.77 GB (45,772,857,344 bytes)”–English user environment
–>  ”45.77 GB (45,772,857,344 バイト)”–Japanese user environment

tell application “Finder”
  free space of startup disk
end tell
–>  4.5784592712E+10

on usedSpace(volumePath)
  set theNSURL to current application’s class “NSURL”’s fileURLWithPath:volumePath –considering “ASOC in Xcode”
  
set {theResult, theSize} to theNSURL’s getResourceValue:(reference) forKey:(current application’s NSURLVolumeAvailableCapacityKey) |error|:(missing value)
  
  
return theSize as real – integer may be too big for AS
end usedSpace

on usedSpace2(volumePath)
  set fileAttr to current application’s NSFileManager’s defaultManager()’s attributesOfFileSystemForPath:volumePath |error|:(missing value)
  
  
return (fileAttr’s objectForKey:(current application’s NSFileSystemFreeSize)) as real – integer may be too big for AS
end usedSpace2

on usedSpaceString(volumePath)
  set fileAttr to current application’s NSFileManager’s defaultManager()’s attributesOfFileSystemForPath:volumePath |error|:(missing value)
  
set fRes to fileAttr’s objectForKey:(current application’s NSFileSystemFreeSize)
  
  
–Formatting
  
set sizeString to current application’s NSByteCountFormatter’s stringFromByteCount:fRes countStyle:(current application’s NSByteCountFormatterCountStyleDecimal)
  
  
return sizeString as text
end usedSpaceString

on usedSpaceLongString(volumePath)
  set fileAttr to current application’s NSFileManager’s defaultManager()’s attributesOfFileSystemForPath:volumePath |error|:(missing value)
  
set fRes to fileAttr’s objectForKey:(current application’s NSFileSystemFreeSize)
  
  
–Formatting
  
set theFormatter to current application’s NSByteCountFormatter’s alloc()’s init()
  
theFormatter’s setCountStyle:(current application’s NSByteCountFormatterCountStyleDecimal)
  
theFormatter’s setIncludesActualByteCount:true
  
set sizeString to theFormatter’s stringFromByteCount:fRes
  
  
return sizeString as text
end usedSpaceLongString

★Click Here to Open This Script 

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