2017-03-04 9 views
0

다음 애플 스크립트 코드는 마지막으로 추가 된 파일,AppleScript - 폴더의 유일한 파일 이름 가져 오기?

tell application "Finder" 
    set latestFile to item 1 of (sort (get files of (path to downloads folder)) by creation date) as alias 
    set fileName to latestFile's name 
end tell 

소스 가져옵니다How to get the latest downloaded file name with applescript programatically?

을하지만 폴더가 하나의 파일이있는 경우이 오류가 발생합니다. 이 문제를 해결하는 방법?

답변

2

스크립트는 파일과 작동해야합니다. 그러나 이없고 파일이 있으면 오류가 발생합니다.

당신은 try 블록

tell application "Finder" 
    try 
     set latestFile to item 1 of (sort (get document files of (path to downloads folder)) by creation date) as alias 
     set fileName to latestFile's name 
    end try 
end tell 
+0

이 잘 작동과 오류를 무시할 수 있습니다. 고맙습니다. 이 "/ MainFolder1/SubFolder1"과 같은 경로를 어떻게 지정할 수 있습니까? –

+1

엄밀히 말하면'/ MainFolder1'은 시동 디스크의 최상위에 있습니다. 이것은 의도 된 것입니까? Finder는 디스크 이름 (시동 디스크)으로 시작하는 (콜론으로 구분 된) 문자열 경로를 필요로합니다. 'get files of folder "라고 쓸 수 있습니다. Macintosh HD : MainFolder1 : SubFolder1 :"... " – vadian

+0

다시 한번 감사드립니다. 너는 매우 도움이되었다. –