【问题标题】:Using automator and AppleScript to move a file to a different folder and subfolder based on name and content of a file使用 automator 和 AppleScript 根据文件的名称和内容将文件移动到不同的文件夹和子文件夹
【发布时间】:2020-09-21 20:32:57
【问题描述】:

我是 Automator 和 Scripting 的新手... 我已经阅读了很多与我的问题有点相似的答案,但我没有成功适应 Automator + AppleScript。

这是我想做的:

  • 当我将文件下载到目录/Volumes/Macboot /Downloads 时,(是的,硬盘名称中有空格),例如statement_EUR_2020-05-01_2020-05-31.pdf.

  • 我验证文件是否带有扩展名 pdf + 它包含 IBAN + 名称包含“语句”。

  • 如果文件对应,我想验证名称中的年份和月份,并相应地将其移动到好的 Google Drive 文件夹中:

    /Volumes/Macboot /Travail en cours/Google Drive/Company/Comptabilité/**2020**/**05**/Compte Transferwise 1/
    

目前,我成功获取了 2 个变量中的年份和月份,但是在 Automator 的下一步中我找不到使用变量移动文件的好方法。

【问题讨论】:

    标签: macos automation scripting applescript automator


    【解决方案1】:

    我已经简化了流程,并找到了使用 AppleScript 的方法:

    on run {input, parameters}
        set theFile to input as text
        set yearName to ((characters 34 thru -1 of theFile) as string) --trim first 35
        set yearName to ((characters 1 thru -22 of yearName) as string) --trim last 23
        set monthName to ((characters 39 thru -1 of theFile) as string)
        set monthName to ((characters 1 thru -19 of monthName) as string)
        set destinationFolder to ("Macboot :Travail en cours:Google Drive:Company:Comptabilité:" & yearName & ":" & monthName & ":Compte Transferwise 1:Relevé PDF + fichier CSV:" as text)
        tell application "Finder"
            activate
            move theFile to destinationFolder -- use "copy source_file to folder (target_folder as alias)" to copy the files
        end tell
        set result to {yearName, monthName, theFile, destinationFolder}
        return result
    end run
    

    【讨论】:

      【解决方案2】:

      以下应该有效,或者至少让您走上正确的道路。为变量设置以下内容:

      • monthName:一个text变量,保存月份值,如上
      • yearName:一个text 变量,保存年份值,如上所述
      • filePath:一个storage 变量,保存对您正在处理的文件的引用
      • outputFolder:一个storage 变量,它将保存对输出文件夹的引用

      前两个动作从存储中收集月份和年份,并将其作为输入变量中的列表传递给 AppleScript 动作。 AppleScript 操作从列表中提取值,将它们连接成路径字符串,然后使用POSIX File 命令将其转换为文件引用。第四个操作将文件引用存储在 outputFolder 变量中。

      请注意,第五个动作忽略了第四个动作的输入。相反,它会恢复原始文件说明符(您将存储在工作流早期的某个位置,然后将文件说明符发送到 Move Finder Items 操作,该操作使用存储在 outputFolder 变量中的值作为其目标。

      【讨论】:

      • 谢谢特德!通过您的回答,我学会了如何连续使用 2 个变量以及输出路径的优雅方式。如我的回答所示,我已成功提出解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多