2013-06-27 12 views
0

학생들이 컴퓨터 랩 (같은 네트워크)에서 교사의 컴퓨터에있는 공유 폴더에 파일을 업로드 할 수있게 해주는 스크립트를 작성하고 있습니다. 실행될 때 학생 컴퓨터의 폴더에있는 모든 파일을 교사 컴퓨터에있는 폴더로 복사하는 작업 스크립트가 있습니다. 그러나 교사 컴퓨터에 이미 파일이 있으면 스크립트가 중지됩니다.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 등으로 복사합니다 오래된 파일은 덮어 쓰지 않습니다. 스크립트는 수행중인 작업을 설명하기 위해 모든 주석을 달았습니다.

** 갱신 2 **

대상 코드 복사가 자신의 핸들러에 지금이다. 원본 파일은 복사가 성공했음을 나타 내기 위해 파인더 레이블 색인 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 쉘 명령의 오류가 아닌 트리거에 대한 오류 메시지를 사용하기 위해 처리기를 약간 업데이트 – markhunte

+0

이미 복사 된 파일이 다시 복사되지 않도록 새 업데이트 ** Update2 **가 있습니다. – markhunte

+0

전적으로 게시 된 코드 예제가 좋을 것이라고 생각하지만 솔루션을 얻는 방법을 설명하는 것이 좋습니다. – Kaydell

0

확인. 나는 약간의 코드를 작성하려고 다시 시도했다. 여기 내 버전입니다.

원래 포스터에서 게시 한 코드와 다른 점이 몇 가지 있습니다. 타임 스탬프 서버에 새 폴더에

  1. 그것은 복사 파일 및 폴더 파일 중 일부가 서버에 이미 존재하는지 여부의 문제에 대처합니다.

  2. 복제 문구의 문구가 모든 "파일"을 복제하여 모든 "항목"을 복제하여 폴더가 복제되도록 변경했습니다.

  3. 오류를 표시하기 위해 try-statement에 on-error 블록을 넣습니다.

  4. 진행 창을 볼 수 있도록 Finder를 활성화합니다.

  5. 오류가없는 경우 끝에 대화 상자가 나타납니다.

는 내가 해결했다 문제가 있었다 : 서버에서

  1. 을, 나는 클라이언트에 대한 쓰기 권한을 가능하게했다 또는 나는 -5000 오류가 발생했습니다.

다음 코드는 꽤 잘 작동한다고 생각합니다. 거의 100 % AppleScript입니다. 셸에 대한 호출은 한 번만 사용되며 현재 날짜를 가져 와서 서버에 생성 된 새 폴더의 타임 스탬프 형식으로 사용합니다.

# 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" 

또 다른 디버깅 기술은 텍스트 파일로 출력을 기록하는 것입니다.

또 다른 디버깅 기술은 애플 스크립트 디버거를 구입하는 것입니다 :

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

내가이 디버거가 나를 위해 너무 비싸다이 $ 200.00 비용 있다고 생각을하지만 난 다른 디버거와 디버거가 있도록 훌륭한 도구 사용했습니다 변수의 값을보고 어떤 코드 행이 실행되는지 추적하기 위해 스크립트가 실행되는 동안 스크립트의 "내부를 들여다 봅니다".