简体   繁体   English

Apple Script用于压缩文件

[英]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: 我只是试图建立一个执行以下操作的非常基本的applescript:

  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 使用命令“ 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
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. 这可以完成您描述的所有操作,但是我不知道-x是什么,并且对其进行谷歌搜索也无济于事。 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'. 正如您可能已经意识到或尚未意识到,Apple有两个用于选择计算机上内容的单独命令,即“选择文件”和“选择文件夹”。 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. 无论如何,无论我最后要解决的想法是什么,现在都给了我旋转的沙滩球,我将要强制退出它,我认为未保存的脚本将消失,并且我真的不想从头开始,但是这个想法本身很简单,您可以执行“设置主题以选择允许多选的文件”之类的内容,然后对“重复项目中的主题进行重复”,然后在重复循环中放入“将主题移动到( 将路径有一个文件夹 )”,然后,而不是尽一切努力‘theitem’你做给你把一切都在该文件夹。

Anyways this does steps 1 and 2. 无论如何,这会执行步骤1和2。

The -r option is doing recursive zip for all sub/sub folders. -r选项为所有子/子文件夹执行递归zip。

The -j option removes , in zip archive, any reference to initial directories. -j选项在zip归档文件中删除对初始目录的任何引用。 just file names. 只是文件名。

The -X option removes all extra fields attributes usually stored in zip file (some attributes are specific to some OS) -X选项删除通常存储在zip文件中的所有其他字段属性(某些属性特定于某些操作系统)

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 : 下面的脚本与Pau的脚本非常相似(我有点慢!),但是如果将其保存为应用程序,则可以通过以下两种方式使用它:

1) you can select files and folders and drop them on this application. 1)您可以选择文件和文件夹并将其放在此应用程序上。 all items dropped will be combine in a single zip file. 所有删除的项目将合并到一个zip文件中。 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). 2)您可以运行该应用程序,要求您选择一个文件夹并提示您输入最终zip文件的名称(默认情况下,该文件夹位于要压缩的文件夹的父文件夹中)。

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. 这样,您可以合并在ZIP文件和文件夹中。

Last, but not least, the Finder already provide this zip function with right click after files/folders selection. 最后但并非最不重要的一点是,Finder在选择文件/文件夹后已经可以通过右键单击提供此zip功能。 But in this script, you can set he -jX options. 但是在此脚本中,您可以设置-jX选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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