on ~ end 는 event 이다. 사용자가 만든 event는 function을 이용한 모듈화와도 같다.
(on run ~ end 같은, built-in event 도 있다(출처))
Text File 읽어서 Dialog Box 에 보여주는 스크립트
set filename to chooseFile()유니코드와 UTF-8
display dialog readFile(filename)
on chooseFile()
set filechosen to choose fileend chooseFile
set filename_ to (filechosen as text)
set filename to POSIX path of filename_
return filename
on readFile(unixPath)
set foo to (open for access (POSIX file unixPath))end readFile
set txt to (read foo for (get eof foo)as«class utf8»)
close access foo
return txt
다음과 같이 as
«class utf8» 옵션 없이 하면 "Mac OS Roman" encoded 파일밖에 못읽는다.set txt to (read foo for (get eof foo))다음과 같이 하면 Utf-16 포맷을 읽을 수 있다.
set txt to (read foo for (get eof foo) as Unicode text)Text의 모든 paragraph를 list로 분리하여 얻어내기
즉 줄단위로 얻어내는 방법. 다음과 같이 하면 된다.
set filename to chooseFile()UNIX 이용하기
set fileContent to readFile(filename)
set theList to every paragraph of fileContent
set firstItem to (item 1 of theList) as text
display dialog firstItem
다음과 같이 UNIX를 이용하여 파일을 읽는 방법도 있다.
on readFile( unixPath )레퍼런스 :
return (do shell script "cat '" & unixPath & "'")end
Read and write text files with AppleScript
Getting the List of It
Basic AppleScript information and syntax - built-in basic events
No comments:
Post a Comment