使用AppleScript将文件复制到具有新名称的其他文件夹

时间:2020-05-03 15:22:53

标签: duplicates applescript move

我正在尝试将文件复制到指定的文件夹,但是出现错误:

错误“ Finder出现错误:无法设置文件\” / Users / Andrew / Documents / Finances / Statement-银行和信用卡CSVs / 2020-05-01 2020年4月Report / Steps / 2020-05-01 ”,然后在2020年4月步骤01.xlsm \之前提交文件“ / Users / Andrew / Documents / Finances / Budget Reports / 2020-05-01 2020年4月.xlsm \”。文件“ / Users / Andrew / Documents / Finances / Statement-银行和信用卡CSVs / 2020-05-01 2020年4月Report / Steps / 2020-05-01 2020年4月1日之前编号-10006”

我必须使用AppleScript,因为我是从VBA内部启动脚本的。 AppleScript中确定3个变量的部分按预期工作。为简洁起见,代码如下所示:

DuplicateFileToStepsFolder("/Users/Andrew/Documents/Finances/Budget Reports/2020-05-01 April 2020.xlsm!/Users/Andrew/Documents/Finances/Statements - Bank And Credit Card CSVs/2020-05-01 April 2020 Report/Steps!BEFORE STEP 01")

on DuplicateFileToStepsFolder(ReportAndStepsPaths)

    --code to split ReportAndStepsPaths into separate strings

    set BudgetReportPath to "/Users/Andrew/Documents/Finances/Budget Reports/2020-05-01 April 2020.xlsm" --this is the file i want to duplicate
    set StepsFolderPath to "/Users/Andrew/Documents/Finances/Statements - Bank And Credit Card CSVs/2020-05-01 April 2020 Report/Steps" --this is the directory I want to duplicate (or copy and move to)
    set BudgetReportStepPath to "/Users/Andrew/Documents/Finances/Statements - Bank And Credit Card CSVs/2020-05-01 April 2020 Report/Steps/2020-05-01 April 2020 BEFORE STEP 01.xlsm" --this is the new file name

    tell application "Finder" to duplicate file BudgetReportPath to folder StepsFolderPath
    --line to rename the moved file would go here
end DuplicateFileToStepsFolder

复制,移动和重命名也可以解决我的问题。我没有尝试找出重命名行。我确定也没有文件名冲突。

1 个答案:

答案 0 :(得分:0)

问题是Finder不支持POSIX路径(斜杠分隔)。

要么用POSIX file将所有内容转换为HFS路径(用冒号分隔),要么仍然使用HFS路径。

这是HFS路径版本,用于指定基本文件夹“ Finances”以及相对的path to

set financesFolder to (path to documents folder as text) & "Finances:"

set BudgetReportFile to financesFolder & "Budget Reports:2020-05-01 April 2020.xlsm"
set StepsFolderPath to financesFolder & "Statements - Bank And Credit Card CSVs:2020-05-01 April 2020 Report:Steps:"
set BudgetReportStepName to "2020-05-01 April 2020 BEFORE STEP 01.xlsm"

tell application "Finder"
    set duplicatedFile to duplicate file BudgetReportFile to folder StepsFolderPath
    set name of duplicatedFile to BudgetReportStepName
end tell