Stuffing folders??

how do I create a stuffItX archive for a folder through applcescript?

pelachrum,

This is a folder action script I wrote for stuffing files and moving them to another drive. I had problems with the naming issue where any file that had a name longer than 27 characters would not stuff so I found a way to work around it. This script is probably not pretty because I would try things and if I got it to work I left it alone and did not try to “clean” it up.

on adding folder items to thisFolder
	tell application "Finder"
		activate
		set compfoldest to folder "afolder" of desktop
		set fol1 to folder "anotherFolder" of desktop as alias
		set cnt2 to the number of items in folder fol1
		try
			set label index of every folder of fol1 whose label index is not 1 to 1
		on error
			display dialog "No incorrect file colors." giving up after 1
		end try
		--select folders to compress and compress them	
		repeat with i from 1 to cnt2
			set foldest1 to compfoldest as string
			set compfol to folder i of fol1 as string
			set folderName to name of folder i of fol1
			set compfol1 to compfol as alias
			--get the name of the folder to compress
			set nme to "untitled archive.sit"
			--set the name for the StuffIt archive
			set foldest to foldest1 & nme
			--set fileNameChk to nme
			set arch to nme
			open compfoldest
			tell application "StuffIt Deluxe"
				with timeout of 600 seconds
					try
						
						make new archive with properties {location:file foldest} with replacing
						stuff {compfol1} into archive arch with replacing
						close archive arch
						
					on error
						tell application "Finder"
							activate
							display dialog "Item " & compfol1 & " was not compressed."
						end tell
					end try
				end timeout
				tell application "Finder"
					select file "untitled archive.sit" of compfoldest
					set thisFolder to selection as alias
					set name of thisFolder to folderName & ".sit"
				end tell
			end tell
			
		end repeat
		
		tell application "StuffIt Deluxe"
			quit
		end tell
		
		-- change color of folders
		set label1 to the number of items in compfoldest
		repeat with i from 1 to label1
			set label index of file i of compfoldest to 2
		end repeat
		
		-- move compressed files to backup disk
		try
			mount volume "afp://155.155.155.155/anotherHardDrive" as user name "dada" with password "dedada"
		on error
			display dialog "Hummm. . ."
		end try
		
		repeat with z from 1 to label1
			select file z of compfoldest
			set nme to selection
			set file_name to the name of file z of compfoldest as string
			set nme_compare to characters 2 through 5 of file_name as string
			--display dialog nme_compare
			if nme_compare is less than "2000" then
				move nme to folder "I0000 - I1999" of disk "anotherHardDrive" with replacing
			else if nme_compare is less than "3000" then
				move nme to folder "I2000 - I2999" of disk "anotherHardDrive"
			end if
		end repeat
		try
			close window "Desktop"
		on error
			display dialog "No window to close" giving up after 1
		end try
		try
			close window "anotherHardDrive"
		on error
			display dialog "No window to close" giving up after 1
		end try
		try
			close window "Completed Jobs"
		on error
			display dialog "No window to close" giving up after 1
		end try
		
	end tell
	say "Finished"
end adding folder items to

Now I’m not sure about the stuffitX file - it may be as simple as giving it the suffix .sitx. I’ll try messing around and see if I can get that part. Also note that the “with replacing” part of the actual Stuffit script above does not seem to work. If a file already named the same is in the destination it errors out. I still haven’t figured that one out.

Hope this helps some.

PreTech

Model: G5 dual 1.8
AppleScript: 2.1
Browser: Safari 125.12
Operating System: Mac OS X (10.4)

Model: G5 dual 1.8
AppleScript: 2.1
Browser: Safari 125.12
Operating System: Mac OS X (10.4)

I appreciate it,

I’ll see what I can derive from it

btw, is stuffit delux the only version that can be used in this scenario?

apple site has this script on it, it seams to zip files instead, you think this could be tweaked to stuff instead? I think it uses an OS built in compressing engine

on adding folder items to this_folder after receiving these_items
tell application "Finder"
if not (exists folder "Done" of this_folder) then
make new folder at this_folder with properties {name:"Done"}
end if
set the destination_folder to folder "Done" 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 & (name of the item_info) & ".zip")
do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & ¬
item_path & " " & destination_path)
end if
end repeat
end adding folder items to

