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

Create Windows-compatible zip files via AppleScript Desktop
While I love working on a Mac, I really hate the fact that you don't have an option to remove Mac-specific files from the zip file created by Finder's Create Archive. I often find myself creating an archive on the Mac only to have to open it with WinZip on the PC so I can take out the hidden dot files (e.g .DS_Store).

So I hacked up this code for the small little archives I create for my website. The code isn't perfect, but I'm sure someone can clean it up and put in some better error trapping.
-- Script name: WinZipMe
-- zip command options: -r = recursive,
--                      -j = junk path (similar to Create Archive)

tell application "Finder"
  -- Note: There's no extensive error trapping on this script
  -- (e.g. folder counts for larger folders to show warnings)
  -- You could also take off any .xxx suffix before adding
  -- the file to the .zip if needed
  set theItem to selection as alias
  set itemPath to quoted form of POSIX path of theItem
  set fileName to name of theItem
  set theFolder to POSIX path of (container of theItem as alias)
  set zipFile to quoted form of (theFolder & fileName & ".zip")
  do shell script "zip -r -j " & zipFile & " " & itemPath
  -- delay 1 -- this may be needed (and adjusted) for larger folders?
  try -- We don't need to say anything if there are no .DS_Store files
    do shell script "zip -d " & zipFile & " '*.DS_Store'"
  end try
end tell
Wouldn't it be great if holding down the Option key while choosing Create Archive would exclude the special hidden Mac dot files?

[robg adds: If you're running 10.4, you can make this script a Finder plug-in using Automator; then it's just a control-click away with any file or folder selected. Open Automator, click on the Automator Library item, then drag the Run AppleScript action to the work area. Replace the (* Your script goes here *) line with the script above, and use File: Save as Plug-in to create a Finder plug-in (I called mine WinZipper). Select anything in the Finder, control-click, and then choose WinZipper from the Automator sub-menu.]
    •    
  • Currently 2.75 / 5
  You rated: 4 / 5 (4 votes cast)
 
[29,060 views]  

Create Windows-compatible zip files via AppleScript | 21 comments | Create New Account
Click here to return to the 'Create Windows-compatible zip files via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create Windows-compatible zip files via AppleScript
Authored by: Interactive on Sep 07, '05 09:29:25AM
I use CleanArchiver
    http://www.macupdate.com/info.php/id/17207
for this purpose.

