uchcode
7/1/2017 - 2:59 PM

JXA $.NSFileManager 関連 ref: http://qiita.com/tom-u/items/6c1d7ece71d9d4e84f1f

JXA $.NSFileManager 関連 ref: http://qiita.com/tom-u/items/6c1d7ece71d9d4e84f1f

fm = $.NSFileManager.defaultManager
p = '/tmp/hoge/fuga/foo/bar/'
i = 1 // or 0
a = $()
e = $()
fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(p, i, a, e)
// => true, 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/")
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 => $()
p = fm.currentDirectoryPath
c = fm.subpathsAtPath(p)
ObjC.deepUnwrap(c).forEach(content=>{console.log(content)})
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;})
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)