AppleScript 将文件移动到正确的位置

AppleScript to move files to correct location

目标:

我正在尝试编写一个脚本,我可以将其绑定到文件夹操作中,以将文件从下载文件夹移动到正确的位置。该文件夹将显示音乐、电影和电视节目或体育节目。将有单个文件以及多个项目的文件夹。我已经完成了脚本的框架,但我正在为一些我知道如何用其他语言做的事情而苦苦挣扎。

参数:

电视:单个文件为 S%%E%%,文件夹为 S%%

电影:包含单个视频文件(mkv、mp4、m4v)且名称中没有 S%%E%% 的文件夹,或没有 S%% 的文件夹

音频:单个文件的文件类型(mp3、aac、flac、wav),文件夹很难看到内容

问题:

如何更改文件选择以包含用于测试的文件夹和文件?我认为当我从 Automator 中的文件夹操作提供脚本输入时,这会很好地工作,但还不完全确定。

在我的比较中,Applescript 中似乎没有可用的通配符。

我的主要问题是不知道如何打开文件夹项目并循环查找特定扩展名或名称的内容。

GitHub:https://github.com/wiebs2334/DownloadManager

如有任何建议或建议,我们将不胜感激!

Vanilla AppleScript 无法处理用于文本搜索的通配符,但有一个免费的脚本添加 Satimage.osax 添加了强大的功能,例如使用正则表达式查找文本

您可以在 Smile Companion OSAX 站点上找到 OSAX。下载Satimage osax 3.7.0 (build 411)并将文件放入/Library/ScriptingAdditions(顶层Library!)

这是您使用 Scripting Addition 优化的脚本,基本的正则表达式模式是

  • "\.S\d{2}E":寻找一个点后跟大写 S 后跟两位数字和一个大写 E
  • "(nhl|nfl|ncaa)" : 文本包含 "nhl" 或 "nfl" 或 "ncaa"

如果文本与模式不匹配,则会抛出错误。


主要变化是

  • run 处理程序显示 select 个文件或文件夹的对话框
  • 添加了 open 处理程序以将脚本用作 droplet
  • 您的 if/end if - if/end if 表达式被替换为 if/else if/end if

如果您要将脚本用作文件夹操作,无论如何都会考虑文件和文件夹


property audioKeywords : {"mp3", "aac", "flac", "wav"}
property videoExtensions : {"mkv", "m4v", "mov", "mp4"}

on run
    set {button returned:buttonReturned} to display dialog "Choose files or folders" buttons {"Cancel", "Choose Files", "Choose Folders"}
    if buttonReturned is "Choose Files" then
        classifyItems for (choose file with multiple selections allowed)
    else
        classifyItems for (choose folder with multiple selections allowed)
    end if

end run

on open theItems
    classifyItems for theItems
end open

to classifyItems for myFiles
    repeat with anItem in myFiles
        set anItem to anItem as text
        set isVideo to false
        set isTV to false
        set isMovie to false
        set isAudio to false
        set isSports to false
        tell application "System Events" to set {theClass, theName, theExt, theContainer} to {class, name, name extension, container} of disk item anItem --set variables
        set className to theClass as text
        (*  &&&&&&&&&&&&&&&&&&&&&&&&&&&&  Files  &&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
        if className is "file" then
            ###########  Video ############
            if theExt is in videoExtensions then --search through all video extensions
                set isVideo to true
                ###########  TV ############
                try
                    find text "\.S\d{2}E" in theName with regexp
                    set isTV to true
                on error
                    ###########  Sports ############
                    try
                        find text "(nhl|nfl|ncaa)" in theName with regexp
                        set isSports to true
                    on error
                        ###########  Movies ############
                        set isMovie to true
                    end try
                end try
                ###########  Audio ############
            else if theExt is in audioKeywords then
                set isAudio to true
            end if

            (*  &&&&&&&&&&&&&&&&&&&&&&&&&&&&  Folders  &&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
        else if className is "folder" then
            ###########  TV ############
            try
                find text "\.S\d{2}\." in theName with regexp
                set isTV to true
            on error
                try
                    ###########  Audio ############
                    find text "(mp3|aac|flac|wav)" in theName with regexp
                    set isAudio to true
                on error
                    ###########  Movie ############
                    set isMovie to true
                end try
            end try
        end if

        display dialog ("Type:" & theClass & " || Ext:" & theExt & " || Video:" & isVideo & "
        " & theName & "
        Movie:" & isMovie & " || TV:" & isTV & " || Sports:" & isSports & "
        Audio:" & isAudio)

    end repeat
end classifyItems