[ Reply to This | # ]
No delay needed
Authored by: Jon on Jun 02, '09 02:54:25AM

The presumed delay is not needed in applescript (macventure's original). Applescript waits for the first shell command to be finished, then executes the second.



[ Reply to This | # ]
Off Topic
Authored by: bedouin on Sep 07, '05 09:34:46AM

Isn't it great that we don't have to deal with StuffIt anymore? The sooner .sits die the happier I will be.



[ Reply to This | # ]
Off Topic
Authored by: siMac on Sep 07, '05 10:12:56AM

...and as for .dmg.sit



[ Reply to This | # ]
Off Topic
Authored by: adrianm on Sep 07, '05 10:19:40AM

*shudder*



[ Reply to This | # ]
Off Topic
Authored by: thereitis on Sep 08, '05 02:19:11PM

Amen!



[ Reply to This | # ]
Create Windows-compatible zip files via AppleScript
Authored by: adrianm on Sep 07, '05 10:22:26AM

Not really "windows compatible", just without all the metadata/resource forks.

This might make the files unusable on a Windows box, making the title even more misleading.



[ Reply to This | # ]
Same, but using only Bourne shell
Authored by: davehole on Sep 07, '05 12:05:22PM
I wrote an example that does the same thing, but without any apple script.

Start a new workflow in Automator and add the Run Shell Script action. For the shell option select "/bin/sh" and for the Pass Input option select "as arguments". then paste in this shell script and save as a finder plugin. Does the same thing, but it's a good example of using the shell with automator


if [ $# == 1 ]; then
	zip -r -j "$1.zip" $1 -x \*.DS_Store
else
	((x=$#-1));
	zip -r -j "$1AND$x-files.zip" $* -x \*.DS_Store
fi

You can now select one file/folder of multiple file/folders in the finder and use your plugin from the automator menu

[ Reply to This | # ]

Same, but using only Bourne shell
Authored by: de291 on Sep 07, '05 01:17:00PM

Is the "fi" at the end of the script correct?



[ Reply to This | # ]
Same, but using only Bourne shell
Authored by: alblue on Sep 07, '05 04:55:32PM

Yes, fi is correct. Bash terminates the 'if' blocks with 'if' in reverse, so 'fi'. The same is true for 'case' statements that end 'esac'.

http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/bash.1.html



[ Reply to This | # ]
Same, but using only Bourne shell
Authored by: de291 on Sep 07, '05 08:33:56PM

Thanks
Too many scripting languages in my head.



[ Reply to This | # ]
add a slash
Authored by: mzs on Sep 07, '05 01:22:01PM
I suggest -x \*/.DS_Store, I assume that without the slash unlikely files like foobar.DS_Store would be excluded too :)

[ Reply to This | # ]
Create Windows-compatible zip files via AppleScript
Authored by: nickdynamite on Sep 07, '05 04:17:20PM

i get an odd error when i try to use this on more then 1 file selected - used the script from the original hint - is there a way to rewrite the script for say 2 items or more? and have that new .zip file be called "Archive.zip" by default just like the contextual menu item "Create Archive" does?



[ Reply to This | # ]
this will make Archive.zip
Authored by: davehole on Sep 07, '05 06:41:05PM
Can be used on one or more selected files. If more than one file is selected it creates an "Archive.zip" folder Like previous hint, make an automator "run shell script" action with shell set to "/bin/sh" and pass input as "arguments".


if [ $# == 1 ]; then
        zip -r -j "$1.zip" $1 -x \*.DS_Store
else
        x=`dirname $1`
        zip -r -j ${x}/Archive.zip $* -x \*.DS_Store
fi


[ Reply to This | # ]
Create Windows-compatible zip files via AppleScript
Authored by: Kosice on Sep 07, '05 04:25:00PM

If one could add extra code to manage the long file name encoding from MacOS roman encoding to Windows Latin 1 this would allow me to zip file with accents (้ รจ รง รค) in there names.

Then a script to unzip files and convert from latin 1 to MacOS Roman would be perfect...
This would put an end to a stupid cross platform and support issue with have...



[ Reply to This | # ]
Create Windows-compatible zip files via AppleScript
Authored by: baba on Sep 07, '05 10:33:08PM
It might be simpler to work on the command line if there're not many nested folders to deal with:
mkdir tempdir
cp *.txt tempdir/
zip -vr zipfilename.zip tempdir
then
rm -rf tempdir

'zipfilename.zip' will be at hand and ought not to contain any dot files.
I tested this via the finder, too, by just dragging items to my "tempdir". No .DS_Store was created.

Question: Just when *are* these created. Is it only when using 'icon view'? If so, that makes sense, since I use only list view.

[ Reply to This | # ]
Create Windows-compatible zip files via AppleScript
Authored by: bluehz on Sep 08, '05 06:11:53PM

This only seems to be addressing the issue of .DS_Store files appearing on Windows based PC's upon compression, which to me is only a minor nusiance to the end-user. What I consider to be a much more critical issue is when a PC user expands a Mac OS X compressed zip archive and they end up with 2 dirs inside the one main dir of the archive. Usually the 2nd dir is named "Mac OS X" or something to that effect and appears to contain duplicates of the real files located in the other dir. Now we as Mac users tend to understand those are remnants of resource enabled files from the Mac world but it gives the PC users no end of grief.

Does anyone have a solution for that problem?



[ Reply to This | # ]
Create Windows-compatible zip files via AppleScript
Authored by: Onirix on Sep 08, '05 11:14:33PM

I will have to say that I revert back to using Stuffit Delux. I use the Archive as zip option when right clicking and using the stuffit contextual menu. It makes zip files with only the .ds_store files. No directory garbage. Kind of ironic that my main use for Stuffit Delux is to zip files.

A shame to. It seems so simple to fix it in OsX. We can open windows zipped files. Why not just have the built-in archive feature zip as windows zip files all the time? I must be missing something.

Anyway, luckily I have stuffit, and patient windows friends.



[ Reply to This | # ]
Create Windows-compatible zip files via AppleScript
Authored by: haighy on Sep 12, '05 07:14:09PM
To eliminate the "MacOS X" folders you mentioned, you could try deleting the resource forks before zipping. The contextual menu plugin Grim Ripper makes it extremely easy. Be careful though; for certain files (particularly older ones, from pre OS X apps for example) you might lose crucial information, even making them unopenable. Best to make a backup if you're unsure.

[ Reply to This | # ]
Create Windows-compatible zip files via AppleScript
Authored by: geppo1982 on Sep 14, '05 10:22:50AM

I wrote an automator workflow to zip files from the Finder contextual menu without Mac resouces and .DS_Store files.

This workflow seems to work fine with single and multiple files/folders but it doesn't include in the zip archive any files or folders with .DS_Store at the end of the name (for example "My house.DS_Store"). I think this is not a big issue beacause I never had a file with .DS_Store in the name but I also think It's important for you to know this.

To download the file, copy and paste this address into your favourite web browser:

http://geppo1982.altervista.org/WinZipper.zip

Then copy the file into: ~/Library/Workflows/Applications/Finder

I also wrote a similar Automator workflow to create rar archives. If you like I'll post a link to download it.

Sorry for my bad English.



[ Reply to This | # ]
Create Windows-compatible zip files via AppleScript
Authored by: seventytwo on Oct 26, '05 05:21:37AM

Perfection!



[ Reply to This | # ]