2013-06-27 61 views
0

我正在編寫一個腳本,允許學生將他們的文件上傳到計算機實驗室(同一網絡)中的教師計算機上的共享文件夾。我有一個工作腳本,它在執行時會將學生機器上UPLOAD文件夾中的所有文件複製到老師機器上的文件夾SUBMISSIONS中。但是,如果教師機器上已存在文件,則腳本會掛起。Applescript將文件複製到裝入的捲上;測試文件已存在

我需要能夠測試教師機器上單個文件的存在,並(a)彈出一條消息,指出「此文件已存在,重命名並再次上傳」或(b)將某些內容附加到文件名區分它...隨機數或「複製1」等。

理想情況下,我希望它作爲文件夾操作運行。當文件被添加到「UPLOAD」文件夾時,它會自動發送給老師。但我不想讓文件複製同名文件......或者腳本掛起。

任何想法或替代方法將受到歡迎。

這裏是我的代碼:

set home_path to path to home folder as string 
set work_folder to alias (home_path & "Desktop:" & "Upload") 

try 
    mount volume "afp://[LOGIN INFO].local/Submissions" 
    set this_folder to result as alias 
    tell application "Finder" 
     tell application "Finder" 
      duplicate every file of work_folder to this_folder 
     end tell 
     eject this_folder 
    end tell 
end try 

回答

0

我認爲,如果你嘗試塊有一個對錯誤塊通知你的任何錯誤,這將有助於。

try 
    # try stuff here 
    j # this will compile but will throw a runtime error and you can see the error 
on error error_message number error_number 
    display alert "Error: " & error_message & return & return & (error_number as text) 
end try 
0

此腳本會將每個文件複製到安裝的捲上。如果一個文件在目的地存在同名,它會在複製文件名的末尾添加一個數字並嘗試。

例:Test.doc的的文件夾中已經存在,則腳本將嘗試與該名test_1.doc等複製它..

原始文件永遠不會改名和舊的文件不會被覆蓋。 該腳本已完全註釋以解釋它在做什麼。

** UPDATE2 **

複製到目標代碼現在是在它自己的處理程序。 原始文件由取景器標籤索引6(綠色)標記以指示成功複製。

這也將停止使用索引顏色作爲檢查複製兩次相同的原始文件。如果它被標記爲索引標籤7,它將被忽略。

如果您想要將成功複製的文件移動到另一個使用該腳本的文件夾,則可以。但是我沒有在這個劇本中做到這一點。

  set home_path to path to home folder as string 
     set work_folder to alias (home_path & "Desktop:" & "Upload") 
     set counter to "" 
     global counter 
     --try 

     set this_folder to mount volume "afp://myMac/UserName/" 
     this_folder as text 


tell application "Finder" to set theFiles to every file of work_folder as alias list #GET ALL FILES OF LOCAL FOLDER AS ALIASES 
tell application "Finder" to set theRemoteFiles to (every file of ((this_folder & "Submissions:" as string) as alias)) #GET ALL FILES OF REMOTE FOLDER -- ONLY NEEDED FOR COUNT CHECK LATER 

repeat with i from 1 to number of items in theFiles #PROCESS EACH LOCAL FILE 

    set this_item to item i of theFiles #GET A LOCAL FILE 
    tell application "Finder" to set LabelIndex to label index of this_item 
    if LabelIndex is not 6 then 
     tell application "Finder" to set this_name to displayed name of this_item #GET ITS DISPLAYED NAME 
     tell application "Finder" to set this_extension to name extension of this_item #GET ITS EXTENSION NAME i.E "txt" 
     set realName to (do shell script "echo " & quoted form of this_name & "| awk -F '" & quoted form of this_extension & "' '{print $1}'") #GET ITS NAME WITHOUT EXTENSION NAME 

     set counter to 1 # SET A NUMBER TO ADD TO THE FILE NAME IF THE FILE NAME EXIST ALREADY IN THE REMOTE FOLDER 
     my checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder) # CALL TO HANDLER THAT WILL DO THE CHECKING AND COPYING 
    end if 

end repeat 
tell application "Finder" to eject this_folder 

# THE CALL TO THE HANDLER INCLUDES VARIABLES THAT ARE NOT GLOBAL OR PROPERTIES BUT NEED TO BE PASSED ON TO IT I.E(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder) 
on checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder) 


    # (1) IF THE NUMBER OF theRemoteFiles IS GREATER THAN 0 THEN FILES EXIST IN THE REMOTE FOLDER AND MAY CONTAIN FILES WITH THE SAME NAMES AS THE LOCAL ONES. PROCESS.. 
    # (2) IF THE NUMBER OF theRemoteFiles IS NOT GREATER THAN 0.THEN FILES DO NOT EXIST IN THE REMOTE FOLDER AND THE LOCAL ONES CAN JUST BE COPIED OVER. 
    if (count of theRemoteFiles) is greater than 0 then # (1) 

     try 
      my copyOver(this_item, this_folder, this_name) 

     on error errMssg #WE USE not overwritten ERROR TO TRIGGER THE RENAME THE DESTINATION FILE NAME TO INCLUDE A NEW NUMBER. 
      --tell application "Finder" to set label index of this_item to 6 
      if errMssg contains "not overwritten" then 

       set this_name to (realName & counter & "." & this_extension) 
       set counter to counter + 1 #WE SETUP THE FILE NAME NUMBER FOR THE POSSIBLE NEXT RUN 

       # RUN THE HANDLER AGAIN WITH THE CHANED DETAILS 
       my checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder) 
      end if 

     end try 

    else # (2) 
     my copyOver(this_item, this_folder, this_name) 
    end if 
