Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AppleScript将文件移动到正确位置_Applescript - Fatal编程技术网

AppleScript将文件移动到正确位置

AppleScript将文件移动到正确位置,applescript,Applescript,目标: 我正在尝试编写一个脚本,可以将其绑定到文件夹操作中,以便将文件从下载文件夹移动到正确的位置。文件夹将显示音乐、电影和电视节目或体育节目。将有单个文件以及包含多个项目的文件夹。我已经完成了脚本的框架,但是我正在努力做一些我知道如何用其他语言做的事情 参数: TV:S%%E%%用于单个文件,S%%用于文件夹 电影:只有一个视频文件(mkv、mp4、m4v)且名称中没有S%%E%%的文件夹,或没有S%%的文件夹 音频:文件类型(mp3、aac、flac、wav)对于单个文件,文件夹将很难看到内

目标:

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

参数:

TV:S%%E%%用于单个文件,S%%用于文件夹

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

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

问题:

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

Applescript中似乎没有可用于比较的通配符

我的主要问题是不知道如何打开一个文件夹项目,并在内容中循环查找某些扩展名或名称

GitHub:


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

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

你可以在网站上找到OSAX。下载
Satimage osax 3.7.0(build 411)
并将文件放入
/Library/ScriptingAdditions
(顶级
!)

这是使用脚本添加的优化脚本,基本正则表达式模式如下

  • “\\.S\\d{2}E”
    :查找一个点,后跟大写字母
    S
    ,后跟两位数字和一个大写字母
    E
  • “(nhl | nfl | ncaa)”
    :文本包含“nhl”或“nfl”或“ncaa”
如果文本与模式不匹配,则抛出错误


主要的变化是

  • run
    处理程序显示一个选择文件或文件夹的对话框
  • 添加
    open
    处理程序以将脚本用作水滴
  • 您的
    if/end if-if/end if
    表达式将替换为
    if/else if/end if
如果要将脚本用作文件夹操作,则仍会考虑文件和文件夹



谢谢这正是我需要的!你知道吗,在我的代码的文件夹部分,我如何能更深入地对文件进行同样的搜索?这样我就能更可靠地说出我正在查看的文件夹类型。有相当多的情况下,文件夹名称中没有任何关键字,但文件中会有。基本上,您可以递归调用处理程序。在文件夹分支中,以别名列表的形式获取文件夹中的所有项目,然后再次调用
对项目进行分类。谢谢,我会尝试一下。我尝试了另一种方法,但失败了。这似乎更简单、更高效。我让这部分工作正常,但当我测试连接到文件夹操作的脚本时,我有5个“.m4v”文件,当脚本绑定到文件夹操作时,这些文件不会移动。我倒了1000个文件,让它过滤和移动,但那5个仍然存在。脚本甚至没有触发,因为我在其中放置了一个显示对话框来测试它是否正在运行。我还通过脚本编辑器测试了脚本,它移动了这些文件,没有问题。
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