使用 AppleScript 将文件复制到不同的文件夹并使用新的名称。

问题描述 投票:0回答:1

我试图将一个文件复制到指定的文件夹,但我得到一个错误。

error "Finder got an error. Can't set file \"UsersAndrewDocumentsFinancesStatements - Bank And Credit Card CSVs2020-05 April 2011 无法将文件 "UsersAndrewDocumentsFinancesStatements - Bank And Credit Card CSVs2020-05-01 April 2020 ReportSteps2020-05-01 April 2020 BEFORE STEP 01.xlsm/"设置为文件 "UsersAndrewDocumentsFinancesBudget Reports2020-05-01 April 2020. xlsm"/"。"编号-10006从文件 "UsersAndrewDocumentsFinancesStatements - Bank And Credit Card CSVs2020-05-01 April 2020 ReportSteps2020-05-01 April 2020 BEFORE STEP 01.xlsm"

我必须使用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

复制、移动和重命名也能解决我的问题. 我还没试着去弄清楚重命名这一行。我确信也没有文件名的碰撞。

duplicates applescript move
1个回答
0
投票

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

要不你就用 POSIX file 或者你还是使用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
© www.soinside.com 2019 - 2024. All rights reserved.