使用AppleScript压缩文件夹

4

我有一个Applescript,它可以将所选项目压缩成文件夹,并使用该名称作为压缩包的名称。但问题是,当我解压缩时,它会保留所有路径,从用户(User)一直到上层目录。 就像这样:

Users:
   username:
      folder:
           folder:

我希望它只是这样简单:
folder:

以下是代码:

tell application "Finder"
    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 " & zipFile & " " & itemPath
end tell
2个回答

3

在你的zip命令中添加-j选项。换句话说,在“end tell”之前,更改脚本的最后一行为:

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

这将会告诉 zip 命令在制作压缩文件的目录结构时“丢弃”你所尝试压缩的路径。


如果您使用-j选项,当您有两个同名文件时会出现错误。 - Peter

2
tell application "Finder"
    set theItems to selection
    repeat with _a from 1 to (count of theItems)
        set theItem to (item _a of theItems) 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 repeat
end tell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接