0
votes

I am attempting to duplicate a file to a specified folder but I get an error:

error "Finder got an error: Can’t set file \"/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\" to file \"/Users/Andrew/Documents/Finances/Budget Reports/2020-05-01 April 2020.xlsm\"." number -10006 from file "/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"

I have to use AppleScript because I am initiating the script from within VBA. the part of the AppleScript that determines the 3 variables works as intended. For brevity the code looks like this:

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

Copy, move and rename would also solve my problem. I haven't tried to figure out the rename line. I am sure that there are no file name collisions either.

1

1 Answers

0
votes

The problem is the Finder doesn't support POSIX paths (slash separated).

Either you convert everything to HFS paths (colon separated) with POSIX file or you use HFS paths anyway.

This is the HFS path version which specifies the base folder "Finances" with a relative 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