2006-04-26

실습 - JPEG 변환

PNG, GIF, TIFF 등의 이미지 파일을 drag & drop으로 JPEG로 바꿔주는 droplet을 만들어봤습니다. 스크립트를 조금만 바꿔주면 다른 포맷으로도 쉽게 바꿀 수 있습니다. 비슷한 기능을 하는 프로그램들이 많이 있겠지만, 애플스크립트로도 할 수 있다는 것을 보여주기 위해 만들어봤습니다. 데스크탑이나 독에 위치시키면 쓸만할 것 같습니다.
on open theseFiles
-- droplet으로 만들기 위해 on open으로 시작합니다.
set thisPath to ((path to desktop folder) as string) & "iPotato:" as string
-- 변환된 파일이 저장될 경로입니다.
tell application "Finder"
if thisPath exists then
else
make new folder with properties {name:"iPotato", location:(path to desktop folder)}
end if
end tell
-- 해당 경로에 폴더가 있는지 확인한 후, 없으면 만듭니다.

tell application "Image Events"
repeat with thisFile in theseFiles
-- drag & drop 된 파일이 여러 개일 경우 repeat으로 하나씩 처리합니다.
set thisImage to open thisFile
-- 해당 파일을 Image Events가 처리할 수 있는 image class로 만듭니다.
save thisImage as JPEG in thisPath
-- 지정된 경로에 JPEG로 저장합니다. JPEG 대신 다른 이미지 유형을 적용할 수 있습니다.
close thisImage
-- 작업을 마쳤기 때문에 image를 닫습니다. 굳이 닫지 않아도 스크립트가 종료되면 시스템 메모리에서 사라지게 되지만, (미래를 위해) 닫아주는 것이 좋다고 생각합니다.
end repeat
end tell
end open


주석으로 언급한 image를 닫는 문제는 공부를 더 해야 명확한 설명이 가능할 것 같습니다. file을 읽거나 쓸 때에도 마찬가지로 만들어진 file class를 닫아주는 것이 좋습니다. 스크립트의 길이가 길어질 때 같은 이름의 변수가 사용될 수도 있기 때문일 것 같기도 하고, 만들어진 file이나 image의 크기가 매우 크다면 스크립트가 끝나기 전까지 메모리를 불필요하게 차지하고 있을 수도 있기 때문인 것 같기도 합니다.

2006-04-25

실습 - 데스크탑 파일 정리

애플포럼에서 바탕화면 정리용 스크립트, Tidy-It라는 글을 읽고서, 비슷한 것을 만들어보고 싶어졌습니다. 따라서 만든 것이기 때문에 독창성을 주장할 여지는 없어보입니다. 이번 실습 역시 이런 것이 이런 식으로 가능하다는 것을 보이는 데에 의의가 있습니다.
각각의 확장자들의 목록을 먼저 만듭니다. 변수에 대해서 설명할 때 나왔지만, property로 선언한 변수는 스크립트 전체에서 어디에서 불러낼 수 있는, 가장 범위가 넓은 전역 변수입니다. Finder의 애플스크립트에 대해서는 아직 강좌를 구성하지 못했는데 자꾸 나오고 있습니다만, 이해하는 데에 큰 어려움은 없을 것 같습니다.
on open으로 시작하는 스크립트를 응용프로그램으로 저장하면 drag & drop으로 실행되는 droplet이 됩니다. 개별 파일들의 저장 경로를 확인해서 없으면 해당 폴더를 만듭니다. 경로는 취향대로 바꿀 수 있습니다.
property imageExtensions : {"jpg", "jpeg", "gif", "png", "tiff", "bmp", "pic", "pict"}
property multimediaExtensions : {"mp3", "m4a", "aac", "mov", "avi", "mpg", "mpeg"}
property documentExtensions : {"txt", "doc", "pdf"}
property compressExtensions : {"zip", "sit", "sitx", "rar", "tar", "gz"}
-- 필요한 확장자를 추가할 수 있습니다.

property myPath : alias (((path to desktop folder) as string) & "iPotato:")
property imagePath : (myPath as string) & "image:" as alias
property multimediaPath : (myPath as string) & "multimedia:" as alias
property documentPath : (myPath as string) & "document:" as alias
property compressPath : (myPath as string) & "compress:" as alias
-- 저장 경로는 임의대로 바꿀 수 있습니다.

on open theseFiles
-- droplet으로 만들기 위해 on open으로 시작했습니다.
try
tell application "Finder"
if myPath exists then
if imagePath exists then
else
make new folder at myPath with properties {name:"image"}
end if

if multimediaPath exists then
else
make new folder at myPath with properties {name:"multimedia"}
end if

if documentPath exists then
else
make new folder at myPath with properties {name:"document"}
end if

if compressPath exists then
else
make new folder at myPath with properties {name:"compress"}
end if
else
make new folder with properties {name:"iPotato", path:path to desktop folder}
end if

repeat with thisFile in theseFiles
set thisExtension to name extension of (info for thisFile)
-- 확장자만 가져옵니다.

-- 각각의 리스트에 일치하는 확장자가 있는지 검사한 후, 있다면 지정된 폴더로 옮깁니다.
if thisExtension is in the imageExtensions then
move thisFile to imagePath
else if thisExtension is in the multimediaExtensions then
move thisFile to multimediaPath
else if thisExtension is in the documentExtensions then
move thisFile to documentPath
else if thisExtension is in the compressExtensions then
move thisFile to compressPath
end if
end repeat
end tell
on error errMsg
display dialog errMsg
end try
end open