Search code examples
bashapplescript

Trying to get AppleScript to create a proper zip file


Script needs to do the same as a right click > "Compress" in the Finder does.

tell application "Finder"
    set myDir to choose folder
    set myParentDir to the parent of myDir as alias
    set myParentPath to POSIX path of myParentDir
    set myDirName to the name of myDir
    set myZipName to "'" & myDirName & ".zip'"
    set myDirName to the quoted form of myDirName
    set myParentPath to the quoted form of myParentPath
    set myShellScript to "cd " & myParentPath & " | zip -r " & myZipName & " " & myDirName
    tell current application
        do shell script myShellScript
    end tell
end tell

if I copy the string saved in myShellScript and run it in Terminal it works perfectly but when run from the script editor it fails with the following:

error " zip warning: name not matched: IDA template Folder
zip error: Nothing to do! (try: zip -r IDA template Folder.zip . -i IDA template Folder)" number 12

Any suggestions?


Solution

  • You need to change the pipe | in myShellScript into a newline \n, so: set myShellScript to "cd " & myParentPath & "\n zip -r " & myZipName & " " & myDirName

    NB: The alternative - and the way it will show in Script Editor when it's compiled - is simply to type a literal line feed:

    set myShellScript to "cd " & myParentPath & "
     zip -r " & myZipName & " " & myDirName