0

I need to figure out how much is on a LOT of hard drives-over 150 USB and thunderbolt drives. I thought I could build an AppleScript or an automator action to get the total size of a volume, and subtract the "available." Ideally, I could start the script, it would watch for a volume to mount, do this math, and add it to a running total.

We're shopping for a new server and we'd love to know how big of a drive we need to put on this thing to keep all these assets live.

I'm pretty new at apple script, but I'm trying. I can't even find AS dictionary items that will look for mounted disks-much less do the math of adding (even if it's just outputting the information it's pulling into a tab delimited file I could calulate in Excel)

Actually, maybe that's the ideal version. It sees a drive mount, gets the name, gets the total capacity, gets the free space...perfect world? a date created. Then adds that to a text file.

Code, pointers to where to learn-all appreciated.

Alex
  • 1
  • You can use a launchd agent to detect when a volume was mounted and the shell command `diskutil info` to get the information about free and available space. – vadian Oct 12 '18 at 16:16

2 Answers2

0

Here is an AppleScript solution that will return the displayed name and the amount of free space available on each mounted volume.

set allMountedVolumesInfo to {}

tell application "System Events"
    set everyDisk to every disk
    repeat with i from 1 to count of everyDisk
        set thisDisk to displayed name of item i of everyDisk
        tell its disk thisDisk
            try
                set diskFreeSpace to (characters 1 thru 7 of ((free space / 1.0E+9) as string))
                set totalCapacity to (characters 1 thru 7 of ((capacity / 1.0E+9) as string))
                set creationDate to creation date
            on error errMsg number errNum
                set diskFreeSpace to (free space / 1.0E+9)
                set totalCapacity to (capacity / 1.0E+9)
                set creationDate to creation date
            end try
            set theRecord to {volume:thisDisk, free space:((diskFreeSpace & " Gigabytes") as string), creation date:creationDate, capacity:((totalCapacity & " Gigabytes") as string)}
        end tell
        set end of allMountedVolumesInfo to theRecord
    end repeat
end tell
wch1zpink
  • 3,026
  • 1
  • 8
  • 19
0

The script below is designed to be run as a folder action. If you're unfamiliar with these, they allow one to monitor a designated folder for any changes to its contents, upon which an AppleScript can be triggered to perform some actions with the items that were added (or removed) from the watched folder.

Whenever one mounts a disk, a mount point is added as a disk item to the /Volumes folder. Therefore, you can set up a folder action to watch the contents of the /Volumes folder, and have it trigger an AppleScript whenever a new volume is mounted.

Here's the script that is designed to be triggered:

use sys : application "System Events"
use scripting additions

property text item delimiters : tab
property fp : "~/Desktop/diskinfo.csv"

on adding folder items to Volumes after receiving D
    set D to a reference to the sys's disk named (item 1 of D)

    set f to a reference to sys's file fp
    if not (f exists) then initCSVFile(fp)

    set isod to the (current date) as «class isot» as string

    get the contents of {isod's text 1 thru 10, ¬
        isod's text 12 thru -1, ¬
        D's name, ¬
        gigabytes(D's capacity), ¬
        gigabytes(D's free space)} as text

    write the result & linefeed ¬
        to (f as alias) ¬
        starting at eof ¬
        as «class utf8»
end adding folder items to

to gigabytes(bytes)
    round bytes / (10 ^ 7)
    result / 100
end gigabytes

to initCSVFile(fp)
    local fp

    set f to make of sys new file with properties {name:fp}

    {"Date", "Time", "Volume Name", "Capacity (GB)", "Free Space (GB)"}
    write (result as text) & linefeed to (f as alias) as «class utf8»
end initCSVFile

Copy-n-paste this into Script Editor and save it as "New Volume Mounted.scpt" in the folder ~/Library/Scripts/Folder Action Scripts, where ~ is your home directory, e.g. /Users/Alex. If the "Folder Action Scripts" folder doesn't exist, create it.

If you're happy creating a folder action from here, then do so, and mount a new volume. When the script above is triggered, it adds the current date and time the volume was mounted together with its name, capacity and amount of free space, to the end of the file diskinfo.csv on your desktop (if the file doesn't exist, the script creates it). This is a CSV (comma-separated value) file, using tabs as delimiters (so, it's actually a TSV). QuickLook can typically display the contents of these files rather nicely:

QuickLook


Folder Action Setup

If you're not familiar with setting up a folder action, then read the section called Attaching a Folder Action Script to a Folder of the Mac Automation Scripting Guide - Watching Folders.

However, to save you some time for now, I also created a script that will create the folder action for you:

use sys : application "System Events"

property name : "Volumes"
property path : "/Volumes"
property folder action : a reference to folder action named (my name)
property script : "New Volume Mounted.scpt"
property folder : a reference to Folder Action scripts folder
property file : a reference to the file named (my script) in my folder

set folder actions enabled to true

if not (my file exists) then return open my folder
if my folder action exists then return my folder action's scripts

make new folder action with properties {name:my name, path:my path}
tell my folder action to make new script with properties ¬
    {name:my file's name, POSIX path:my file's POSIX path}

set my folder action's enabled to true
set my folder action's scripts's enabled to true

Again, copy-n-paste that into a new document in Script Editor and then run it. Only do so once you've saved the first script to the location I specified, and double check the filename is "New Volume Mounted.scpt" (you can pick whatever filename you like, but you'll need to make the appropriate change in the script that creates your folder action).

You may need to grant the necessary accessibility privileges for this script to run.

If you run into any problems, let me know.

CJK
  • 5,732
  • 1
  • 8
  • 26