简体   繁体   中英

Zip a folder using apple script

I am attempting to use applescript to copy a folder to my desktop, zip it, then move the .zip file elsewhere but I can't get the zipping part to work.

I've looked everywhere for ways to zip a file/folder in applescript and I don't know what I'm doing wrong but none of them have worked for me.

I would also rather not have to choose the folder after it's been copied and the folder after it's been zipped but I thought I'd leave them till the zipping is fixed.

Any help at all would be greatly appreciated.

This is my code: (updated after help from djbazziewazzie )

set workspace to (path to desktop as text) --"Users:produser:Desktop"

tell application "Finder"

display dialog "Select a folder to be zipped"
set inputFolder to choose folder
set inputFolderProperties to properties of inputFolder
set inputFolderName to name of inputFolder

duplicate inputFolder to workspace with properties --copy input folder to workspace
{name:inputFolderName} --keep the same name

--set copiedFile to (workspace & inputFolderName) 
display dialog "Select the folders desktop copy"
set copiedFile to choose folder --select the file copy thats on the workspace

tell current application
    set qpp to quoted form of POSIX path of copiedFile
    do shell script "zip -r " & qpp & ".zip " & qpp -- zips the file (or not...)
end tell

display dialog "Select the .zip file" --select the new .zip file
set zipFile to choose file
display dialog "Select the output folder"
set outputFolder to choose folder --moves zipped file
move zipFile to outputFolder

end tell   

Applications are directories so you need the -r option with zip to add all the files of the folder to the zip files. In Mac OS X directories ending with .app are shown as files instead of folders.

Also using a do shell script inside an tell application "Finder" violates the scripting addition security policy. do shell script should only be used when the target is set to the constant current application . Every code that isn't targeted to an application is by default targeted to current application

tell current application
 do shell script "zip -r " & qpp & ".zip " & qpp -- zips the file (or not...)
end tell

EDIT 1: showing working code

EDIT 2: Updated the do shell script to work with relative paths

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  \"$(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM