File Size Converter

This script will convert a file size from bytes (like what is returned from the size element of a “get info for” record) to KB, MB, GB, or TB.

OS version: Any

property base_file_size : 1024 --some would use 1000

set the_size to 10000 --a size in bytes like what is retruned by "get info for"
--set the_size to (size of (get info for (choose file)))
return my get_file_size(the_size)

on get_file_size(the_size)
	if the_size > (base_file_size ^ 4) then
		set {div_1, the_unit} to {(base_file_size ^ 4), "TB"}
	else if the_size is greater than or equal to (base_file_size ^ 3) then
		set {div_1, the_unit} to {(base_file_size ^ 3), "GB"}
	else if the_size is greater than or equal to (base_file_size ^ 2) then
		set {div_1, the_unit} to {(base_file_size ^ 2), "MB"}
	else if the_size is greater than or equal to base_file_size then
		set {div_1, the_unit} to {base_file_size, "KB"}
	else
		set {div_1, the_unit} to {1, "B"}
	end if
	set the_size to (((the_size div div_1) & "." & ((the_size mod div_1) div (div_1 / 10)) as string) as real) as string
	if the_size ends with ".0" then set the_size to (text 1 thru -3 of the_size)
	return (the_size & " " & the_unit as string)
end get_file_size