Folder Action to Compress and Move a folder

I’ve set up a two part folder action scenario.
Intent is to store the most recent data set on another server for ready access. It needs to be compressed, as it takes to much bandwidth to send otherwise.
(base scripts obtained from several sources, I don’t write AS, only edit):

First folder A has an action set to compress newly arriving content to a zip with a specified folder B.
Next action is on that specified folder B, instructing it to move the accepted zip file to another directory off site.

Problem 1:
The “new content” is 2GB of files from a database backup which takes about 30 seconds to complete.
However, the compress action doesn’t wait for the files to finishing writing, so the zip is made too soon.
I tried “delay 3600” at various step points, but it does not work as expected (no zip is created).
How to I get the action to wait a minute before beginning?

on adding folder items to this_folder after receiving these_items
tell application “Finder”
if not (exists folder “NYBU” of this_folder) then
make new folder at this_folder with properties ¬
end if
set the destination_folder to folder “NYBU” of ¬
this_folder as alias
set the destination_directory to POSIX path of ¬
the destination_folder
end tell
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to info for this_item
if this_item is not the destination_folder and the ¬
name extension of the item_info is not in ¬
{“zip”, “sit”} then
set the item_path to the quoted form of the ¬
POSIX path of this_item
set the destination_path to the quoted form of ¬
(destination_directory & “”)
do shell script ¬
("/usr/bin/ditto -c -k -rsrc --keepParent " & ¬
item_path & " " & destination_path)
end if
end repeat
end adding folder items to

The second stage is the send that zip to another computer share point.
Problem 2
That worked once, but is not reinitiated when the new zip appears in the source folder B. I tried it both on the enclosing folder as well as the zip.
How do I get it to always send the changed file (even though name is same, which I do want to overwrite).

on adding folder items to this_folder after receiving these_items
set SourceNY to alias “:Library:Database”
set DestinationLA to alias “”
tell application “Finder” to duplicate SourceNY to folder (alias “:Volumes:laadmin:Desktop:From_NY:NYBU”) with replacing
end adding folder items to

Model: xserve
AppleScript: 2.2.1
Operating System: Mac OS X (10.5)

Hello and welcome to Macscripter!

First of all, why not use a droplet instead of a folder action, a droplet works all of the time.

And now for the funny thing :slight_smile: The third sceneario:

Say you could configure the compress action, that you have on the contextual menu through a preference pane to send the compressed file to its destination directly, how about that?

(I am not 100% sure that it works, but the options are there, at least in Snow Leopard, maybe they are there in Leopard as well. )

Well. I am too tired right now, heading for bed, I’ll look into the options of the compress action tomorrow. :slight_smile:


I tried the preference pane of the archieve utility, it turns out to not store any values in a property-list file, so having a UI for determining post processing, was a dead end.

I’am cleaning up the folder action for you as soon as I am finished writing this post. I’d rather use a launchAgent, but this will suffice for now, until you are having problems at least.

(Folder actions aren’t that reliable, due to eventqueues and timeouts, that will make a folder action “forget” a file now and then.)

As for the second part of your problem, have you tried using a symbolic link to the recievers directory?

That way, (if such symblic links works-which-I-believe-they-do) you just drop the file into the folder that is a symbolic link, and the file is automatically sent without further ado! (Laziness pays off now and then :D)

This is why, in the sample file which I posted this morning, I drop the contents of added_items and work upon the contents of the targetted folder.
Given the description of the problem, the folder isn’t supposed to contain many files so it would not be a serious penalty.

Yvan KOENIG (VALLAURIS, France) jeudi 7 mars 2013 15:33:27

No, but say if you are using Finder for this, and the users have like 40 Finder windows open, and are doing some more file manipulations while the folder action is working, then I see the timeout coming.

We have a Norwegian saying: it isn’t the last drop that makes the glass spill over, it is what was in it from before. :wink:

Hello. I think I have some right issues somewhere, at least it seems so in the console.log

So this doesn’t work for me, maybe it will for you. I can’t execute System Events blocks, nor do shell scripts.

I have simplified the first script heavily, removed the Finder block, and just assumes you create the folder up front.
I hope you can use it. :expressionless:

on adding folder items to this_folder after receiving these_items
	set the destination_folder to ((this_folder as text) & "NYBU") as alias
	set the destination_directory to POSIX path of the destination_folder
	repeat with i from 1 to (count these_items)
		set this_item to item i of these_items
		set theFileExt to text -4 thru -1 of (this_item as text)
		if theFileExt is not ".zip" and theFileExt is not ".sit" then
			set pxfn to POSIX path of this_item
			set sz to (do shell script "stat " & pxfn & "|grep -o 'Size: [[:digit:]]*' |sed -n 's/\\([^[:digit:]]*\\)\\(.*\\)/\\2/p'")
			repeat while true
				delay 1
				set nz to (do shell script "stat " & pxfn & "|grep -o 'Size: [[:digit:]]*' |sed -n 's/\\([^[:digit:]]*\\)\\(.*\\)/\\2/p'")
				if sz = nz then exit repeat
				set sz to nz
			end repeat
			set the destination_path to the quoted form of ¬
				(destination_directory & "")
			do shell script ¬
				("/usr/bin/ditto -c -k -rsrc --keepParent " & ¬
					pxfn & " " & destination_path)
		end if
	end repeat
end adding folder items to

I’ll check it out. I’m afraid the lower portion is hard to decipher, as I rarely dabble in AS.

To clarify though, this is for a ‘robot’, scheduled scripts already create file backups to a local folder, erasing the prior folder after it completes the new set. I could use a CRON script in that scheduler to compress the backup folder, then move that zip to the other server share point. As I couldn’t find sample CRON scripts to get that idea off the ground, I explored a folder action on the backup folder.
As I recollect, a droplet requires user interaction. This is intended to be an unattended process. :slight_smile:
It also has to run on 10.5.8 (xserve), so some of the newer Automator concepts could not be implemented.



I hope it works, you’ll have to create the folder NYBU before you run the folder action.

As I said, I can’t test folder actions at the moment properly, as it seems I have some problems with launchd. :slight_smile:

pxFn? Changed to Cap F, and then it worked, but it still created a zip before the file finished copying to the folder. I Changed delay from 1 to 60, and then it didn’t respond. That IS in seconds, right?


Yes that is in seconds, try setting it to 10 for starters, I think it might possibly time out in 20, but I really don’t understand why it would start zipping before it is finished copying. (Rethrottling of the underlying launchAgent.

How do you observe that it starts zipping before it is finished copying? But it can happen, for some seconds, that the sizes will be the same, in the middle of the copy process, if the io is blocked.

Try adding “as number” after the do shell scripts, (outside the parenthesis).

Like this. :slight_smile:

set nz to (do shell scrkipt .) as number

Just a precaution.