How do I create a Folder Action Script to tar items dropped in folder?
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.
Solution 1:
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
Solution 2:
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/
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:
(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.