File io 将文件复制到其他文件夹并使用当前日期重命名

File io 将文件复制到其他文件夹并使用当前日期重命名,file-io,copy,applescript,File Io,Copy,Applescript,我正在尝试重新创建我编写的VBScript,该脚本将文件复制到远程卷,然后使用当前日期重命名该文件。有一大堆谷歌(等等)的搜索结果,但我似乎无法让它安静下来。下面是我所拥有的,但是当我运行它时,它会返回error“Finder得到一个错误:Handler不能处理这个类的对象。”关于duplicate命令 set {month:mm} to (current date) set {day:d} to (current date) set {year:y} to (current date) set

我正在尝试重新创建我编写的VBScript,该脚本将文件复制到远程卷,然后使用当前日期重命名该文件。有一大堆谷歌(等等)的搜索结果,但我似乎无法让它安静下来。下面是我所拥有的,但是当我运行它时,它会返回
error“Finder得到一个错误:Handler不能处理这个类的对象。”
关于duplicate命令

set {month:mm} to (current date)
set {day:d} to (current date)
set {year:y} to (current date)
set theDate to mm & "-" & d & "-" & y
set fileName to theDate & ".xml"


tell application "Finder"
    duplicate POSIX file "/Users/RHPT/Documents/File.xml" to POSIX file "/Volumes/Public/Backup"
    set name of POSIX file ("/Volumes/Public/Backup/File.xml" as alias) to fileName
end tell
尝试:

编辑


使用do shell脚本也很有用

