使用AppleScript移动整个文件夹

时间:2018-12-15 06:53:47

标签: applescript directory

这是我对AppleScript的首次使用。我正在尝试使用它将100多个文件夹(及其内容文件)从一个位置移动到另一个位置。我不只是移动文件,因为我想维护文件系统。因此,我在.txt文件中列出了以下格式的文件夹:

  

Macintosh HD:用户:Tom:音乐:iTunes:Afrika Bambaataa

     

Macintosh HD:用户:Tom:音乐:iTunes:Air

然后在AppleScript中运行以下命令:

tell application "Finder"
    set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
    set My_Folder to (choose folder with prompt "Select your destination folder")
    set List_files to paragraphs of (read TextFile)
    move List_files to My_Folder
end tell

但是我收到的错误如下:

  

错误“ Finder出现错误:处理程序无法处理此类的对象。”编号-10010

对我在这里做错的任何帮助吗?

非常感谢!

1 个答案:

答案 0 :(得分:1)

@model IList<BookStore.Models.Book>

当前,@model List<BookStore.Models.Book> 只是文本对象的列表,即

tell application "Finder"
    set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
    set My_Folder to (choose folder with prompt "Select your destination folder")
    set List_files to paragraphs of (read TextFile)
    move List_files to My_Folder
end tell

所以您要 Finder 要做的是将一些文本移动到文件夹中,这没有任何意义。您需要使用List_files说明符来告诉 Finder 该文本代表文件夹的路径。由于无法整体完成此操作,因此您必须遍历列表:

{"Macintosh HD:Users:Tom:Music:iTunes:Afrika Bambaataa",
 "Macintosh HD:Users:Tom:Music:iTunes:Air", ...}

但是,我实际上不会这样做,因为这将需要100多个单独的folder命令-repeat with fp in List_files move folder fp to My_Folder end repeat 中的每个项目一个。取而代之的是,我们将首先在列表项之前添加move修饰符,然后在单个命令中先 then List_files来编辑列表项:

folder

根据文件的大小以及传输需要多长时间,脚本可能会在传输完成之前超时。老实说,我不确定这会对现有文件传输产生什么影响。我怀疑,AppleScript只会失去与 Finder 的连接,但是由于命令已经发出,因此传输仍将继续(使用单个{{1 }}命令而不是倍数)。但是,如果您想避免发现错误,我们可以延长超时时间以确保安全:

move

最终的脚本如下所示:

repeat with fp in List_files
    set fp's contents to folder fp
end repeat

move List_files to My_Folder

move开头的多余行只是为了安全起见,以防文本文件的最后一行是空白行(通常是这种情况)。这将检查with timeout of 600 seconds -- ten minutes move List_files to alias "Macintosh HD:Users:CK:Example:" end timeout 是否包含一个空字符串作为最后一项,如果是,则将其删除;否则,将其删除。保留它会在稍后的脚本中引发错误。

编辑:处理不存在的文件夹

如果tell application "Finder" set TextFile to (choose file with prompt "Select your text file" of type {"txt"}) set My_Folder to (choose folder with prompt "Select your destination folder") set List_files to paragraphs of (read TextFile as «class utf8») if the last item of the List_files = "" then set ¬ List_files to items 1 thru -2 of List_files repeat with fp in List_files set fp's contents to folder fp end repeat move List_files to My_Folder end tell 循环由于无法识别的文件夹名称而引发错误,那么我们可以从列表中排除该特定文件夹。但是,如果我们创建一个仅包含已验证文件夹(即未引发错误的文件夹)的新列表,则会更容易:

if the last item of...

这也意味着我们可以删除List_files开头的行,因为现在将捕获repeat ... set verified_folders to {} repeat with fp in List_files try set end of verified_folders to folder fp end try end repeat move the verified_folders to My_folder 错误捕获块来代替对其执行的检查