Codename K
Codename K

Reputation: 744

AppleScript to compress a folder without folder structure?

I am trying to zip the entire files inside a folder using AppleScript. I use this code,

tell application "Finder"
    set x1 to (path to home folder as text)
    set theItem to x1 & "Documents:MyFolder" 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 " & zipFile & " " & itemPath
end tell

This works OK. But it zips the entire folder structure like this inside the zip file,

Users:MyUser:Documents:MyFolder:

I want to just zip the entire files inside the folder without this folder structure. I mean not even the MyFolder. Just the files inside to a zip file.

?

Upvotes: 0

Views: 1102

Answers (1)

vgmoose
vgmoose

Reputation: 443

Here's a quick modification that will create zip all of the files inside of itemPath and output them to zipFile:

tell application "Finder"
    set x1 to (path to home folder as text)
    set theItem to x1 & "Documents:MyFolder" 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 "cd " & itemPath & ";zip -r " & zipFile & " *"
end tell

If you run this as is, it will create MyFolder.zip in Documents containing the contents of MyFolder.

The trick going on here is the script is cd-ing into the folder first, and then recursively call zip on *, which represents all of the files in the current directory (which is the target to be zipped).

Upvotes: 1

Related Questions