AppleScript/ファイルの読み書き
書き込む
set fpath to "MacOSX:Users:hoge:Desktop:test.txt" try set fn to open for access file fpath with write permission set eof of fn to 0 -- ファイルの内容をクリア write "Hello" to fn end try close access fn
シェルを使っちゃう
do shell script "echo hello > /tmp/hoge"
読み込む
set fpath to "MacOSX:Users:hoge:Desktop:test.txt" set txt to "" try set fn to open for access file fpath set txt to read fn -- 全ての内容を得る end try close access fn display dialog txt
シェルを使っちゃう
set txt to do shell script "cat < /tmp/hoge.txt" as text
内容をリストとして得る
set fpath to "MacOSX:tmp:hanage.txt" try set fn to open for access file fpath with write permission set eof of fn to 0 write "hello" & return & "goodbye" to fn end try close access fn try set fn to open for access file fpath set txts to read fn as text using delimiter {return} -- using delimiter {list} : 区切り -- txts には {"hello", "goodbye"} が入る end try close access fn
例: /etc/passwd を得る
set fpath to "/etc/passwd" as POSIX file -- POSIXパスを指定してfileを得ることもできる set lf to ASCII character 10 set cr to ASCII character 13 try set fn to open for access file fpath set _users to read fn as text using delimiter {cr & lf, cr, lf} set users to {} repeat with x in _users if first character of x is not equal to "#" then set users to users & x end if end repeat on error err display dialog err end try close access fn users
Last modified:2008/02/02 14:36:21
0.072691