set theFile to "/Users/RHPT/Documents/File.xml"
set backUpPath to "/Volumes/Public/Backup"
set theDate to (do shell script "date "date '+%m-%d-%Y'")
do shell script "cp " & quoted form of theFile & space & quoted form of backUpPath & theDate & ".xml"

您的脚本有一些问题

调试的一种方法是将return语句放在其中,然后查看到目前为止所创建的内容。当您运行脚本时,它将在return语句处停止,您将在AppleScript编辑器窗口底部的结果窗格中看到返回的内容。像这样:

set {month:mm} to (current date)
set {day:d} to (current date)
set {year:y} to (current date)
set theDate to mm & "-" & d & "-" & y
set fileName to theDate & ".xml"
return fileName -- debugging line, remove after debugging
-- returns: {August, "-", 7, "-", 2014, ".xml"}
因此,在“theDate”变量中有一个列表,在“set fileName…”之后要做的是在该列表的末尾添加一个项目。在这些变量中需要的是文本。因此,在构建“theDate”变量的地方,将“as text”附加到该行:

set {month:mm} to (current date)
set {day:d} to (current date)
set {year:y} to (current date)
set theDate to mm & "-" & d & "-" & y as text
set fileName to theDate & ".xml"
return fileName -- debugging line, remove after debugging
-- returns: "August-7-2014.xml"
现在,在“设置文件名…”行中,您连接了两个文本字符串,您有了一个文本文件名,以后可以使用

当然,现在已经调试了脚本的上述部分,您可以去掉“returnfilename”语句,以便脚本继续下一行:

duplicate POSIX file "/Users/RHPT/Documents/File.xml" to POSIX file "/Volumes/Public/Backup"
由于您希望稍后使用复制的文件(更改其名称),因此应将复制命令的结果设置为变量:

set theDuplicatedFile to duplicate POSIX file "/Users/RHPT/Documents/File.xml" to POSIX file "/Volumes/Public/Backup"
此外,您不应该硬编码到文档文件夹的路径。它有一个特殊属性-“文档文件夹的路径”-这将使您的脚本能够为任何用户工作,而不仅仅是具有名为“RHPT”的主文件夹的用户-或者,如果您的用户名在下一台Mac上发生更改,则在将来为您提供。因此,请在变量声明中添加以下两行,并添加一条调试返回语句,以查看POSIX路径的外观:

set theDocumentsFolderPath to the path to the documents folder as text
set theDocumentsFolderPOSIXPath to the POSIX path of theDocumentsFolderPath
return theDocumentsFolderPOSIXPath -- debugging line, remove after debugging
-- returns: "/Users/RHPT/Documents/"
但问题是,POSIX路径对您没有多大好处,因为“复制”命令需要一个alias对象。在使用Finder时,您不必考虑路径,因为这不是bash或MS-DOS-Mac在对象中工作。因此,您真正想要的是:

set theDocumentsFolder to the path to the documents folder as alias
return theDocumentsFolder -- debugging line, remove after debugging
-- returns: alias "Macintosh HD:Users:RHPT:Documents:"
然后上面的“复制…”行变成:

set theDuplicatedFile to duplicate file "File.xml" of theDocumentsFolder to POSIX file "/Volumes/Public/Backup"
…但“重复”行仍存在更多问题

这:

…不是POSIX文件。它是一个文件夹路径。您希望复制的文件进入磁盘“Public”的“Backup”文件夹,对吗?您可以用简单的英语向Finder询问:

set theDuplicatedFile to duplicate file "File.xml" of theDocumentsFolder to the folder "Backup" of the disk "Public"
…如果文件已经存在,您可能应该告诉Finder不要替换该文件,以防万一:

set theDuplicatedFile to duplicate file "File.xml" of theDocumentsFolder to the folder "Backup" of the disk "Public" replacing no
然后你的最后一句话:

set name of POSIX file ("/Volumes/Public/Backup/File.xml" as alias) to fileName
…变成:

set the name of theDuplicatedFile to fileName
您的整个脚本如下所示:

set {month:mm} to (current date)
set {day:d} to (current date)
set {year:y} to (current date)
set theDate to mm & "-" & d & "-" & y as text
set fileName to theDate & ".xml"
set theDocumentsFolder to the path to the documents folder as alias

tell application "Finder"
    set theDuplicatedFile to duplicate file "File.xml" of theDocumentsFolder to the folder "Backup" of the disk "Public" replacing no
    set the name of theDuplicatedFile to fileName
end tell
您还可以稍微优化一下脚本。您将获取“当前日期”对象3次,而您只需要获取一次,这将占用三分之一的时间:

set theCurrentDate to the current date
set {month:mm} to theCurrentDate
set {day:d} to theCurrentDate
set {year:y} to theCurrentDate
您还可以选择重写一点脚本以提高可读性,因为AppleScript的一个关键特性是,即使对于不知道如何编写脚本的人来说,它也可以像普通英语一样可读:

set theCurrentDate to the current date
set theMonth to the month of theCurrentDate
set theDay to the day of theCurrentDate
set theYear to the year of theCurrentDate
set theFileNameDate to theMonth & "-" & theDay & "-" & theYear as text
set theDuplicatedFileName to theFileNameDate & ".xml"
set theDocumentsFolder to the path to the documents folder as alias

tell application "Finder"
    set theDuplicatedFile to duplicate file "File.xml" of theDocumentsFolder to the folder "Backup" of the disk "Public" replacing no
    set the name of theDuplicatedFile to theDuplicatedFileName
end tell

最后,除非您是bash的专家用户,否则我建议您不要使用shell脚本执行类似这样的简单操作。理想情况下,您不会使用“do shell script”命令以更安全的方式执行Finder可以为您执行的操作。请使用“do shell script”获取UNIX层独特功能的命令,如运行PHP函数或Perl脚本。Mac上在HFS+磁盘之间复制和重命名文件的最佳应用程序是Finder。

您是否尝试将posix file命令移到Finder块之外?
set {month:mm} to (current date)
set {day:d} to (current date)
set {year:y} to (current date)
set theDate to mm & "-" & d & "-" & y as text
set fileName to theDate & ".xml"
set theDocumentsFolder to the path to the documents folder as alias

tell application "Finder"
    set theDuplicatedFile to duplicate file "File.xml" of theDocumentsFolder to the folder "Backup" of the disk "Public" replacing no
    set the name of theDuplicatedFile to fileName
end tell
set theCurrentDate to the current date
set {month:mm} to theCurrentDate
set {day:d} to theCurrentDate
set {year:y} to theCurrentDate
set theCurrentDate to the current date
set theMonth to the month of theCurrentDate
set theDay to the day of theCurrentDate
set theYear to the year of theCurrentDate
set theFileNameDate to theMonth & "-" & theDay & "-" & theYear as text
set theDuplicatedFileName to theFileNameDate & ".xml"
set theDocumentsFolder to the path to the documents folder as alias

tell application "Finder"
    set theDuplicatedFile to duplicate file "File.xml" of theDocumentsFolder to the folder "Backup" of the disk "Public" replacing no
    set the name of theDuplicatedFile to theDuplicatedFileName
end tell