My co-worker used that script and came up with this.

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set label index of every item of this_folder to 2 -- Red label
		set the destination_folder to 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 & (name of the item_info) & ".zip")
			do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & item_path & " " & destination_path)
		end if
	end repeat
end adding folder items to

Just change the “.zip” to “.sitx” and it does the job. I don’t know much about the shell script part but I know there are alot of users on here that do. Of course this script changes the label color of your folder too. This is setup as a folder action script.

PreTech

it worked indeed, thank you

one question though, when I drop two files for instance, it creates individual stuffit archives for each file and when I unstuff those, in each there’s actually the file that should be in there and then its duplicate with “1” added as last character (before extension)

how do I tweak this to target a certain folder on the desktop and stuff that as single archve

Pelachrum,

I added and changed the script just a little. At the top I made “destination_folder” a property. A property keeps its value until you change it through the script or you recompile your script. I added this so that the first time you run the script you get prompted to choose your destination folder. The next time you run the script, it remembers where the destination is. If you want to choose another destination each time you need to remove the property statement and then the “if” statement. The line I changed is the line

“set destination_folder to this_folder as alias”.

You can use this line to set your destination folder to what ever folder you want such as:

“set destination_folder to folder xxxx of folder xxxy of folder xxyz of desktop” or

"set destination_folder to folder “Macintosh HD:Users:username:Desktop:xxyz:xxxy:xxxx:”

Note that in the last method you have to have a colon after your folder name. If it is missing the script will think your folder is a file. Both of these need to be in a tell app “Finder” block.

Anyway here is the code as I changed it.

property destination_folder : {}
on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		set label index of every item of this_folder to 2 -- Red label
		if destination_folder is {} then
			set the destination_folder to choose folder with prompt "Choose the destination folder."
		end if
		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 & (name of the item_info) & ".zip")
			do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & item_path & " " & destination_path)
		end if
	end repeat
end adding folder items to

Hope this helps. I added extra information because I don’t know how much you know about scripting.

PreTech

weirdness on my end…
The script works as posted (very nice)… but when I re-configure as suggested and define a location instead of a prompt, and request sitx (using 10.4.3 and applescript 2.1.1) 2 things happen.

  1. If just changing to .sitx, the action keeps creating more duplicate archives ad infinitum.
  2. If I define the destination, for example: “Mac OS X:Personal Storage:Send:Archive:”; then the files change labels, but no zip or sitx file is created.

I’m new at scripting, so I’m probably missing something really obvious…
Any help is appreciated.

my changes====>
property destination_folder : {}
on adding folder items to this_folder after receiving these_items
tell application “Finder”
set label index of every item of this_folder to 2 – Red label
if destination_folder is {} then
set destination_folder to “Mac OS X:Personal Storage:Send:Archive:”
end if
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 & (name of the item_info) & “.sitx”)
do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & item_path & " " & destination_path)
end if
end repeat
end adding folder items to

Thanks.

cblev,

One thing I see is where you declare destination_folder. You have:

This is actually setting destination_folder to the text “Mac OS X:Personal Storage:Send:Archive:” instead of setting it to a folder. To set it to a folder location you need to modify it slightly.

set destination_folder to folder "Mac OS X:Personal Storage:Send:Archive:"

I’d say try changing this and see if this solves the problem. I have to admit that I don’t know diddly about shell scripting so I don’t know exactly what that line is doing but since it involves the destination_folder I’d be inclined to say that this will probably solve the issue. If it doesn’t, just post again.

PreTech

Found the trick after running bits of the script thru the event log – have to set the destinatoin to “Alias” not just the path.
Works fine after that (though the .sitx thingy is still funky)

If Destination_folder is {} then
Set Destination_Folder to [b]alias[\b} "Mac OS X:Personal Storage:Archive:send:"
end if

Thanks

Based on the script below. Is there to wait until all the files arrive in the folder and then make one zip of all the files? Thanks

on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if not (exists folder "Done" of this_folder) then
			make new folder at this_folder with properties {name:"Done"}
		end if
		set the destination_folder to folder "Done" 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 & (name of the item_info) & ".zip")
			do shell script ("/usr/bin/ditto -c -k -rsrc --keepParent " & ¬
				item_path & " " & destination_path)
		end if
	end repeat
end adding folder items to