Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I would like to set up an automated way for archiving files in OS X Lion. By archiving I mean using HFS+ compression feature on a specific folder, and make every file that I move into this folder automatically compressed. I'm thinking of using this to archive large files that I don't use often, but without having to manually create compressed archives and having to extract them in case I need them.

I came across the tool afsctool (brew install afsctool), which can, via command-line, apply HFS+ compression to a folder and all files in it. But I believe it does not activates compression of future files moved to the folder.

Moving files manually and running a command line instruction repeatedly is inconvenient. Therefore I'm guessing I would need to used automator/AppleScript to execute this in two steps:

  1. Create a script that allows moving any file to a specific folder. Ideally an option named Archive accessible via right-click on the file.
  2. Run a command line instruction afsctool -c <folder>, to activate compression on all new files just moved to the folder.

I have no experience using automator or AppleScript, so I would like to know where to start, especially regarding how to add actions to Finder, and how to run a command line tool from a script.


Updates

@kopischke guide works well. The script itself, I made based on the answer by @mark, but using afsctool -c <folder>, instead of ditto. Here's the script: It should be put in ~/Library/Scripts/Folder Action Scripts

on adding folder items to this_folder after receiving these_items
repeat with i from 1 to number of items in these_items
    try
        set this_item to item i of these_items
        tell application "Finder"
            set the file_name to the POSIX path of this_item
        end tell
        set cmd to "/usr/local/bin/afsctool -c " & file_name
        do shell script cmd
    on error the error_message number the error_number
        display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1

    end try
end repeat
end adding folder items to

Since http://web.me.com/brkirch/brkirchs_Software/afsctool/afsctool.html at MobileMe is no longer available, there's diimdeep/afsctool at GitHub, currently 1.6.4.

Also afsctool in MacPorts where they're aware of the MobileMe issue but maybe unaware of the source at GitHub.

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

There are two parts to the answer to your question.

  1. How to add a Finder context menu item to “Archive” files: this is easy to achieve by creating an Automator Service (Mac OS X Automation has a good overview of what the Automator services introduced in OS X 10.6 can do):

    • Launch Automator, choose “Service” when prompted for the kind of workflow you want to create.
    • Choose “Files or Folders” in the “Service receives” drop down (approximate translations – I’m on a German system). Optionally, set “in” drop down to “Finder”.
    • Add a “Get Selected Finder Items” action.
    • Add a “Move Finder Items” actions below that and set it to your target folder.
    • Save your service in the default location (~/Library/Services) as “Archive”.

    you now have a new service menu and context menu entry (depending on the number of services active: either on the first menu level, or in the “Services” submenu) called “Archive” that will move the selected file or folder to your target folder.

  2. How to automate HFS+ compression of files added to your target folder: there are several ways to achieve that. You could, of course, simply add that step to your archiving service. The disadvantage of this approach is that no compression will be applied if files or folders are ever added to the folder outside the service, of course. A better approach would be to have everything in the folder be compressed automatically, without reliance on the the entry vector or user interaction.

    One way is, as you have discovered, to have a compression utility run every time a file or folder is added to your watched folder:

    • the way to launch a shell utility in AppleScript is the do shell script command – see the linked documentation;
    • the inbuilt way to leverage filesystem events in an AppleScript is to use Folder Actions, which call AppleScripts on changes in a watched folder. What events the script reacts to are defined by the script itself, through the handlers it provides (for instance, the script in Mark’s answer has a handler for adding folder items – meaning it reacts to newly added files; see the Applescript Language Guide for the full reference). Folder Actions configuration is found in the services menu of folders in Finder (in the context menu, too).
    • a turbocharged alternative to Folder Actions is Paul Kim’s Hazel (commercial software), which adds rule based processing and a plethora of criteria for filesystem event handling that go far beyond what you can achieve with simple Folder Actions – you might want to investigate Hazel if you plan on doing more or more complex stuff along the lines of what you are planning now.

    An alternative to the whole scripting approach is using LateNiteSoft’s Clusters – another commercial software, that does nothing but automatically apply (and re-apply, where needed) HFS+ compression to the contents of watched folders.

share|improve this answer
    
Thanks for your answer. I'll try the option of running an AppleScript as a Folder Action. I'm aware of Clusters, and what I'm trying to achieve is exactly that (a poor man's Clusters) ;) –  WIll May 7 '12 at 23:47
    
@williamvoor: Glad if could help. Would you mind accepting the answer, then? That’s the check mark button :). –  kopischke May 8 '12 at 7:28
    
The folder action script solution works. The script itself I pasted on the question. –  WIll May 8 '12 at 11:31
add comment

Clusters

…  takes advantage of the file compression technology in Mountain Lion, Lion and Snow Leopard …

MoreSpace Folder Compression

… HFS+ compression has the best effect on uncompressed file formats like text and data documents, databases and mailboxes. A typical Mail mailbox size can be reduced up to 50%. …

Preference

Of those two, I find Clusters more mature and easier to use – probably a better fit for this question.


Proceed with caution

In an edge case, silent dataloss may occur with third party uses of AppleFSCompression. From what I can tell this is a bug in the OS, and may occur only where there's an extraordinary lack of free space. I'll feed the bug to Apple.

share|improve this answer
add comment

@williamvoor: The script fails with filepaths which contain spaces, as set the file_name to the POSIX path of this_item does not escape properly. Correct the appropriate line of your code to this:

set cmd to "/Users/sn/bin/bin/afsctool -c \"" & file_name & "\""

Explanation: afcstool's file_name argument is surrounded with quotation marks, which themselves in the AppleScript code are escaped in order to be correctly handed over to the command line environment.

Meta-topic: As I lack reputation points, I could not comment at the appropriate position (= to @williamvoor 's own post), therefore I post it as an own answer, and ask you to edit the code change into your post, and give me a thumbs up, so that I gain enough reputation, enabling me to directly comment in the future. Thanks!

After that is done I or and admin could delete my post.

share|improve this answer
add comment

This thread gives an applescript to use as a Folder action which will compress files as they are added

on adding folder items to thisFolder after receiving theseItems
    set destinationPath to "/POSIX path/to/destination/folder"
    repeat with thisItem in theseItems
        set cmd to "ditto --hfsCompression " & quoted form of POSIX path of thisItem & " " & destinationPath
        do shell script cmd with administrator privileges
    end repeat
end adding folder items to
share|improve this answer
    
Didn’t know about the --hfsCompression option in ditto – good one! But your answer doesn’t address OP’s question about integrating a moving script into Finder, it moves files dropped into the archive folder to another folder (which I think would be one more redirection than OP was planning to do) and it will ask for authentication every time (annoying in an automation context). –  kopischke May 7 '12 at 9:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.