2012-02-17 69 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