Created
July 12, 2017 13:09
-
-
Save uchcode/7181e12a87a3247a5420207a2659c612 to your computer and use it in GitHub Desktop.
JXA - FileManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FileManager = (()=>{ | |
function pwd() { | |
return $.NSFileManager.defaultManager.currentDirectoryPath.js | |
} | |
function cd(path='~') { | |
if (path==='') path = '.' | |
let p = $(path).stringByStandardizingPath | |
let r = $.NSFileManager.defaultManager | |
.changeCurrentDirectoryPath(p) | |
if (!r) { | |
throw `cd(): No such file or directory: "${path}"` | |
} | |
return r | |
} | |
function read(path) { | |
let p = $(path).stringByStandardizingPath | |
let d = $.NSFileManager.defaultManager.contentsAtPath(p) | |
if(d.isNil()) throw `read(): No such file: ${path}` | |
return d | |
} | |
function write(contents, path) { | |
let p = $(path).stringByStandardizingPath | |
let c = contents | |
let a = $() | |
let r = $.NSFileManager.defaultManager | |
.createFileAtPathContentsAttributes(p,c,a) | |
if (!r) { | |
throw `write(): Create file failed: ${path}, ${contents}` | |
} | |
return r | |
} | |
function mkfile(path) { | |
let p = $(path).stringByStandardizingPath | |
if ($.NSFileManager.defaultManager.fileExistsAtPath(p)) { | |
throw `mkfile(): Path is exists: "${path}"` | |
} | |
let c = $.NSData.data //zero data | |
let a = $() | |
let r = $.NSFileManager.defaultManager | |
.createFileAtPathContentsAttributes(p, c, a) | |
if (!r) { | |
throw `mkfile(): Create file failed: "${path}"` | |
} | |
return r | |
} | |
function mkdir(path, createIntermediatesFlag=1) { | |
let p = $(path).stringByStandardizingPath | |
let i = createIntermediatesFlag ? 1 : 0 | |
let a = $() | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(p, i, a, e) | |
if (!e.isNil()) { | |
let s1 = 'mkdir(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r | |
} | |
function rm(path) { | |
let p = $(path).stringByStandardizingPath | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.removeItemAtPathError(p, e) | |
if (!e.isNil()) { | |
let s1 = 'rm(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r | |
} | |
function mv(at, to) { | |
let a = $(at).stringByStandardizingPath | |
let t = $(to).stringByStandardizingPath | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.moveItemAtPathToPathError(a, t, e) | |
if (!e.isNil()) { | |
let s1 = 'mv(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r | |
} | |
function cp(at, to) { | |
let a = $(at).stringByStandardizingPath | |
let t = $(to).stringByStandardizingPath | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.copyItemAtPathToPathError(a, t, e) | |
if (!e.isNil()) { | |
let s1 = 'cp(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r | |
} | |
function link(at, to) { | |
let a = $(at).stringByStandardizingPath | |
let t = $(to).stringByStandardizingPath | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.linkItemAtPathToPathError(a, t, e) | |
if (!e.isNil()) { | |
let s1 = 'link(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r | |
} | |
function ln(at, to) { | |
let a = $(to).stringByStandardizingPath | |
let w = $(at).stringByStandardizingPath | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.createSymbolicLinkAtPathWithDestinationPathError(a, w, e) | |
if (!e.isNil()) { | |
let s1 = 'ln(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r | |
} | |
function readlink(path) { | |
let p = $(path).stringByStandardizingPath | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.destinationOfSymbolicLinkAtPathError(p, e) | |
if (!e.isNil()) { | |
let s1 = 'readlink(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r.isNil() ? '' : r.js | |
} | |
function chmod(value, path) { | |
let a = $({NSFilePosixPermissions:value}) | |
let p = $(path).stringByStandardizingPath | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.setAttributesOfItemAtPathError(a, p, e) | |
if (!e.isNil()) { | |
let s1 = 'chmod(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r | |
} | |
function chown(user, path) { | |
let u = user.split(':') | |
let d = {NSFileOwnerAccountName:u[0]} | |
if (u[1]) d.NSFileGroupOwnerAccountName = u[1] | |
let a = $(d) | |
let p = $(path).stringByStandardizingPath | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.setAttributesOfItemAtPathError(a, p, e) | |
if (!e.isNil()) { | |
let s1 = 'chown(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r | |
} | |
function chgrp(group, path) { | |
let a = $({NSFileGroupOwnerAccountName:group}) | |
let p = $(path).stringByStandardizingPath | |
let e = $() | |
let r = $.NSFileManager.defaultManager | |
.setAttributesOfItemAtPathError(a, p, e) | |
if (!e.isNil()) { | |
let s1 = 'chgrp(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return r | |
} | |
function isExists(path) { | |
return $.NSFileManager.defaultManager | |
.fileExistsAtPath($(path).stringByStandardizingPath) | |
} | |
function isDir(path) { | |
let p = $(path).stringByStandardizingPath | |
let e = $() | |
let a = $.NSFileManager.defaultManager | |
.attributesOfItemAtPathError(p, e) | |
if (!e.isNil()) { | |
let s1 = 'isDir(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return a.objectForKey($.NSFileType).js === $.NSFileTypeDirectory.js | |
} | |
function isFile(path) { | |
let p = $(path).stringByStandardizingPath | |
let e = $() | |
let a = $.NSFileManager.defaultManager | |
.attributesOfItemAtPathError(p, e) | |
if (!e.isNil()) { | |
let s1 = 'isFile(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return !(a.objectForKey($.NSFileType).js === $.NSFileTypeDirectory.js) | |
} | |
function isSymbolicLink(path) { | |
let p = $(path).stringByStandardizingPath | |
let e = $() | |
let a = $.NSFileManager.defaultManager | |
.attributesOfItemAtPathError(p, e) | |
if (!e.isNil()) { | |
let s1 = 'isSymbolicLink(): ' | |
let s2 = e.localizedDescription.js | |
let s3 = e.localizedRecoverySuggestion.js || '' | |
throw s1+s2+s3 | |
} | |
return a.objectForKey($.NSFileType).js === $.NSFileTypeSymbolicLink.js | |
} | |
function isReadable(path) { | |
return $.NSFileManager.defaultManager | |
.isReadableFileAtPath($(path).stringByStandardizingPath) | |
} | |
function isWritable(path) { | |
return $.NSFileManager.defaultManager | |
.isWritableFileAtPath($(path).stringByStandardizingPath) | |
} | |
function isDeletable(path) { | |
return $.NSFileManager.defaultManager | |
.isDeletableFileAtPath($(path).stringByStandardizingPath) | |
} | |
function isExecutable(path) { | |
return $.NSFileManager.defaultManager | |
.isExecutableFileAtPath($(path).stringByStandardizingPath) | |
} | |
return { | |
pwd: pwd, | |
cd: cd, | |
read: read, | |
write: write, | |
mkfile: mkfile, | |
mkdir: mkdir, | |
rm: rm, | |
mv: mv, | |
cp: cp, | |
link: link, | |
ln: ln, | |
readlink: readlink, | |
chmod: chmod, | |
chown: chown, | |
chgrp: chgrp, | |
isExists: isExists, | |
isDir: isDir, | |
isFile: isFile, | |
isSymbolicLink: isSymbolicLink, | |
isReadable: isReadable, | |
isWritable: isWritable, | |
isDeletable: isDeletable, | |
isExecutable: isExecutable, | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment