user1313051
user1313051

Reputation: 39

Apple Script for zipping a file

I've had a look at 2 of the example scripts on here to do a similar thing however both dont really fit the use case and i cant seem to butcher them to work!

I'm simply trying to set up a really basic applescript that does the following:

  1. asks the user to select files & folders as the input source.
  2. asks the user to select the output destination and filname
  3. Uses the command "zip -r -j -X

Would like it to run an an application on the desktop or possible a contect menu from a right mouse click... But i'll take anything right now! Can anyone help with this?

I was trying to adapt this script if it helps?

set workspace to (path to desktop as text)

tell application "Finder"
   set inputFolder to choose folder with prompt "Select a folder to be zipped"

   set copiedFile to (duplicate inputFolder to workspace) as string
   set copiedFile to text 1 thru -2 of copiedFile --remove the trailing ":" 

   tell current application
      set qpp to quoted form of POSIX path of copiedFile
      do shell script "cd $(dirname " & qpp & ")
        zip -r  -j -X \"$(basename " & qpp & ").zip\" \"$(basename " & qpp & ")\""
     set zipFile to copiedFile & ".zip"
   end tell

   set outputFolder to choose folder with prompt "Select the output folder"
   move zipFile to outputFolder                                                          
end tell

Upvotes: 0

Views: 1665

Answers (2)

pbell
pbell

Reputation: 3095

The -r option is doing recursive zip for all sub/sub folders.

The -j option removes , in zip archive, any reference to initial directories. just file names.

The -X option removes all extra fields attributes usually stored in zip file (some attributes are specific to some OS)

Script bellow is very similar to Pau's script (I am bit slow !) but if you save it as an application you can use it in 2 ways :

1) you can select files and folders and drop them on this application. all items dropped will be combine in a single zip file. This is the On Open handler.

2) you can run the application which will ask you to select a folder and will prompt to ask you name of final zip file (by default in parent folder of the folder to be zipped).

on open Droped_Items -- could be folders or files
set ZipFile to (choose file name with prompt "Define your zipped file" default name "") as string

repeat with MyItem in Droped_Items
    do shell script "zip -rjX " & (quoted form of (POSIX path of ZipFile)) & ".zip " & quoted form of (POSIX path of (MyItem as string))
end repeat
end open


on run
set inputFolder to choose folder with prompt "Select a folder to be zipped" 
tell application "Finder"
    set Myfolder to (container of inputFolder) as alias -- get the parent folder
    set MyName to name of inputFolder
end tell
set ZipFile to (choose file name with prompt "Define your zipped file" default name MyName default location Myfolder) as string
do shell script "zip -rjX " & (quoted form of (POSIX path of ZipFile)) & ".zip " & quoted form of (POSIX path of (inputFolder as string))
end run

Doing so, you can combine in ZIP files AND folder.

Last, but not least, the Finder already provide this zip function with right click after files/folders selection. But in this script, you can set he -jX options.

Upvotes: 0

pau
pau

Reputation: 166

tell application "Finder"
    set theItem to choose file --i had no problems when i changed this to choose folder

    set itemPath to quoted form of POSIX path of theItem
    set fileName to name of theItem

    set thefolder to choose folder

    set thefolder to POSIX path of thefolder
    set zipFile to quoted form of (thefolder & fileName & ".zip")

    do shell script "zip -jr " & zipFile & " " & itemPath
end tell

This does everything you described, but i don't know what -x is and googling for it isn't helping. Does that delete the original item?

As you may or may not have already realized, apple has two separate commands for choosing things on your computer, 'choose file', and 'choose folder'. As their names would suggest, you dont seem to be able to choose both with either. It makes sense too, as there are times when you might want to make sure a user is/isnt selecting a file. Having said that, there seems to be a way around it documented here. The first answer seems a bit extreme, but i like the bottom answer's idea of making a script that you'd simply drag the files onto. What he wrote suggests its easy if you do it that way.

I was also able to use "with multiple selections allowed" and then i was able to move everything into a folder, but for some reason at that point i started having problems, no idea why, either. Maybe i changed something in the code by accident, cause youd think zipping a folder was an identical process to zipping a file. Anyways whatever my last idea for solving it was is now giving me the spinning beach ball and i'm about to force quit it, i assume the unsaved script is going to be gone and i dont really want to start from scratch, but the idea itself is simple, you do something like "set theitems to choose file with multiple selections allowed" and make a bit about "repeat with theitem in the items" and inside that repeat loop you just put "move theitem to (you put the path to a folder there)" then instead of doing everything to "theitem" you do it to that folder you put everything in.

Anyways this does steps 1 and 2.

Upvotes: 1

Related Questions