2017-01-26 17 views
0

사실을 다른 열 값을 추가합니다AppleScript로 - 파일을 엑셀 시트에 나열되어있는 경우 접두어로

  • 내가 .PDF 파일 잔뜩 들어있는 폴더가 있습니다. 이름은 다음과 같습니다 (2-581.pdf, 131-681.pdf, 1138-4.pdf, ...)
  • .pdf 파일은 partA-partB.pdf로 구분되어 "-"로 구분됩니다.
  • 폴더의 모든 결과를 나열하는 Excel 시트가 있습니다.
  • Excel 시트에는 폴더에있는 것보다 많은 pdfs가 나열되어 있습니다.
  • 엑셀 시트 제 2 행은 제목과 헤더
  • 엑셀 시트 첫번째 컬럼
  • 엑셀 시트 네번째 열은 원래 된 .pdf 파일명 partA, 5 열을 partB 인 프리픽스 것 시험 번호이다 아르

이상적인 결과 - 나는 automator/applescript를 실행하고 .pdfs가 들어있는 폴더와 정보가 포함 된 Excel 시트를 가리킨다. 그것은 엑셀에있는 폴더의 모든 .pdfs를 실행하고 이름을 변경합니다. 이름이 바뀐 파일은 테스트 #, 공백 및 원래 이름으로 구성됩니다. Excel 시트의 행은 어떤 방법으로 강조 표시되거나 플래그가 지정되어 발견 된 것을 보여줍니다.

나는이 포럼뿐만 아니라 몇 가지 다른 포럼을 검색했으며 작동하도록 노력하는 코드를 함께 모으기 위해 노력했습니다. 나는이 시나리오를 내 시나리오에서 작동시키지 않았기 때문에이 코드를 붙여 넣지 않을 것이다.

다음 코드를 다른 도움말 포럼에서 복사하여 시나리오에 맞게 조정 해 보았습니다. 처음 6 개 항목에 대해 일종의 작업을 한 다음 중지합니다 (7 번째 항목이 폴더에 없기 때문에 생각합니다). 또한 작동 시키려면 원래 파일 이름과 새 파일 이름을 수동으로 만들어야했습니다. 나는 그것이 엑셀 밖에서 일할 수 없기 때문에 이것을했다.

모든 도움말, 제안, 가능한 해결책에 대한 링크를 환영합니다. 나는 많은 다른 엑셀 시트를 통과하는 많은 테스트 결과를 가지고 있으며, 이것은 나에게 많은 시간을 절약 해 줄 것이다. 나는 오늘 자신을 알아 내려고 노력했다. 나는 이것을 지나치거나 머리 위로 생각하고있다. 고맙습니다!

set KeyFileName to choose file with prompt "Choose the Excel file" 
set WorkingDirectory to (choose folder with prompt "Please locate the folder with the processed .pdf's.") as string 
set KeyFilePath to WorkingDirectory & KeyFileName 
set FileNamesKey to my GetFileNamesKey(KeyFilePath) 
set theResult to ChangeFileNames(WorkingDirectory, FileNamesKey) 
return theResult 
end run 

to GetFileNamesKey(KeyFilePath) 
    tell application "Microsoft Excel" 

    set ResultsRange to used range of active sheet of active workbook 

    set FileNames to {} 
    repeat with i from 1 to count of rows in ResultsRange 
     set NewFileName to value of third column of row i of ResultsRange 
     set OldFileName to value of second column of row i of ResultsRange 
     set FileNames to FileNames & {{OldFileName, NewFileName}} 

    end repeat 

    return FileNames 

end tell 
end GetFileNamesKey 

to ChangeFileNames(WorkingDirectory, FileNamesKey) 

tell application "Finder" 
    repeat with aFile in FileNamesKey 
     set OldFileName to item 1 of aFile 
     set NewFileName to item 2 of aFile 



     set PathToFile to WorkingDirectory & OldFileName 
     try 
      set FileExtension to name extension of file (WorkingDirectory & (item 1 of aFile)) 
      set name of file PathToFile to NewFileName & "." & FileExtension 
     on error 
      exit repeat 
      return false 
     end try 
    end repeat 
end tell 

return true 
end ChangeFileNames 
return input 
end run 

답변

0

