5

I would like to drop/copy files/folders and have them compressed then copied to my desktop. This is what I know so far... and it isn't much so any help would be appreciated.

on adding folder items to theFolder after receiving theAddedItems
repeat with x in theAddedItems

This will create my list of files dropped into the folder. Now my compression command...

tar cvf - *variableForFileListHere* | gzip -9 - > files.tar.gz

Somehow I know I have to feed the list "theAddedItems" created in the first part of the script to the tar command. But I'm clueless as to how this might be done. Reading now about tar and it's options as I'm sure there is an "include" function for pointing to a list of items to add to the archive.

4

Try:

on adding folder items to theFolder after receiving theAddedItems
    set myList to {}
    repeat with x in theAddedItems
        set end of myList to quoted form of x's POSIX path & space
    end repeat
    set myList to text 1 thru -2 of (myList as text)
    do shell script "tar cvf " & myList & " | gzip -9 - > ~/Desktop/files.tar.gz"
end adding folder items to
| improve this answer | |
  • Thank you for the script adayzdone. It doesn't seem to be working though. I attached it to a folder and dropped a few .PDF files in there. Nothing pops up on my desktop. I did NOT edit the script at all as it seems you made it pretty much portable for any machine and to be used by anyone. Any thoughts and again thank you for your time. – BsdHelper Aug 23 '13 at 15:44
  • 1
    The extra space at the end of the list my have caused a problem. Try it now. – adayzdone Aug 23 '13 at 17:52
  • 1
    Your script is working. Thank you so much. I will use it as a reference in the future to learn more about Applescript in general and to play around with different compression levels. Again thank you!!! – BsdHelper Aug 27 '13 at 15:24
7

Rather than spending your time with Folder Actions (which I, and many others, have found to be unreliable), might I suggest that you give Hazel a try?

http://www.noodlesoft.com

Disclaimers: I'm going to start off by telling you it isn't free (it's US$28), but if you like automation, this is the place to start. There is a free 14-day trial. I am completely unrelated to the company except as an extremely happy user.

Hazel allows you to create 'folder actions' very easily using a nice GUI which is similar to creating Mail.app rules. You can take action depending on filename, extension, kind (i.e. "images, documents, movies") and many other.

Here is the rule I use for any file/folder added to ~/Action/ZipIt/

Screenshot of Hazel rule

Hazel looks for any file which is not already an archive (I could have told it to act on any files at all, but I want to avoid compressing files which are already compressed). I could add other criteria (which could be options or requirements, like boolean "AND" or "OR" statements).

Whatever it is will be archived, then moved to the Desktop, then it will be shown to me in the Finder.

See the little "Options" button next to the 'Move to Desktop'? Click on that and you can tell Hazel what to do if it detects that the file you are going to move is a duplicate.

There is also an option to have Finder brought to the front or not.

There are lots of other actions Hazel can do besides archive:

screenshot of hazel actions

(Note: iPhoto and Aperture are greyed out because I don't have them installed.)

The options to run AppleScript/Automator/shell scripts means that you have the power to do pretty much anything you can imagine.

I could go on and on, but here's one more example: I have Hazel watch ~/Downloads/ for .zip files. When it finds them, it unzips them to ~/Action/Unzipped/ (you can do this either by telling Archive Utility to use that folder, or use The Unarchiver). Hazel then looks in ~/Action/Unzipped/ for new files which end with .app and, if found, moves them to /Applications/ and opens them. Voilà! Instant installation of apps.

In the world of 99¢ apps, $28 probably sounds like a huge expense, but to me Hazel is one of those tools which takes something Apple did, but not very well (Folder Actions), and replaces it with something that will make you wonder why Apple didn't do it. You get a 2 week trial, and a dedicated developer who has worked on this app for years, and who is very good at answering support questions.

Sorry to sound like a fanboi, but when it comes to Hazel and a few other Mac apps, I really am.

| improve this answer | |
  • I wish it weren't true about folder actions being unreliable, but you're absolutely right. If the OP wants the thrill of creating his/her own folder action, please try that. If it needs to reliably just work, I'd steer away from folder actions, based on my experience. – Daniel Aug 22 '13 at 18:04
  • I don't want to involve any third party utilities but thank you TJ Luoma for the suggestion. – BsdHelper Aug 23 '13 at 15:45
  • Ditto on using Hazel. Also a happy user, running at both home and work. – Gavin Anderson Aug 27 '13 at 21:33
0

There is another option.

Instead of a Folder Action (and the problems inherent with them), you could use QueueDirectories in launchd to call a shell script.

For example, this will call the script /usr/local/bin/mktgz.sh any time a file is put into /Users/foo/Action/Gzip

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.tjluoma.mktgz</string>
    <key>Program</key>
    <string>/usr/local/bin/mktgz.sh</string>
    <key>QueueDirectories</key>
    <array>
        <string>/Users/foo/Action/Gzip</string>
    </array>
</dict>
</plist>

I have a more developed version of this available at https://github.com/tjluoma/mktgz.

| improve this answer | |
  • TJ thank you for your reply. I have copied both you and adayzdone's answers and will try them both out to see which I like best. I know people have stated Applescript is "unreliable" but I think it's a fun language to learn even though your way seems to be more powerful on a system level. Thanks again. – BsdHelper Aug 28 '13 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .