0

我一直在使用 applescript 和 rsync 开发一个简单的自动备份实用程序,并且除了另外 1 个问题之外,一切正常,在启动脚本时,我希望能够选择多个硬盘驱动器进行备份(这工作),之后,我只允许选择 1 个备份驱动器,问题是,我希望所有选定的驱动器都驻留在备份驱动器上自己的文件夹中(请参阅随附的屏幕截图),但是我应该如何做到这一点有点迷失(我是新手到 applescript 和 rsync,通过大量研究才走到这一步,但现在卡住了)

这是我拥有的代码,所有驱动路径都在“源”变量中,我在 for 循环中对其进行迭代。

    set sources to (choose folder with prompt "Select drives to back up" default location "Volumes" with multiple selections allowed)
    set destination to (the POSIX path of (choose folder with prompt "Select destination volume" default location "Volumes"))

    #for loop iterating all sources to backup
    repeat with aliasPath in sources

        set source to the POSIX path of aliasPath

        set sciptsFinished to false

        tell application "Terminal"
            if not (exists window 1) then reopen
            activate
            do script "rsync --dry-run --archive -v --progress --exclude=\".*\"  --delete --delete-during --stats --human-readable --human-readable " & quoted form of source & " " & quoted form of destination in window 1

            delay 5 #give terminal time to activate
            repeat while (window 1 is busy)
                delay 1
            end repeat

            do script "osascript -e 'display notification \"RSYNC file changes calculated.\" with title \"RSYNC Backup\"'" in window 1

            #mark scripts completed
            set scriptsFinished to true

        end tell

        #wait for scripts to finish before proceeding
        repeat while (scriptsFinished is false)
            delay 2
        end repeat

任何帮助将不胜感激(如果您对代码有意见,请随时启发我,因为我仍然是 applescript 的菜鸟)

提前致谢!

要备份的来源(可多选)

目标驱动器(仅允许选择 1 个)

4

1 回答 1

0

使用一个小技巧解决了这个问题:

从选择文件夹返回并转换为 POSIX 路径的路径有一个结束正斜杠,例如 Volumes/Data Drive X/

但是,当不提供最后一个斜杠时,路径目标处应该有一个同名的文件夹(如果不可用,则创建),这正​​是我所需要的。

要从路径中删除最后一个斜杠,我只是在将别名转换为 POSIX 路径后添加了以下内容: set source to text 1 thru -2 of source

这样 Volumes/Data Drive X/ 就变成 了Volumes/Data Drive X

希望这可以帮助任何有同样问题的人!:)

于 2017-12-07T00:19:00.983 回答