Home Archives
 

Today's Date

Tuesday February 07, 2012

Friends & Family

Stock Quotes

Latest Joy of Tech

Latest Joy of Tech!
An Applet to Measure Resource Forks Print E-mail
Saturday, 03 June 2006 13:31

The Get Info window in Mac OS X 10.4 does not properly report the size of a file. It rounds and it doesn't report the size of a file's resource fork.

Neither is the ls -l command from the command-line perfectly accurate -- it only reports the size of the data fork.

Apple has provided a way to check the size of the resource fork of a file from the command line, by adding /..namedfork/rsrc at the end of the ls -l command, but it feels ridiculous to resort to the command line to see the actual file size.

And this is an issue on more situations than you might realize. Lots of programs still add resource forks to files, including MS Office, Photoshop, Quark and Illustrator. The Finder adds a resource fork to a file when you paste a custom icon onto it. Text-clippings and alias files are resource-fork files.

So this is a script that I kicked out last week to accurately report the size of any file that's dropped on it (to 3 decimal places). Only files. Not folders. Not volumes. Not applications (which are usually actually folders under OS X). And not Alias files, even though they are resource files -- Applescript droplets automatically resolve alias files so there's no easy way to do that. I may tweak it to measure the size of folders at some point in the future.

Here's what the output should look like:

FileSizer.jpg

Copy and paste the script into a new document in the Script Editor and save it as an application. To see what a file's real size is, just drop the file onto the Application icon.

on open dropped_file
set file_info to info for dropped_file
set dropped_file to dropped_file as alias
set path_to_file to quoted form of (POSIX path of dropped_file)

if kind of file_info is "Folder" then error "This applet only works with document files!"
if kind of file_info is "Volume" then error "This applet only works with document files!"
if kind of file_info contains "Application" then error "This applet only works with document files!"

-- Get File Size of RSRC and DATA Forks
-- ================================
set file_info_RSRC to (do shell script "ls -l " & path_to_file & "/..namedfork/rsrc") as string
set file_info_DATA to (do shell script "ls -l " & path_to_file & "/..namedfork/data") as string
-- ================================

-- Get File Size of RSRC fork in MB, K or B
-- ================================
set AppleScript's text item delimiters to {" "}
set file_sizeRSRC to (text item 9 of file_info_RSRC) as integer
set AppleScript's text item delimiters to {""}
if file_sizeRSRC < 1 then
set file_sizeRSRC_K to "0" as text
else
if (file_sizeRSRC ≥ 1 and file_sizeRSRC < 1024) then
set file_sizeRSRC_K to (round (file_sizeRSRC * 1000 / 8)) / 1000 & "b" as text
else
if (file_sizeRSRC ≥ 1024 and file_sizeRSRC < 1048567) then
set file_sizeRSRC_K to (round (file_sizeRSRC * 1000 / 1024)) / 1000 & "K" as text
else
if (file_sizeRSRC ≥ 1048567) then
set file_sizeRSRC_K to (round (file_sizeRSRC * 1000 / 1024 / 1024)) / 1000 & "MB" as text
end if
end if
end if
end if
-- END of the File Size RSRC routine
-- ================================

-- Get File Size of DATA fork in MB, K or B
-- ================================
set AppleScript's text item delimiters to {" "}
set file_sizeDATA to (text item 9 of file_info_DATA) as integer
set AppleScript's text item delimiters to {""}
if file_sizeDATA < 1 then
set file_sizeDATA_K to "0" as text
else
if (file_sizeDATA ≥ 1 and file_sizeDATA < 1024) then
set file_sizeDATA_K to (round (file_sizeDATA * 1000 / 8)) / 1000 & "b" as text
else
if (file_sizeDATA ≥ 1024 and file_sizeDATA < 1048567) then
set file_sizeDATA_K to (round (file_sizeDATA * 1000 / 1024)) / 1000 & "K" as text
else
if file_sizeDATA ≥ 1048567 then
set file_sizeDATA_K to (round (file_sizeDATA * 1000 / 1024 / 1024)) / 1000 & "MB" as text
end if
end if
end if
end if
-- END of the File Size DATA routine
-- ================================


-- Get File Size in MB, K or B
-- ================================
set AppleScript's text item delimiters to {" "}
set file_size to (file_sizeRSRC + file_sizeDATA) as integer
set AppleScript's text item delimiters to {""}
if file_size < 1 then
set file_size_K to "0" as text
else
if (file_size ≥ 1 and file_size < 1024) then
set file_size_K to (round (file_size * 1000 / 8)) / 1000 & "b" as text
else
if (file_size ≥ 1024 and file_size < 1048567) then
set file_size_K to (round (file_size * 1000 / 1024)) / 1000 & "K" as text
else
if (file_size ≥ 1048567) then
set file_size_K to (round (file_size * 1000 / 1024 / 1024)) / 1000 & "MB" as text
end if
end if
end if
end if
-- END of the File Size routine
-- ================================


display dialog "Name: " & (name of file_info) & "
" & "
" & "Type: " & (file type of file_info) & " Creator: " & (file creator of file_info) & "
" & "
" & "Size: " & file_size_K & "
" & "
" & "Data: " & file_sizeDATA_K & " Resource: " & file_sizeRSRC_K & "
" & "
" & "Empty type and creator fields are normal."

end open

Comments

No comments have been added yet. Be the first to comment...

Add a New Comment

 
 
 
 
 
 
 
Joomla Templates by Joomlashack