尝试将文件复制到共享服务器时出错

时间:2014-03-12 16:41:43

标签: applescript

我试图将重复的主文件夹(桌面,电影,音乐,家庭等)发送到共享服务器。我终于能够使用字符串和变量的串联来安装驱动器。但现在我在复制线上收到错误。如果我手动安装驱动器,此代码可以正常工作,但在通过applescript安装驱动器时似乎无法正常工作

--Assigns variable to username
tell application "System Events"
    set user_name to name of current user
end tell

--Prompts for eraider as string
set returnedName to (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1 giving up after 5)

--Sets string as variable
set work_name to text returned of returnedName


--Sets department list
set deptList to {"dept1", "dept2", "dept3", "dept4"}

set dept_name to {choose from list deptList} as string

--Tells Finder to mount shared drive
tell application "Finder"
    mount volume ("smb://vserver/athletics/"&dept_name&"/"&work_name)
end tell

--Sets sever as variable
set vserver to POSIX file ("/Volumes/" & work_name)

--Copies Documents to Server
set source to POSIX file ("/Users/" & user_name & "/Documents")


tell application "Finder"

    duplicate source to vserver with replacing

end tell

我收到此错误

  

错误" Finder收到错误:AppleEvent处理程序失败。"数字-10000

在这一行:duplicate source to vserver with replacing

1 个答案:

答案 0 :(得分:0)

您的代码中有一些奇怪的东西。它们中的大多数不会影响您的错误,但应该纠正它们。例如,“mount volume”命令不是Finder命令,它是一个applescript命令,因此您不应该告诉Finder执行该命令。接下来,“选择文件”行...为什么你周围有括号?加上其他小东西。所以我清理了代码。

要解决您的具体问题,可能有两件事。首先,我在尝试复制文件之前使用if语句来确保磁盘已挂载。其次,“POSIX文件”命令也可能导致问题。这是一个不寻常的命令,因为它并不总是按照你期望的方式工作。因此,在此代码中,为了确保我们避免使用该命令的任何问题,我将其强制转换为文本,然后在复制命令中,我使用前面的“folder”一词将其放入正确的格式。

所以我没有测试这段代码,但它应该可行。我希望它有所帮助。

--Assigns variable to username
tell application "System Events" to set user_name to name of current user

--Prompts for eraider as string
set work_name to text returned of (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1 giving up after 5)

--Sets department list
set deptList to {"dept1", "dept2", "dept3", "dept4"}

set dept_name to item 1 of (choose from list deptList)

--mount shared drive
mount volume ("smb://vserver/athletics/" & dept_name & "/" & work_name)

if work_name is in (list disks) then
    --Copies Documents to Server
    set source to (POSIX file ("/Users/" & user_name & "/Documents")) as text
    tell application "Finder" to duplicate folder source to disk work_name with replacing
else
    display dialog "Disk " & work_name & " is not mounted"
end if

更好的方法是使用“path to”命令建立文档文件夹的路径。这样您就不需要使用“POSIX文件”甚至不知道用户名,因为“path to”命令会自动适用于当前用户。另外,我会更多地检查装载量的东西。所以这就是我写代码的方式......我也没有测试过这个!

--Prompts for eraider as string
set work_name to text returned of (display dialog "Enter Raider Name:" default answer "eRaider" buttons {"Continue"} default button 1 giving up after 5)

--Sets department list
set deptList to {"dept1", "dept2", "dept3", "dept4"}

set dept_name to item 1 of (choose from list deptList)

--mount shared drive
set isMounted to false
if work_name is not in (list disks) then
    mount volume ("smb://vserver/athletics/" & dept_name & "/" & work_name)

    -- wait for the volume to be mounted but only wait for a limited time before failing (10 seconds in this case)
    set inTime to current date
    repeat
        delay 0.2
        if work_name is in (list disks) then
            set isMounted to true
            exit repeat
        end if
        if (current date) - inTime is greater than 10 then exit repeat
    end repeat
else
    set isMounted to true
end if

--Copies Documents to Server
if isMounted then
    set source to path to documents folder
    tell application "Finder" to duplicate source to disk work_name with replacing
else
    display dialog "There was an error mounting disk " & work_name buttons {"OK"} default button 1
end if