Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: An AppleScript to compress a Finder selection System 10.6
Snow Leopard only hintI use my faithful Butler to run Finder's usual operations, like copying, moving, and deleting files, making both my mouse and trackpad completely useless devices. As a keyboard-only user, I also wanted to add some other Finder operations, such as file compression.

Here is a small AppleScript, to be run from Butler (or any other script-launcher utility), that will compress the current Finder selection into a .zip archive, with the same name and within the same location. You can launch this script from Butler, triggering it with an abbreviation or a keyboard hotkey.

My script is based on this hint and turns it into a Finder-selection script.

I didn't test any compression levels, as my only point was to be able to compress a Finder selection directly, using ditto.

Also, it was tested in 10.6.5 only.
tell application "Finder"
  activate
  set theItems to the selection as alias
  set theItemsprops to properties of theItems
  set itemPath to POSIX path of theItems
  set destFold0 to (container of theItems) as alias
  set destFold to POSIX path of destFold0
  set itemName to name of theItems
  do shell script ("ditto -c -k --sequesterRsrc --keepParent " & itemPath &  " " & destFold & "'" & itemName & "'" & ".zip")
end tell

[crarko adds: You should also be able to make this into an Automator Service, and assign a keyboard shortcut to produce the same effect.]
    •    
  • Currently 2.25 / 5
  You rated: 2 / 5 (8 votes cast)
 
[7,020 views]  

10.6: An AppleScript to compress a Finder selection | 15 comments | Create New Account
Click here to return to the '10.6: An AppleScript to compress a Finder selection' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: An AppleScript to compress a Finder selection
Authored by: regulus on Mar 04, '11 09:13:11AM
Nice idea about the script however the applescript code isn't clean so I cleaned it up for you. Here's some of the coding issues I saw...
1) you are telling the Finder to perform some tasks that its not needed for which is not a good idea because it can cause slowness and other problems
2) "the selection" is a list of selected items, so coercing to "alias" will not work if more than 1 item is selected
3) there is no error checking
4) a few other small things

I hope this helps.
try
	tell application "Finder"
		set theSelection to the selection
		set selectionCount to count of theSelection
		if selectionCount is greater than 1 then
			error "Please select only one Finder item before running this script."
		else if selectionCount is less than 1 then
			error "Please select one Finder item before running this script."
		else
			set theItem to (item 1 of theSelection) as alias
			set destFolder to (container of theItem) as alias
			set itemName to name of theItem
		end if
	end tell
	
	do shell script ("ditto -c -k --sequesterRsrc --keepParent " & quoted form of POSIX path of theItem & space & quoted form of (POSIX path of destFolder & itemName & ".zip"))
on error theError
	tell me
		activate
		display dialog "Error: " & theError buttons {"OK"} default button 1 with icon stop
	end tell
end try
---
Hank
http://www.hamsoftengineering.com
---
Hank
http://www.hamsoftengineering.com
Edited on Mar 04, '11 09:18:14AM by regulus


[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: osxpounder on Mar 04, '11 03:11:57PM

Regulus,

Nice of you to try improving this script, but I'm not sure it works properly yet.

When I try to invoke it with Butler, I get a dialog box from Butler with an error, telling me, "Error: Please select only one Finder item before running this script."

I paste your script into a Butler "Smart Item: Applescript." I select several files in Finder. I invoke the Butler item with my chosen abbreviation, "zipem" and see the error dialog box.

I also tried saving your script as an app, and invoking that with Butler, when several files are selected in Finder. I get the same error ("Please select only one file...").

Is there a better way to invoke your script using Butler? How are you doing it?



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: regulus on Mar 04, '11 03:22:20PM
I didn't write the basic script code, I just improved it. The author's script only works when you select one file, thus my script works the same. The code can probably be improved to allow more than one item selected. So your problem seems to be that you're selecting several items in the Finder window... that's why I put error handling into the code... the error is telling you the problem... try it on just one item.
---
Hank
http://www.hamsoftengineering.com
Edited on Mar 04, '11 03:25:30PM by regulus


[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: osxpounder on Mar 04, '11 09:22:13PM

Darn, I misunderstood. I thought you intended your script to handle more than one file. My mistake. Thanks, though!

I found that an Automator action can handle more than one file, but since it strips some metadata, I think I'll stick with using the right-click contextual menu in OS X instead.



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: prijker on Mar 05, '11 03:03:56AM

The original script handles several files. I dropped this part of the original code, but you could try to merge this code with mine



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: prijker on Mar 04, '11 03:45:55PM

Nice improvment!
It works better than my code and has error handling too.
I'm using it already, thanks



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: drudus on Mar 04, '11 10:33:25AM

Isn't this already a built in feature?

Right click (or ctrl + click) a file in the Finder & the context menu has a 'Compress…' option that spits out a zip. it handles multiple selections too. It uses the built in Archive Utility, so preferences set there should be honoured.
Archive Utility is hidden away in /System/Library/CoreServices/Archive Utility.



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: chrischram on Mar 04, '11 10:47:31AM

Yeah. Why write a script when the function you need is built right into the OS?



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: everkleer80 on Mar 04, '11 12:03:34PM
LOL It's funny how many people just look at the title or read the first sentence, and don't bother reading even as far as the second sentence of the hint. =)
As a keyboard-only user, I also wanted to add some other Finder operations, such as file compression.


[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: drudus on Mar 04, '11 12:57:56PM

It's also funny how many advanced users don't bother checking out the built in features, before using third party tools (Butler) to do basic tasks.

Automator has a built in 'Create Archive' feature (as well as moving & deleting), save that as a Finder plug-in then assign it a shortcut in the Services section of keyboard Shortcuts in System Preferences. No need to write any Applescript to run a Terminal command.

The reason you need the Automator action is because the 'Compress' menu item includes the file name, so targeting a shortcut via the name fails to work.

Edited on Mar 04, '11 12:58:46PM by drudus



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: osxpounder on Mar 04, '11 02:48:23PM

Sometimes it's more efficient to let someone find a groovy time-saving hint and share it with you, rather than sink time into exploring all the features that might be available in your OS or apps (or might not be there).

Kinda like prijker did here, giving us this hint.

Thanks, prijker! I really appreciate that you took time to share it with us.



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: alangbaker on Mar 04, '11 12:06:42PM

Wouldn't it be a lot easier just to assign a keyboard shortcut to the "Compress" command in the Finder?



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: osxpounder on Mar 04, '11 02:45:09PM

Have you tried it?



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: wallybear on Mar 04, '11 03:44:01PM

You cannot assign a shortcut to the Compress menu, as it includes the name of the item selected.



[ Reply to This | # ]
10.6: An AppleScript to compress a Finder selection
Authored by: darren2 on Mar 09, '11 10:34:15AM

Nice improvment!
It works better than my code and has error handling too.
I'm using it already, thanks



[ Reply to This | # ]