end checkName 

on copyOver(this_item, this_folder, this_name) 
    # THE -n OPTION IN THE SHELL COMMAND TELLS CP NOT TO OVERWRITE EXISTING FILES. AN ERROR OCCURE IF THE FILE EXISTS. 
    # THE -p OPTION IN THE SHELL COMMAND TELLS CP TO PRESERVE THE FOLLOWING ATTRIBUTES OF EACH SOURCE FILE IN THE COPY: 
    # modification time, access time, file flags, file mode, 
    #user ID, and group ID, as allowed by permissions. Access Control 
    #Lists (ACLs) and Extended Attributes (EAs), including resource 
    #forks, will also be preserved. 
    # THE -v OPTION IN THE SHELL COMMAND TELLS CP TO USE VERBOS MODE. WHICH GIVES US A BETTER CLUE OF THE ERROR 

    set theResult to (do shell script "cp -npv " & quoted form of (POSIX path of this_item) & space & quoted form of (POSIX path of (this_folder & "Submissions:" as string) & this_name)) 
    if theResult contains "->" then 
     tell application "Finder" to set label index of this_item to 6 
    else 
     tell application "Finder" to set label index of this_item to 7 
    end if 

end copyOver 
+0

輕微更新處理程序以將錯誤消息用於觸發器,而不僅僅是來自cp shell命令的任何錯誤 – markhunte

+0

有一個新的更新** Update2 **,它可以停止已複製的文件再次被複制。 – markhunte

+0

我認爲發佈全面工作的代碼示例可能很好,但我認爲解釋如何得到解決方案也很好。 – Kaydell

0

好的。我再次嘗試寫一些代碼。這是我的版本。

它做了一些不同於原始海報發佈的代碼。

  1. 它複製文件和文件夾與時間戳服務器上的一個新的文件夾,以應付是否某些文件在服務器上已經存在的問題。

  2. 我改變了複製每個「文件」的重複語句的措辭,以複製每個「項目」,以便文件夾也被複制。

  3. 我在try語句中放置了一個on-error塊來顯示任何錯誤。

  4. 我激活Finder,以便您可以看到進度窗口。

  5. 如果沒有錯誤,我彈出一個對話框。

我有一個問題,我不得不修復:

  1. 在服務器上,我不得不啓用寫權限的客戶端或我有一個-5000錯誤。

我認爲下面的代碼應該工作得很好。這幾乎是100%的AppleScript。它只使用一次對shell的調用,即獲取當前日期並將其格式化爲服務器上創建的新文件夾的時間戳。

# set the path to the "work_folder" where the files are to be uploaded 
set home_path to path to home folder as string 
set work_folder to alias (home_path & "Desktop:" & "Upload") 

# duplicate the files and folders in work_folder to the server 
try 
    # TODO set the name of your server here 
    set the_volume to mount volume "afp://32-bit.local/Submissions" 
    set destination_path to the_volume as text 
    set folder_name to getTimeStamp() 
    tell application "Finder" 
     activate 
     set new_folder to make new folder at alias destination_path with properties {name:folder_name} 
     duplicate every item of work_folder to new_folder 
     eject the_volume 
     display alert "Successfully uploaded the files and folders" 
    end tell 
on error error_message number error_number 
    if error_number is not equal to -128 then 
     display alert "Error: " & error_message & return & return & (error_number as text) 
    end if 
end try 

# This function returns the current date and time as a time-stamp of the form yyyy-mm-dd-hh-ss 
# This function uses a shell script because it's just a lot easier to do than in AppleScript 
on getTimeStamp() 
    set time_stamp to do shell script "date '+%Y-%m-%d-%H-%M-%S'" 
    return time_stamp 
end getTimeStamp 
0

這是調試的另一個想法。你可以把來電「顯示對話框」才能夠知道你的腳本失敗:

display dialog "Entering script" 
set home_path to path to home folder as string 
display dialog "home_path: " & home_path 
set work_folder to alias (home_path & "Desktop:" & "Upload") 
display dialog "work_folder: " & work_folder as string 

try 
    mount volume "afp://32-bit.local/Submissions" 
    set this_folder to result as alias 
    display dialog "this_folder: " & this_folder as string 
    tell application "Finder" 
     display dialog "Inside of the first tell application Finder" 
     tell application "Finder" 
      display dialog "About to call duplicate" 
      duplicate every file of work_folder to this_folder 
      display dialog "Just returned from calling duplicate" 
     end tell 
     display dialog "About to call eject" 
     eject this_folder 
     display dialog "Just returned from call to eject" 
    end tell 
on error error_message number error_number 
    display alert "Error:" & error_message & return & return & (error_number as text) 
end try 
display dialog "Exiting script" 

另一個調試技術是輸出記錄到一個文本文件中。

另一個調試技術是購買的AppleScript調試器:

http://www.latenightsw.com/sd4/index.html

我相信,這個調試費用$ 200.00這對我來說太昂貴,但我用其他調試器和調試器是美妙的工具,讓您在腳本運行時「查看腳本內部」以查看變量的值並跟蹤哪些代碼行正在執行。

相關問題