你如何使用Fetch和applescript下载所有文件?

时间:2012-02-17 09:28:10

标签: applescript fetch

我有以下脚本(已修改以删除任何私人信息)。

-- 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

我得到的错误如下:

  

错误“获取错误:无法获取窗口的每个远程项目(传输窗口ID 232280960)。”从窗口的每个远程项目(传输窗口ID 232280960)编号-1728

有谁知道错误的含义或如何解决?我没有太多运气就尝试了Fetch网站。 “获取”btw是Fetch FTP客户端。

2 个答案:

答案 0 :(得分:2)

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

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

  

下载说明符:要下载的远程文件,远程文件夹,快捷方式或网址

这就是你需要遍历项目的原因。 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

答案 1 :(得分: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 ...)。
  3. 这应该有效:

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