Another compressing (zip) question…

I’ve written a script to take files from two separate servers and put them together, then compress one of the folders. I am currently using a shell script that I found in another post around here.

shell script:
do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & (quoted form of (POSIX path of destination)) & " " & (quoted form of (POSIX path of zipFile))

The problem that I have is that on large folders, I’m getting timeoouts, and the users don’t see a progress bar, so they have a tendency to think that the script has failed or quit. I also found this script that pidge1 wrote:


tell application "Finder"
	activate
	set x to choose folder --pick the folder to work on
	set theitem to every file of folder x
	repeat with theitem in theitem -- go through each item in the folder
		select theitem
		---------------------------------------------------------------------------------------------------
		tell application "System Events"
			tell process "Finder" to click menu item 1 of menu "File" of menu bar 1
			display dialog "Archiving.. Please Wait." giving up after 1
		end tell
		---------------------------------------------------------------------------------------------------
		-- this bit above creates the archive
	end repeat
end tell
tell application "Finder" to move (every file in the folder x whose name does not end with ".zip") to the trash

But it doesn’t seem to work on my system. Not really sure why, but I get an error message that says “System Events got an error: NSRecieverEvaluationScriptError: 4”

I’m using Applescript 1.10.7 on OS 10.4.7

Thanks for any help.
Brian

Hi, Brian.

Although it’s not as fast as your shell script, the Finder route seems a good one to go from the point of view of getting a progress bar.

The problem with pidge1’s script (as posted above) is that item 1 of the Finder’s ‘File’ menu is ‘New Finder Window’. It’s the ‘Create Archive’ item that needs to be clicked. When there’s a selection in the Finder, this item is enabled with a qualifier appended to it: eg. ‘Create Archive of 3 items’ or ‘Create Archive of “MyFile.txt”’. So pidge1’s script should work with the following substitution:

-- Change this line:
tell process "Finder" to click menu item 1 of menu "File" of menu bar 1

-- . to this:
tell process "Finder" to click (first menu item of menu 1 of menu bar item "File" of menu bar 1 whose name begins with "Create Archive")

Another way, if only one item at a time is to be archived, and which would also work with user languages other than English, would be to filter for the name of the item to be archived. Tell application “Finder” to set itemName to name of the item, then:

tell application "System Events"
	tell process "Finder" to click (first menu item of menu 1 of menu bar item 3 of menu bar 1 whose name contains itemName)
end tell

Another potential problem is that once the ‘click’ command has been issued, the script simply carries on without waiting for the archiving process to finish. kai posted a neat handler yesterday in this thread that makes the script wait until the progress window (if any) no longer exists. I found his first repeat finished a bit too quickly for the current purpose, so I’ve replaced it with a simply one-second delay here. It may need to be lengthened on slower machines:

on check_progress() -- After kai.
	delay 1 -- or longer.
	tell application "Finder"
		repeat while ((first window whose class is «class prwd») exists)
			delay 0.2
		end prepeat
	end tell
end check_progress

tell application "Finder" -- After pidge1.
	activate
	set x to choose folder --pick the folder to work on
	set theFiles to every file of folder x
	repeat with thisFile in theFiles -- go through each item in the folder
		set itemName to name of thisFile
		select thisFile
		---------------------------------------------------------------------------------------------------
		tell application "System Events"
			tell process "Finder" to click (first menu item of menu 1 of menu bar item 3 of menu bar 1 whose name contains itemName) -- GUI Scripting must be enabled for this.
			-- display dialog "Archiving.. Please Wait." giving up after 1
		end tell
		---------------------------------------------------------------------------------------------------
		-- this bit above creates the archive
	end repeat
end tell

check_progress() -- Wait for any progress bar to disappear.

tell application "Finder" to move (every file in the folder x whose name does not end with ".zip") to the trash

If you don’t mind an extra application and an indeterminate (just spinning) progress bar, then you might want to check out BP Progress Bar.

Thanks Nigel for all the info. But I still get the same error message when I try to run the script that you wrote. Is there something that I need to activate or enable on my computer that will allow System Events to run? I tried running it on another computer at work, and it ran fine.

Any suggestions would be greatly appreciated.
Brian

Hi, Brian.

System Events runs anyway. But you need to have GUI Scripting enabled on your machine before the script will work. Either use the AppleScript Utility application that’s in Tiger’s AppleScript folder, or check the box marked “Enable access for assistive devices” in System Preferences’s “Universal Access” pane.

Forgot to say thanks… that worked perfectly… so thanks for all your help!

Brian