如何在苹果脚本中使用不同的名称复制同一文件夹中的文件?

时间:2013-12-10 08:30:30

标签: copy applescript duplicates

我在“我的文件夹”文件夹中有一个“My File.txt”文件 我想在名为“My File 1.txt”,“My File 2.txt”,“My File 3.txt”等相同文件夹“My Folder”中制作该文件的100份副本。

我在苹果脚本方面有近0经验,所以如果有人能给我发一个完整的片段,我会很感激。

非常感谢,

特雷弗

2 个答案:

答案 0 :(得分:0)

在此之前,它有点复杂,然后需要使用自动名称和扩展名提取,但它应该作为编写Finder脚本的指南。

关键是在脚本顶部设置文件的路径。

set master_file to POSIX file "/Users/trevor/Desktop/My Folder/My File.txt"

tell application "Finder"
    set master_dir to (container of file master_file)
    set root_nm to the name of file master_file
    set ext to the name extension of file master_file
    set root_nm to characters 1 thru ((the offset of ext in root_nm) - 2) of root_nm as text

    repeat with counter from 1 to 100

        with timeout of 10 seconds -- may need more time for big files
            set err to false
            try
                set duplicated_file to duplicate master_file to master_dir
            on error msg
                log ("ERROR DUPLICATING file " & counter & " : " & msg)
                set err to true
            end try
        end timeout

        if (not err) then -- try and rename it.
            try
                set the name of duplicated_file to (root_nm & " " & counter & "." & ext)
            on error msg
                log ("ERROR RENAMING file " & counter & " : " & msg)
            end try
        end if

    end repeat

end tell

say "Finished"

答案 1 :(得分:0)

tell application "Finder"
    set dir to (POSIX file "/Users/username/My Folder") as alias
    repeat 100 times
        duplicate file "My File.txt" of dir to dir
    end repeat
end tell

你也可以在终端中运行这样的命令:

for i in {1..100};do cp ~/My\ Folder/My\ File.txt ~/My\ Folder/My\ File\ $i.txt;done