【问题标题】:Use AppleScript to Duplicate a File to a Different Folder with a New Name使用 AppleScript 将文件复制到具有新名称的不同文件夹
【发布时间】:2020-05-03 15:22:53
【问题描述】:

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

错误“Finder 出错:无法设置文件\”/Users/Andrew/Documents/Finances/Statements - Bank And Credit Card CSVs/2020-05-01 April 2020 Report/Steps/2020-05-01 2020 年 4 月在步骤 01.xlsm\" 之前归档 \"/Users/Andrew/Documents/Finances/Budget Reports/2020-05-01 April 2020.xlsm\"。"编号 -10006 来自文件“/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”

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

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】:

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

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

    这是 HFS 路径版本,它使用相对 path to 指定基本文件夹“Finances”

    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
    

    【讨论】:

      猜你喜欢
      • 2010-10-28
      • 2014-11-08
      • 2022-10-05
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多