Wednesday, March 24, 2010

인터넷에서 받은 이미지 파일의 확장자가 잘못되었거나 없는 것을 correct해주는 AppleScript

다음은, 인터넷에서 받은 Image File이 Extension이 없거나, 잘못되어있는 것을 correct 해주는 script이다.
on adding folder items to myFolder after receiving myFiles
repeat with myFile in myFiles
set myPath to (POSIX path of myFile)
set myType to do shell script "file --mime -br " & (quoted form of myPath)

if myType is "image/jpeg" and myPath does not end with ".jpg" and myPath does not end with ".jpeg" then
set newExtension to ".jpg"
else if myType is "image/gif" and myPath does not end with ".gif" then
set newExtension to ".gif"
else if myType is "image/png" and myPath does not end with ".png" then
set newExtension to ".png"
else
set newExtension to null
end if
tell application "Finder"
if newExtension is not null then
set name of myFile to (name of myFile) & newExtension
end if
end tell
end repeat
end adding folder items to
버그 :
여기에는 버그가 있는데
set myType to mySubstringBetweenFirstDelimiters(myTypeString, "image/", ";")
와 같은 함수를 사용해서 image/jpeg;
와 같은 부분만 추출해야 할 필요가 있다.

레퍼런스 :
http://henrik.nyh.se/2008/11/extension-action -> 설명
http://gist.github.com/27558 -> 소스코드

No comments:

Post a Comment