2013-12-10 67 views
0

我在文件夾「我的文件夾」中有一個文件「我的File.txt」 我想在名爲「我的文件1.txt」的同一文件夾「我的文件夾」中創建該文件的100個副本,「我的文件2 .txt「,」我的文件3.txt「等。如何在蘋果腳本中使用不同名稱在同一文件夾中複製文件?

我有接近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" 
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 
相關問題