3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

巨大なUTF8テキストファイルを一行ずつ読み込む

Posted at

使用OS: macOS 10.12

JXA の app.read() では utf8テキストファイル を読み込むことができない。そこで read だけは AppleScript で実装することに。

example.js
app_readline = Library('FileHandle').readline

app = Application.currentApplication()
app.includeStandardAdditions = true

p = '/Users/uchcode/Desktop/巨大テキスト.txt'

try {
    let f = app.openForAccess(p)
    while(1) {
        let t = app_readline(f)
        // ここで取得した1行のテキストを処理する
        console.log(t)
    }
} catch(e) {
    // console.log( Automation.getDisplayString(e) )
    // -39 eofを超えた
    if (e.errorNumber != -39) throw e
} finally {
    try { app.closeAccess(p) } catch(e) {
        // console.log( Automation.getDisplayString(e) )
        // -38 pは開かれていない
        if (e.errorNumber != -38) throw e
    }
}
FileHandle.scpt
on readline(theFile)
	return read theFile before "\n" as «class utf8»
	--return read theFile before return as «class utf8»
end readline

FileHandle.scpt は ~/Library/Script Libraries/FileHandle.scpt へ配置。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?