【问题标题】:Automator (2.5) Script to create folder based on file name and move file to folderAutomator (2.5) 脚本根据文件名创建文件夹并将文件移动到文件夹
【发布时间】:2020-02-18 00:43:40
【问题描述】:

感谢您花时间查看。我正在尝试使用 automator 2.5 创建一个工作流,它将读取我目录中的视频文件,创建一个具有文件名的文件夹,并将文件移动到新创建的文件夹中。我已经尝试了多个在线可用的脚本并遇到了错误。然后我创建了这个不会出错或做任何事情的混合脚本。任何帮助将不胜感激(我对编程并不陌生,但对这个版本的自动机没有经验)

【问题讨论】:

  • IMO 直接在 Terminal 中执行此操作要容易得多。只需更改目录 cd ... 到包含.mov 文件的文件夹并使用以下命令for f in *.mov; do mkdir -p "${f%.*}" && mv -vn "$f" ./"${f%.*}"; done 注意: 像往常一样,在执行任何会更改文件系统内容的操作之前,请始终确保您有适当的备份。
  • 当我的文件扩展名都相同时,您的示例很棒。有时我有多个同名但扩展名不同的文件。不过,我很欣赏您的回复,这让我考虑另一种方法。
  • 然后使用,例如,for f in *.*; do ...

标签: applescript automator


【解决方案1】:

在前面的 Finder 窗口中选择您的文件后,在 Script Editor.app 中运行以下代码应该可以完成您想要实现的目标

tell application "Finder"
    activate
    set selectedFiles to selection as alias list
    set containingFolder to container of (item 1 of selectedFiles) --as alias
    repeat with i from 1 to count of selectedFiles
        set foldersRef to (a reference to folders of containingFolder)
        set foldersRefItems to name of (contents of foldersRef)
        set thisItem to item i of selectedFiles
        set fileName to (text items 1 thru -5) of (name of thisItem as text) as string
        if fileName is not in foldersRefItems then
            move thisItem to (make new folder at containingFolder ¬
                with properties {name:fileName})
        else
            move thisItem to folder fileName of containingFolder
        end if
    end repeat
end tell

【讨论】:

  • 你是上帝派来的。效果很好!我希望我能在你救了我的所有时间里投票数亿次,谢谢!
  • 如果有两个同名文件并允许将两者都移动到文件夹中,有什么方法可以使脚本不出错?示例:File1.abc & File1.xyz
  • 我刚刚更新了帖子中的代码,因此如果有包含相同名称的文件,脚本不会出错。如果您遇到任何其他问题,请告诉我
  • 再次感谢。对于尝试在 Automator 中执行此操作的任何人,只需将原始帖子中的 Applescript 代码替换为此处的答案即可。这也可以作为一项服务使用,允许您直接从 finder 启动它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
相关资源
最近更新 更多