使用OS: macOS 10.12
ファイルマネージャーの取得
fm = $.NSFileManager.defaultManager
ディレクトリの作成
p = '/tmp/hoge/fuga/foo/bar/'
i = 1 // or 0
a = $()
e = $()
fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(p, i, a, e)
// => true, e => $()
ファイルの作成
p = '/tmp/a.txt'
c = $.NSData.data //ゼロデータ
a = $()
fm.createFileAtPathContentsAttributes(p, c, a)
// => true
存在確認
fm.fileExistsAtPath('/bin/sh')
// => true
fm.fileExistsAtPath('/bin/')
// => true
fm.fileExistsAtPath('/private/hogehoge/')
// => false
isDir = Ref()
fm.fileExistsAtPathIsDirectory('/bin/', isDir)
// => true, isDir[0] => 1
isDir = Ref()
fm.fileExistsAtPathIsDirectory('/bin/sh', isDir)
// => true, isDir[0] => 0
種別確認
e = $()
a = fm.attributesOfItemAtPathError('/bin/', e)
a.objectForKey($.NSFileType).js === $.NSFileTypeDirectory.js
// => true, e => $()
a = fm.attributesOfItemAtPathError('/tmp/a-symbolic-link', e)
a.objectForKey($.NSFileType).js === $.NSFileTypeSymbolicLink.js
// => true, e => $()
ファイル同一内容確認
fm.contentsEqualAtPathAndPath('/tmp/a.txt','/tmp/b.txt')
// => true or false
移動または名称変更
at = '/tmp/a.txt'
to = '/tmp/d.txt'
e = $()
fm.moveItemAtPathToPathError(at, to, e)
// => true, e => $()
複製
at = '/tmp/d.txt'
to = '/tmp/a.txt'
e = $()
fm.copyItemAtPathToPathError(at, to, e)
// => true, e => $()
削除
p = '/tmp/d.txt'
e = $()
fm.removeItemAtPathError(p, e)
// => true, e => $()
リンク
at = '/tmp/a.txt'
to = '/tmp/a-hard-ln.txt'
e = $()
fm.linkItemAtPathToPathError(at, to, e)
// => true, e => $()
at = '/tmp/a-symbolic-ln.txt'
de = '/tmp/a.txt'
e = $()
fm.createSymbolicLinkAtPathWithDestinationPathError(at, de, e)
// => true, e => $()
p = '/tmp/a-symbolic-ln.txt'
e = $()
fm.destinationOfSymbolicLinkAtPathError(p, e)
// => $("/tmp/a.txt"), e => $()
パーミッションの設定と確認
a = $({'NSFilePosixPermissions':0777})
p = '/tmp/c.sh'
e = $()
fm.setAttributesOfItemAtPathError(a, p, e)
// => true, e => $()
fm.isReadableFileAtPath(p)
// => true
fm.isWritableFileAtPath(p)
// => true
fm.isDeletableFileAtPath(p)
// => true
fm.isExecutableFileAtPath(p)
// => true
読み込み・書き込み
p = '/tmp/a.txt'
u = $.NSUTF8StringEncoding
c = $('本日は晴天なり\n').dataUsingEncoding(u)
a = $()
fm.createFileAtPathContentsAttributes(p, c, a)
// => true, e => $()
d = fm.contentsAtPath(p)
// => [id NSConcreteData]
$.NSString.alloc.initWithDataEncoding(d, u)
// => $("本日は晴天なり\n")
d = fm.contentsAtPath('/tmp/hogehoge.txt')
// => $()
$.NSString.alloc.initWithDataEncoding(d, u)
// => $("")
作業ディレクトリの変更と現在位置
p = '/private/tmp/'
fm.changeCurrentDirectoryPath(p)
// => true
p = '/private/hoge/'
fm.changeCurrentDirectoryPath(p)
// => false
fm.currentDirectoryPath
// => $("/private/tmp/")
一覧
ls.js
p = fm.currentDirectoryPath
fm.directoryContentsAtPath(p)
c = fm.directoryContentsAtPath(p)
c = ObjC.deepUnwrap(c)
d = fm.currentDirectoryPath.js
c.filter((name)=>{var isDir=Ref();var result=fm.fileExistsAtPathIsDirectory(d+"/"+name,isDir);return result&&isDir[0]?true:false;})
c = fm.directoryContentsAtPath(p)
c = ObjC.deepUnwrap(c)
d = fm.currentDirectoryPath.js
c.filter((name)=>{var isDir=Ref();var result=fm.fileExistsAtPathIsDirectory(d+"/"+name,isDir);return result&&!isDir[0]?true:false;})
find.js
p = fm.currentDirectoryPath
c = fm.subpathsAtPath(p)
ObjC.deepUnwrap(c).forEach(content=>{console.log(content)})
tree.js
p = '/private/tmp/'
fm.changeCurrentDirectoryPath(p)
p = fm.currentDirectoryPath
c = fm.enumeratorAtPath(p)
content=c.nextObject.js; do {console.log(content);content=c.nextObject.js;} while(content)