SCRIPT: Get Size of Entire Folder

files_folders
applescript

#1

Continuing the discussion from How would I calculate the size of a folder (including all contents)?:

Use Case

  • Put this script in an Execute AppleScript Action.
  • It will return size (in size-appropriate units) of all items (files, folders, sub-folders) in the folder currently selected in the Finder.
  • If a Folder is NOT selected, an error msg is returned.

How To Use

  1. Select Folder in Finder
  2. Trigger this script, or macro with script in an Execute AppleScript Action.
property ptyScriptName : "Get Entire Folder Size of Finder Selection"
property ptyScriptVer : "1.1"
property ptyScriptDate : "2017-12-31"
property ptyScriptAuthor : "JMichaelTX"
# RETURNS:  | text | Folder Size in Size-Appropriate Units;  
#                           OR ERROR Msg if selection is not a folder.

use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation" -- this IS required
use scripting additions

## Some Scripts may work with Yosemite, but no guarantees ##
#  This script has been tested ONLY in macOS 10.11.6+

tell application "Finder" to set finderSelectionList to selection as alias list
set folderAlias to item 1 of finderSelectionList

set itemKind to kind of (get info for folderAlias without size)

if (itemKind ≠ "folder") then
  set scriptResults to "[ERROR]" & linefeed ¬
    & "First Finder Item Selected MUST be a FOLDER."
else
  
  --- Get Size in Bytes of ENTIRE Folder ---
  tell application "System Events" to set folderSize to size of folderAlias
  
  --- Convert Bytes to Size-Appropriate Units ---
  set folderSize to (current application's NSByteCountFormatter's stringFromByteCount:folderSize countStyle:(current application's NSByteCountFormatterCountStyleDecimal)) as text
  
  set scriptResults to folderSize
end if

return scriptResults