我正在尝试重新创建一个我写的VBScript,它将文件复制到远程卷,然后使用当前日期重命名该文件。有一堆谷歌(和SO)的结果,但我似乎无法让它安静。我的内容如下,但是当我运行它时,它会在复制命令上返回error "Finder got an error: Handler can’t handle objects of this class."
。
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
答案 0 :(得分:2)
尝试:
set {month:mm, day:d, year:y} to (current date)
set theDate to mm & "-" & d & "-" & y as text
set fileName to theDate & ".xml"
tell application "Finder"
set dupeFile to duplicate POSIX file "/Users/RHPT/Documents/File.xml" to POSIX file "/Volumes/Public/Backup"
set dupeFile's name to fileName
end tell
修改
set {month:mm, day:d, year:y} to (current date)
set theDate to mm & "-" & d & "-" & y as text
set fileName to theDate & ".xml"
set sourceFile to POSIX file "/Users/RHPT/Documents/File.xml"
set destFolder to POSIX file "/Volumes/Public/Backup"
tell application "Finder"
set dupeFile to duplicate sourceFile to destFolder
set dupeFile's name to fileName
end tell
答案 1 :(得分:0)
使用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"
答案 2 :(得分:0)
您的脚本存在一些问题。
调试的一种方法是在其中放置一个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"
现在,在“set fileName ...”行中,您将连接2个文本字符串,并且您有一个文本文件名,以后可以使用。
当然,既然您已经调试了脚本的上述部分,您可以取出“return fileName”语句,以便脚本继续到下一行:
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。因此,在您的变量声明中添加以下2行,并使用调试返回语句来查看您的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路径不会对你有好处,因为“复制”命令需要一个别名对象。使用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"
...但“重复”行还有更多问题。
此:
to POSIX file "/Volumes/Public/Backup"
...不是POSIX文件。这是一个文件夹路径。您希望复制的文件进入磁盘“公共”的“备份”文件夹,对吗?你可以用简单的英语问问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脚本做这样的简单事情。理想情况下,您不会使用“执行shell脚本”命令来执行Finder可以更安全地为您执行的操作。使用“do shell script”命令获取UNIX层的独特功能,例如运行PHP函数或Perl脚本。 Mac上用于在HFS +磁盘之间复制和重命名文件的最佳应用程序是Finder。