2006-04-13

파일 처리(file)

AppleScript로 파일 내용을 읽거나, 파일을 만들 수 있습니다. 파일의 전체 혹은 일부를 읽을 수 있고, 새로 작성하거나 가공한 내용으로 파일을 작성할 수 있습니다.
파일의 경로를 직접 입력하거나 choose file을 이용해서 파일의 경로를 지정할 수 있습니다. Standard Additions의 사전에 read와 write에서 사용할 수 있는 옵션들이 기재되어있습니다.

파일 읽기
try
set theFile to choose file
set theOpenFile to open for access theFile
read theOpenFile
on error errMsg number errNum
get errNum & " : " & errMsg
end try
try
set theFile to "Macintosh HD:Users:iPotato:Documents:sample.txt"
set theOpenFile to open for access theFile
read theOpenFile from 100 to 300
on error errMsg number errNum
display dialog (errNum as string) & " : " & (errMsg as string)
end try
try
set theFile to "Macintosh HD:Users:iPotato:Documents:sample.html"
set theOpenFile to open for access theFile
read theOpenFile using delimiter "<"
close access theFile
on error errMsg number errNum
display dialog (errNum as string) & " : " & (errMsg as string)
end try


파일 쓰기
try
set theFile to path to desktop
set theFileName to (theFile & "testfile.txt") as string
set theOpenFile to open for access theFileName with write permission
write "Hello, World!" to theOpenFile

read (open for access theFileName)
on error errMsg number errNum
get errNum & " : " & errMsg
end try

open for access명령에 with write permission이라는 매개변수가 반드시 포함되어야 합니다. 그렇지 않으면 파일을 작성할 수 있는 권한을 부여받지 못했기 때문에 오류가 발생합니다.

0 Comments:

댓글 쓰기

<< Home