Friday, July 19, 2019

#1 2018-09-17 11:55:03 pm

From:: Cayman Islands
Registered: 2018-08-07
Posts: 2

Selected Items Size AppleScript

Hello, I made this script because sometimes I need to copy some folders and files to a removable media drive, USB, FireWire, etc, and I want to know exactly how much is the free space required at the target/destination drive before to start the copy task, with this script I only have to select all the desired items to copy and run it, it will show you the total size of all the selected items in human readable units the same as finder does, it supposes to be able to handle sizes from bytes up to PB but I only have been able to test it with few TB big_smile so if someone else can verify that and let me know it would be great, you can localize the text on dialogs/buttons and make it match with your system language.


#Selected Items Size AppleScript
#2018 (ɔ) jraulc at gmail dot com

set localeString to user locale of (get system info)

#you can localize to your language here
if localeString starts with "es" then
   set okButton to "Aceptar"
   set noSelectionString to "Por favor seleccione al menos dos items para calcular su tamaño."
   set totalSizeString to "Tamaño total seleccionado: "
   set selectedItemsString to " items seleccionados en: " & return
   set reallyString to "Es en serio?" & return
   set tooBigString to "Es en serio?" & return & return & "Solo puedo decir esto 'El tamaño total de los items seleccionados es mayor a 1 ExaByte.'"
else #default if not match with the localized language
   set okButton to "OK"
   set noSelectionString to "Please select at least two items to calculate it size."
   set totalSizeString to "Total selected size is: "
   set selectedItemsString to " items selected at: " & return
   set reallyString to "Really?" & return
   set tooBigString to "Really?" & return & return & "I only can say this 'The total size of selected items is bigger than 1 ExaByte.'"
end if

set allItemsSize to ""
set itemsCount to 0

tell application "Finder"
   with timeout of 3600 seconds
       set selectedItems to selection as list
       if selectedItems is {} then
           display dialog noSelectionString buttons {okButton} default button okButton with icon stop
           error number -128
           set itemsPath to POSIX path of (folder of the front window as alias)
           set itemsCount to 0
           repeat with iCount in selectedItems
               set itemsCount to itemsCount + 1
               set lastItemSize to size of (info for (iCount as alias))
               set allItemsSize to allItemsSize + lastItemSize
           end repeat
       end if
   end timeout
end tell

set {sizeInUnits, stringUnits} to converSize(allItemsSize)

if itemsCount > 1 then
   set summaryAlert to (itemsCount as string) & selectedItemsString & itemsPath & return & totalSizeString & return & sizeInUnits & " " & stringUnits
   set summaryAlert to reallyString & (itemsCount as string) & selectedItemsString & itemsPath & return & totalSizeString & return & sizeInUnits & " " & stringUnits
end if

tell application "Finder" to display alert summaryAlert

on converSize(allBytesSize)
   #convert bytes size to human readable memory size and return it with the correct unit.
   if allBytesSize > 1.15292150460685E+18 then
       display dialog tooBigString buttons {okButton} default button okButton with icon caution
       error number -128
   end if
   if allBytesSize < 1.15292150460685E+18 and allBytesSize > 1.12589990684262E+15 then
       set totalSize to (round (100 * (allBytesSize / 1.0E+15))) / 100
       set Units to "PB"
   end if
   if allBytesSize < 1.12589990684262E+15 and allBytesSize > 1.099511627776E+12 then
       set totalSize to (round (100 * (allBytesSize / 1.0E+12))) / 100
       set Units to "TB"
   end if
   if allBytesSize < 1.099511627776E+12 and allBytesSize > 1.073741824E+9 then
       set totalSize to (round (100 * (allBytesSize / 1.0E+9))) / 100
       set Units to "GB"
   end if
   if allBytesSize < 1.073741824E+9 and allBytesSize > 1048576 then
       set totalSize to (round (100 * (allBytesSize / 1000000))) / 100
       set Units to "MB"
   end if
   if allBytesSize < 1048576 and allBytesSize > 1024 then
       set totalSize to (round (100 * (allBytesSize / 1000))) / 100
       set Units to "KB"
   end if
   if allBytesSize < 1024 then
       set totalSize to allBytesSize
       set Units to "bytes"
   end if
   return {totalSize, Units}
end converSize

The function converSize() take the bytes as input and returns an array {size, units} with the size as human-readable memory and the corresponding units.
The link of the script exported to an app (10.11.6) with an icon and ready to add to the finder toolbar: … K8Rd-vujTB
Greetings from the Cayman Islands.

Filed under: size, items, selected



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)