Applescript - 将文件移动到名称以文件名的前7个字符开头的文件夹

时间:2013-08-23 08:12:12

标签: macos file applescript finder

我有2个文件夹

第一个文件夹包含我要移动到第二个文件夹的子文件夹的所有文件。我想基于文件和文件的前7个字符。文件夹名称。

因此,如果FOLDER 2中子文件夹的前7个字符与FOLDER 1中文件的前7个字符匹配。文件get被移动到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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

使用shell脚本可能更容易:

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

答案 2 :(得分:0)

不确定您使用了多少个文件,但我使用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