2012-02-17 71 views
0

我有以下脚本(修改为删除任何私人信息)。如何使用Fetch和applescript下载所有文件?

-- This line is for testing. 
set the clipboard to "1234567890" 

set loginName to "username" 
-- Password is stored in KeyChain (you need to do manually). 

-- Create Remote path 
set folderNumber to the clipboard as string 
set subdir1 to character 1 of folderNumber 
set subdir2 to character 2 of folderNumber 

set remotePath to "/files/" & subdir1 & "/" & subdir2 & "/" & folderNumber 

-- Create Local path 
set homeFolder to (path to home folder) as string 
set localPath to homeFolder & "LOCALSTORAGE" as string 
set localStorage to localPath & ":" & folderNumber & ":" as string 

-- Create Local file 
tell application "Finder" 
    try 
     make new folder at localPath with properties {name:folderNumber} 
    on error e number n 
     -- Do nothing. 
    end try 
end tell 

-- Connect to FTP 
tell application "Fetch" 
    activate 
    set tWindow to make new transfer window at beginning with properties {hostname:"ftpServerAddress", username:loginName, initial folder:remotePath} 

    tell window tWindow 
     download every remote item to beginning of alias localStorage 
     close window 
    end tell 

    quit 
end tell 

-- Open folder 
tell application "Finder" 
    open localStorage 
end tell 

当我运行脚本时,下面一行失败。

download every remote item to beginning of alias localStorage 

我得到的错误如下:

error "Fetch got an error: Can’t get every remote item of window (transfer window id 232280960)." number -1728 from every remote item of window (transfer window id 232280960)

有谁知道什么错误意味着或如何解决呢?我尝试了Fetch网站,但没有多少运气。 “取”btw是取FTP客户端。

回答

2

首先,你应该检查你正在生成的remotePath确实存在(例如,通过添加log的语句,如log tWindow's remote items和脚本编辑器的事件日志中查找是否能得到这些)。

如果路径是正确的,我认为问题在于您使用的download命令引用了一个列表对象(every remote item...)。在文档中,命令期望单个项目的说明符:

download specifier : the remote file, remote folder, shortcut, or url to download

这就是为什么您需要遍历项目。 The snippet below完美的作品对我来说:

-- my settings for testing 
set theHost to "ftp.fetchsoftworks.com" 
set loginName to "anonymous" 
set remotePath to "/example/" 
set localStorage to ((path to home folder) as text) & "LOCALSTORAGE:1234567890:" 

-- Connect to FTP 
tell application "Fetch" 
    activate 
    set tWindow to make new transfer window at beginning with properties {hostname:theHost, username:loginName, initial folder:remotePath} 
    set localStorage to (localStorage as alias) 
    repeat with theItem in tWindow's remote items 
     try 
      download theItem to localStorage 
     end try 
    end repeat 

    close tWindow 
    quit 
end tell 
+0

谢谢!这样可行。我还发现你可以记录“镜像文件夹”选项的AppleScript。从那里我得到了一个指令来把所有东西都拉过来。 – 2012-02-17 10:58:10

1

有传递名单download没有问题。但有两个问题与原来的代码:

tell window tWindow 
    download every remote item to beginning of alias localStorage 
    close window 
end tell 
  1. tell块指示封闭命令到一个通用的window对象,而不是一个transfer window,和通用window对象不包含远程项目。
  2. download命令的to参数应该是别名,而不是插入位置(例如beginning of ...)。

这应该工作:

tell tWindow 
    download every remote item to alias localStorage 
    close 
end tell 
+0

谢谢吉姆。这为我解决了另一个问题。 :) – 2012-02-17 16:35:08