내가 잘 이해한다면, 당신은 엑셀이 col.A = #test, col.B = oldFileName (XX-yyy.pdf), col.C = NewName으로 (빈?) 대령과 파일이 D = PartA (xx), Col.E = PartB (yyy)

해당 파일의 모든 행을 반복하고 Col.B 파일이 선택한 폴더에 있는지 검색하고 변경하려는 경우 변경하려고합니다. 파일 이름 (= colA + ""+ 이전 이름)을 선택하고 Excel 행에 표시합니다.

확인을 클릭하면 아래 스크립트가 원하는 작업을 수행합니다. 선택한 폴더에서 파일이 발견 된 경우 Excel 행을 다음과 같은 두 가지 방법으로 표시했습니다. C 열에 새 파일 이름을 설정하고 (이전에 비어 있는지 확실하지 않은 경우?) 해당 색을 빨간색으로 설정합니다. 라인 "열기 KeyFileName은"건너 뛸 수 있습니다

set KeyFileName to choose file with prompt "Choose the Excel file" 
set WorkingDirectory to (choose folder with prompt "Please locate the folder with the processed .pdf's.") as string 

tell application "Microsoft Excel" 
open KeyFileName 
set ResultsRange to used range of active sheet of active workbook 
-- Data bloc starts row 2 with headers and then row 3 to x 
-- col.A= #test, col.B= oldFileName (xx-yyy.pdf), col.C= NewName (empty), Col.D= PartA (xx), Col.E= PartB (yyy) 
repeat with i from 2 to count of rows in ResultsRange 
    -- loop through each Excel row only starting row 2 of the range to skip hearders 
    set OldFileName to value of second column of row i of ResultsRange 
    set NewName to (value of the first column of row i of ResultsRange) & " " & OldFileName 
    set theFile to WorkingDirectory & OldFileName 
    tell application "Finder" to set FileExist to (file theFile exists) --check file exists 
    if FileExist then 
     tell application "Finder" to set name of file theFile to NewName --change file name in the folder 
     set (value of third column of row i of ResultsRange) to NewName -- set new name in Excel col.C 
     set (color index of interior object of third column of row i of ResultsRange) to 3 -- = Red ! 
    end if 
end repeat --- next Excel row 
end tell 

은 Excel 파일이 이미 열려 있습니다 : 나는 (! I 희망)이 명확하게 많은 의견을 덧붙였다. 또한 스크립트는 변경 후 Excel 파일을 저장하지 않습니다!

+0

나는 이것을 달렸고 다음과 같은 오류 메시지가 나타납니다 : Finder에 오류가 발생했습니다 : {1.0, "", "A-53.pdf"}를 유니 코드 텍스트로 만들 수 없습니다. 는 다음이 오류와 함께 제공 코드입니다 강조 : '경우 FileExist 다음 \t \t \t TELL 응용 프로그램 "찾기"**' – OriginalDuplicate

+0

이상한 내가 그것을 확인 시험 **을 NewName에 파일 theFile의 이름을 설정하는 내 측면. 나는 당신의 칼럼 A의 가치에서 올 수 있다고 생각합니다. 먼저 "if FileExist .."앞에 줄을 추가하십시오 : log "name ="& NewName. NewName의 값이 표시됩니다. 귀하의 오류를 기반으로, 그것은 문자열 대신 목록이 될 것 같습니다. 그렇다면 "set NewName ..."행을 변경해야 할 수도 있습니다. 먼저 확인하고 스크립트를 업데이트하겠습니다. – pbell

+0

위에서 언급 한 로그 라인을 추가하려고 시도했지만 동일한 오류가 발생했습니다. 로그를 검사하여 변수가 무엇을 가져 왔는지 확인하는 방법을 잘 모르겠습니다. OldName과 NewName을 설정 한 후 "텍스트로"를 추가 할 수있었습니다. 스크립트가 실행되고, 셀이 빨간색으로 변하고 파일 이름이 바뀐 것을 볼 수 있습니다! 이름이 바뀐 파일의 이름은 소수점이있는 접두사로 시작됩니다. 나는 포맷팅에서 Excel 시트의 소수점 이하 자릿수를 제거했지만 여전히 그 이름에 거기에 넣는 것처럼 보입니다. 예 : "75.0 769-315.pdf" – OriginalDuplicate