【问题标题】:Applescript - move files to folder which name begins with first 7 characters of filenameApplescript - 将文件移动到名称以文件名的前 7 个字符开头的文件夹
【发布时间】:2013-08-26 05:12:53
【问题描述】:

我有 2 个文件夹

第一个文件夹包含我想移动到第二个文件夹的子文件夹的所有文件。我想根据文件和文件夹名称的前 7 个字符来做这个。

因此,如果 FOLDER 2 中子文件夹的前 7 个字符与 FOLDER 1 中文件的前 7 个字符匹配,则文件将移至 FOLDER 2 的子文件夹中

现在我有一个可以运行的 Applescript,但速度非常慢。

这是目前为止的脚本:

set mgFileList to ""
set mgDestFolder to ""

set mgFilesFolder to (choose folder with prompt "Where are the files stored which you   would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")


set folderList to contents of mgDestFolder


tell application "Finder" to set fileList to files of mgFilesFolder

repeat with aFile in fileList
set prefix to getPrefix(name of aFile)
tell application "Finder"

     try
        set destinationFolder to (1st folder of mgDestFolder whose name begins with prefix)
        move aFile to destinationFolder
    end try

end tell
end repeat



on getPrefix(aName)
set prefix to text 1 thru 7 of aName
return prefix
end getPrefix

我最近才开始接触 Applescripting。这可以在 Applescript 中更有效地完成吗?我一直在寻找一种适用于 shell 脚本的解决方案(我认为这可能会快很多),但无法让它们中的任何一个工作。

我正在开发 OSX Mountain Lion 10.8.4

-- 编辑

我发现我上传的脚本版本有误,目前我已设法将它们组合在一起。上面是正在运行的脚本,但它真的很慢。可能是因为它不使用任何 shell 脚本。

---------------更新-------

这是目前运行速度非常快的最终脚本:

set mgFilesFolder to choose folder with prompt "Where are the files stored which you would like to move to the destination?"
set mgDestFolder to choose folder with prompt "Where is the destination folder?"

tell application "System Events"
set fileList to files of mgFilesFolder whose name does not start with "."

repeat with aFile in fileList
    set prefix to my getPrefix(name of aFile)
    try
        set destinationFolder to (1st folder of mgDestFolder whose name  begins with prefix)
        move aFile to POSIX path of destinationFolder
    end try
end repeat
end tell


on getPrefix(aName)
try
    set prefix to text 1 thru 7 of aName
    return prefix
on error
    return aName
end try
end getPrefix

【问题讨论】:

    标签: macos file applescript finder


    【解决方案1】:

    使用 shell 脚本可能更容易:

    cd FOLDER1; for f in *; do d=../FOLDER2/"${f:0:7}"*; [[ -d $d ]] && mv "$f" "$d"; done
    

    【讨论】:

    • 哇,这是一个快速的回复。我还没有太多的shell脚本经验。我可以而且应该将您的 shell 脚本实现到我的 applescript 中吗?如果是这样,你能告诉我怎么做吗?
    • 也许更容易,因为它在一行代码中,但我不认为它对于编码新手来说是可读或可理解的。这就是 applescript 的乐趣……任何人都可以理解……主要是。
    • @sirbaxx 您可以使用 do shell 脚本命令,例如 do shell script "cd FOLDER1; for f in *; do d=../FOLDER2/\"${f:0:7}\"*; [[ -d $d ]] && mv \"$f\" \"$d\"; done"
    • @LauriRanta,我对 shell 脚本的经验还不够丰富,不知道如何在上面的脚本中实现它。如何编辑 shellscript,例如 FOLDER1 将成为变量 mgFilesFolder?无论如何感谢您的回复。我将尝试在 Applescript 中找到有关 shell 脚本的更多信息。
    【解决方案2】:

    Finder 通常是一个速度较慢的程序,因为它在您的计算机上执行很多操作。因此,您应该尽可能避免使用它。在您的情况下,这些任务可以使用系统事件来完成。试试这个,也许它会更快。请注意,您的代码中有几个错误已在此代码中得到修复。

    set mgFilesFolder to choose folder with prompt "Where are the files stored which you would like to move to the destination?"
    set mgDestFolder to choose folder with prompt "Where is the destination folder?"
    
    tell application "System Events"
        set fileList to files of mgFilesFolder whose name does not start with "."
    
        repeat with aFile in fileList
            set prefix to my getPrefix(name of aFile)
            try
                set destinationFolder to (1st folder of mgDestFolder whose name begins with prefix)
                move aFile to POSIX path of destinationFolder
            end try
        end repeat
    end tell
    
    
    on getPrefix(aName)
        try
            set prefix to text 1 thru 7 of aName
            return prefix
        on error
            return aName
        end try
    end getPrefix
    

    【讨论】:

    • 谢谢!!这似乎工作得很快。我有超过 1500 个文件和相应的文件夹。这个脚本在几分钟内完成了整个过程
    【解决方案3】:

    不确定您正在处理多少个文件,但我用 10 个文件夹和约 100 个文件对此进行了测试,运行时间不到 4 秒。使用字符串比使用文件要快得多,因此脚本将文件/文件夹作为字符串获取并在需要时构建别名。

    set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
    set mgDestFolder to (choose folder with prompt "Where is the destination folder?")
    
    --Always use System Events whenever possible. It's WAY faster than Finder.
    tell application "System Events"
        set folderList to name of folders of mgDestFolder
        set fileList to name of files of mgFilesFolder
    end tell
    
    
    repeat with i from 1 to (count folderList)
        set folderName to item i of folderList
        set filesToMove to {}
        repeat with j from 1 to (count fileList)
            set filename to item j of fileList
            if filename begins with folderName then
                set end of filesToMove to alias ((mgFilesFolder as string) & filename)
            end if
        end repeat
    
        --Can't use system events for moving files. You have to use Finder.
        tell application "Finder"
            move filesToMove to alias ((mgDestFolder as string) & folderName & ":")
        end tell
    end repeat
    

    【讨论】:

    • 您可以使用系统事件来移动文件...见我的帖子。我发现的技巧是使用目标文件夹的 posix 路径。
    • 我打算这样做,但是当我在我的机器(10.7)上运行它时,它并没有移动文件。我也试过你的,但结果相同。
    • 好的。我使用的是 10.8,所以我没有意识到它不适用于早期版本的 applescript。
    • 我认为这个脚本只会将文件移动到与文件同名的文件夹(不带扩展名)。例如,我有一个名为“8020396”的文件,我希望将此文件移动到文件夹“8020396 - bank roemer - grijs”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多