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

发布于 2025-01-07 02:01:44 字数 1615 浏览 0 评论 0原文

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

-- 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)。”窗口每个远程项的编号 -1728(传输窗口 id 232280960)

有谁知道错误意味着什么或如何修复它?我尝试过 Fetch 网站,但运气不佳。顺便说一句,“Fetch”是 Fetch FTP 客户端。

I have the following script (modified to remove any private information).

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

When I run the script the following line fails.

download every remote item to beginning of alias localStorage

The error I get is as follows:

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)

Does anyone know what the error means or how to fix it? I've tried the Fetch website without much luck. "Fetch" btw is the Fetch FTP client.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落在眉间の轻吻 2025-01-14 02:01:44

首先,您应该检查您生成的 remotePath 是否确实存在(例如,通过添加 log 语句,例如 log tWindow's Remote items 并查看在脚本编辑器的事件日志中查看是否能够获取这些)。

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

下载说明符:要下载的远程文件、远程文件夹、快捷方式或 URL

这就是为什么您需要循环访问这些项目。 下面的代码片段非常适合我:

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

First you should check that the remotePath that you're generating really exists (e.g. by adding a log statement such as log tWindow's remote items and looking up in the Script Editor's event log whether it was able to get those).

If the path is correct, I think the problem is that you're using the download command with a reference to a list object (every remote item...). In the documentation, the command expects a specifier of a single item:

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

That's why you need to loop through the items. The snippet below works perfectly for me:

-- 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
夢归不見 2025-01-14 02:01:44

将列表传递给下载没有问题。但原始代码存在两个问题:

tell window tWindow
    download every remote item to beginning of alias localStorage
    close window
end tell
  1. tell 块将所包含的命令定向到通用 window 对象,而不是传输窗口,并且通用 window 对象不包含远程项目。
  2. download 命令的to 参数应该是别名,而不是插入位置(例如beginning of ...)。

这应该有效:

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

There's no problem passing a list to download. But there are two problems with the original code:

tell window tWindow
    download every remote item to beginning of alias localStorage
    close window
end tell
  1. The tell block directs the enclosed commands to a generic window object, rather than a transfer window, and the generic window object does not contain remote items.
  2. The download command's to parameter should be an alias, not an insertion location (e.g. beginning of ...).

This should work:

tell tWindow
    download every remote item to alias localStorage
    close
end tell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文