如何使用在所述USB上运行的Apple脚本将文件复制到USB?

时间:2016-10-28 16:57:21

标签: macos applescript

我在丹佛大学经营一个计算机实验室。我们的备份系统存在问题,我需要将(Apple)计算机上的所有文件复制/复制到我保留在桌面上的USB上。

目前,我需要将USB插入每台计算机并手动复制"文件中的每一项"文件夹并将它们存放在USB上的新文件夹中。我需要一个Apple脚本放在我的USB上,将USB插入计算机,激活应用程序,它将创建一个名为"(用户名)的新目录上传",并存入所有项目进入该目录。以下是我到目前为止的情况:

tell application "Finder"
set theFolder to disk / Volumes / Lexar / stuff
set Files1 to Users / matanya / documents
tell application "Finder"
    try
        duplicate file Files1 to theFolder
    on error
        tell application "Finder"
            display dialog "Transfer Failed"
        end tell
    end try
end tell

其中一个问题是每次我运行脚本时,都会收到一个错误,上面写着变量" Volumes"没有定义。另一个是我担心当我将这个脚本插入另一台计算机时,它不会找到文件夹" matanya"我在我的目录中。有没有办法称之为" home"还是什么?

1 个答案:

答案 0 :(得分:0)

首先,Finder仅接受HFS路径 - 以磁盘名称开头并以冒号分隔。使用HFS路径时,文件夹Volumes无关紧要。

path to documents folder始终指向当前用户的文件夹Documents

set theFolder to "Lexar:stuff:"
set documentsFolder to path to documents folder
tell application "Finder"
    if not (exists disk "Lexar") then
        display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
    end if
    try
        duplicate documentsFolder to folder theFolder
    on error
        display dialog "Transfer Failed"
    end try
end tell

此版本更复杂,会创建一个文件夹,其中包含当前用户的短用户名,并将文档文件夹复制到该单个文件夹

set theFolder to "Lexar:stuff:"
set currentUser to short user name of (system info)
set documentsFolder to path to documents folder
tell application "Finder"
    if not (exists disk "Lexar") then
        display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
    end if
    if not (exists folder currentUser of folder theFolder) then
        make new folder at folder theFolder with properties {name:currentUser}
    end if
    try
        duplicate documentsFolder to folder currentUser of folder theFolder
    on error
        display dialog "Transfer Failed"
    end try
end tell

使用launchd,您甚至可以在插入USB驱动器时自动运行此脚本。