2006-04-15

실습 - windows를 위한 파일 이름 확인(변경)

windows에서는 파일 이름으로 사용할 수 없는 문자가 있다고 합니다. "\", "/", "*" 같은 문자가 파일 이름에 있으면 복사가 안되는 것 같습니다. 이것들 외에 안되는 문자가 더 있는 것 같은데, 잘 모르겠네요. 더 발견되면 목록에 추가하면 될 것입니다.
droplet으로 만들어서 windows로 옮길 파일들을 drag & drop으로 떨구면 임시 폴더에 옮긴 후 해당 문자가 있는지 검사한 후에, 있으면 "_"로 바꿔주는 스크립트입니다. 혹시 동일한 이름의 파일이 임시 폴더에 이미 존재한다면 파일 이름 앞에 날짜를 붙여서 구분하게 했습니다.
on open theFiles
set theForbiddenList to {"\\", "/", "*"}
-- 기타 금지 문자들을 목록에 추가할 수 있습니다.
set cd to current date
-- 현재 시간을 지정합니다. 파일 이름이 겹칠 경우 사용하기 위해서입니다.

set theDestination to ((path to desktop folder as string) & "iPotato:")
tell application "Finder"
if theDestination exists then
else
make new folder with properties {name:"iPotato", path:path to desktop folder}
end if
end tell
-- 임시 저장 폴더를 지정하고, 없다면 만듭니다. '실습 - 이미지 크기 변경'을 참고하세요.

try
repeat with i from 1 to count of items of theFiles
set theFile to item i of theFiles
set theFileName to the name of (info for theFile)
-- 파일 이름을 가져옵니다.
set theFileNameList to every character of theFileName
-- 파일 이름의 각 문자를 list로 만듭니다. {"f", "i", "l", "e", ".", "t", "x", "t"}와 같은 형태로 만들어집니다.
set newFileName to ""
repeat with n in theFileNameList
-- n에 각 문자가 순서대로 대입됩니다.
if n is in theForbiddenList then
set newFileName to newFileName & "_"
-- 금지 문자가 있으면 "_"로 바꿉니다.
else
set newFileName to newFileName & n
end if
end repeat
set newFileName to newFileName as string
-- list 형태의 파일 이름을 문자열로 합칩니다.
tell application "Finder"
try
set newFile to duplicate theFile
-- 해당 파일을 복제한 후
set newFile to move newFile to theDestination
-- 임시 저장 폴더로 옮깁니다.
set the name of newFile to newFileName
-- 이름을 바꿉니다. 이미 같은 이름의 파일이 있다면 오류가 발생합니다.
on error errMsg number errNum
if errNum is -48 then
-- 동일한 이름의 파일이 있을 경우 -48번 오류가 발생합니다.
set prefix to (year of cd) & (month of cd as integer) & (day of cd) & (hours of cd) & (minutes of cd) & (seconds of cd) & "_" & i & "_" as string
-- 20060415123456_3_file.txt 와 같은 형태로 바꿉니다.
set the name of newFile to (prefix & newFileName) as string
end if
end try
end tell

end repeat
on error errMsg
display dialog errMsg
end try
end open

0 Comments:

댓글 쓰기

<< Home