Declaration of VAR

and some other stuff

Mac OS service for displaying biggest files in the directory

2017-05-29 15:58:13 +0200

2017-05-29 15:58:13 +0200 | Comments

Having solved the problem of locating the biggest files in a folder, I decided to create a service that would do the same in Finder, showing results in a dialog window.

So, the Terminal’s command is the following:

du -sh ~/* | gsort -rh | head -10

Now we just need to feed a folder path to it in the Automator’s workflow:

Here’s the script:

on run {input, parameters}
    
    if (count of input) is not 1 then # if there are more (or less) than 1 folders selected, then show an error message end exit
        display dialog "This works only with a single folder." with icon stop with title "Error" buttons {"OK, jeez"} default button "OK, jeez"
        return
    else # do the shit
        set result to do shell script "du -sh " & quoted form of POSIX path of input & "* | /usr/local/bin/gsort -rh | head -10"
        display dialog result with title "Top 10 biggest files in this directory" buttons {"OK"} default button "OK"
        #return result
    end if
    
end run

Some explanation for the script:

  • quoted form of - wraps the string in quotes (in case of file names with spaces);
  • POSIX path of - transforms the given path into standard /path/to/something/-format;
  • /usr/local/bin/gsort - the full path to the gsort utility, because for some reasons even though it works fine in Terminal, you can’t call it just by name here, so you have to provide the full path (use find /usr/local -iname "gsort" to find it);
  • display dialog - shows a dialog window right from the script, which is quite handy, because you don’t need to add more steps to the workflow. Read more about this command here (you can also use display alert).

And here it is in action: