Skip to content

Instantly share code, notes, and snippets.

@uchcode
Last active July 3, 2017 12:19
Show Gist options
  • Select an option

  • Save uchcode/f531f9714d8966ef413258cf9a876243 to your computer and use it in GitHub Desktop.

Select an option

Save uchcode/f531f9714d8966ef413258cf9a876243 to your computer and use it in GitHub Desktop.
jxa-pathitem.js
class PathItemObject {
constructor(path) {
if (!path) throw new Error('PathItemObject: The argument "path" is required.')
if (Object.prototype.toString.call(path).slice(8,-1).toLowerCase()!=='string') throw new Error('PathItemObject: The argument "path" must be a string')
this.pathString = path
let $p = $(path).stringByStandardizingPath.js
if (/^\//.test($p)) {
this.path = $p
} else {
this.path = $($.NSFileManager.defaultManager.currentDirectoryPath.js + '/' + $p).stringByStandardizingPath.js
}
}
// Create
mkfile() {
if (this.isExists) throw new Error(`mkfile: Path is exists: "${this.pathString}"`)
let p = this.path
let c = $.NSData.data //zero data
let a = $()
let r = $.NSFileManager.defaultManager.createFileAtPathContentsAttributes(p, c, a)
if (!r) throw new Error(`mkfile: make file failed: "${this.pathString}"`)
return this
}
mkdir(createIntermediatesFlag=1) {
let p = this.path
let i = createIntermediatesFlag ? 1 : 0
let a = $()
let e = $()
let r = $.NSFileManager.defaultManager.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(p, i, a, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
return this
}
// Update
remove() {
if (!this.isExists) return this
let p = this.path
let e = $()
let r = $.NSFileManager.defaultManager.removeItemAtPathError(p, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
return this
}
move(to) {
if (!to) return this
if (Object.prototype.toString.call(to).slice(8,-1).toLowerCase() === 'function') return this.move(to(this))
let $to = new PathItemObject(to)
let a = this.path
if ($to.isDirectory) {
var t = $to.path + '/' + $(a).lastPathComponent.js
} else {
var t = $to.path
}
let e = $()
let r = $.NSFileManager.defaultManager.moveItemAtPathToPathError(a, t, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
this.path = t
if ($to.isDirectory) {
this.pathString = to.replace(/\/$/,'') + '/' + $(t).lastPathComponent.js
} else {
this.pathString = to
}
return this
}
copy(to) {
if (!to) return this
if (Object.prototype.toString.call(to).slice(8,-1).toLowerCase() === 'function') return this.copy(to(this))
let $to = new PathItemObject(to)
let a = this.path
if ($to.isDirectory) {
var t = $to.path + '/' + $(a).lastPathComponent.js
} else {
var t = $to.path
}
let e = $()
let r = $.NSFileManager.defaultManager.copyItemAtPathToPathError(a, t, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
if ($to.isDirectory) {
return new PathItemObject(to.replace(/\/$/,'') + '/' + $(t).lastPathComponent.js)
} else {
return new PathItemObject(to)
}
}
link(to) {
if (!to) return this
if (Object.prototype.toString.call(to).slice(8,-1).toLowerCase() === 'function') return this.link(to(this))
let $to = new PathItemObject(to)
let a = this.path
if ($to.isDirectory) {
var t = $to.path + '/' + $(a).lastPathComponent.js
} else {
var t = $to.path
}
let e = $()
let r = $.NSFileManager.defaultManager.linkItemAtPathToPathError(a, t, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
if ($to.isDirectory) {
return new PathItemObject(to.replace(/\/$/,'') + '/' + $(t).lastPathComponent.js)
} else {
return new PathItemObject(to)
}
}
symboliclink(to) {
if (!to) return this
if (Object.prototype.toString.call(to).slice(8,-1).toLowerCase() === 'function') return this.symboliclink(to(this))
let $to = new PathItemObject(to)
let d = this.path
if ($to.isDirectory) {
var a = $to.path + '/' + $(d).lastPathComponent.js
} else {
var a = $to.path
}
let e = $()
let r = $.NSFileManager.defaultManager.createSymbolicLinkAtPathWithDestinationPathError(a, d, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
if ($to.isDirectory) {
return new PathItemObject(to.replace(/\/$/,'') + '/' + $(t).lastPathComponent.js)
} else {
return new PathItemObject(to)
}
}
permission(value) {
if (Object.prototype.toString.call(to).slice(8,-1).toLowerCase() === 'function') return this.permission(to(this))
let a = $({'NSFilePosixPermissions':value})
let p = this.path
let e = $()
let r = $.NSFileManager.defaultManager.setAttributesOfItemAtPathError(a, p, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
return this
}
// Computed property
get isExists() {
if (!this.pathString) {
$.NSLog('isExists: Invalid value: %@', JSON.stringify(this))
return false
}
return $.NSFileManager.defaultManager.fileExistsAtPath(this.path)
}
get isReadable() {
if (!this.isExists) return false
return $.NSFileManager.defaultManager.isReadableFileAtPath(this.path)
}
get isWritable() {
if (!this.isExists) return false
return $.NSFileManager.defaultManager.isWritableFileAtPath(this.path)
}
get isDeletable() {
if (!this.isExists) return false
return $.NSFileManager.defaultManager.isDeletableFileAtPath(this.path)
}
get isExeutable() {
if (!this.isExists) return false
return $.NSFileManager.defaultManager.isExecutableFileAtPath(this.path)
}
get isDirectory() {
if (!this.isExists) return false
let p = this.path
let e = $()
let a = $.NSFileManager.defaultManager.attributesOfItemAtPathError(p, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
return a.objectForKey($.NSFileType).js === $.NSFileTypeDirectory.js
}
get isSymbolicLink() {
if (!this.isExists) return false
let p = this.path
let e = $()
let a = $.NSFileManager.defaultManager.attributesOfItemAtPathError(p, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
return a.objectForKey($.NSFileType).js === $.NSFileTypeSymbolicLink.js
}
get destinationOfSymbolicLink() {
if (!this.isSymbolicLink) return ''
let p = this.path
let e = $()
let r = $.NSFileManager.defaultManager.destinationOfSymbolicLinkAtPathError(p, e)
if (!e.isNil()) {
let a = e.localizedDescription.js
let b = e.localizedRecoverySuggestion.js ? e.localizedRecoverySuggestion.js : ''
throw new Error(a+b)
}
return r.isNil() ? '' : r.js
}
toString() {
return JSON.stringify(this)
}
}
class PathItemArray extends Array {
constructor(...path) {
super()
for (let p of path) this.push(new PathItemObject(p))
}
// Create
mkfile() {
for (let o of this) o.mkfile()
return this
}
mkdir() {
for (let o of this) o.mkdir()
return this
}
// Update
remove() {
for (let o of this) o.remove()
return this
}
move(to) {
for (let o of this) o.move(to)
return this
}
copy(to) {
let a = new PathItemArray()
for (let o of this) a.push(o.copy(to))
return a
}
link(to) {
let a = new PathItemArray()
for (let o of this) a.push(o.link(to))
return a
}
symboliclink(to) {
let a = new PathItemArray()
for (let o of this) a.push(o.symboliclink(to))
return a
}
permission(value) {
for (let o of this) o.permission(value)
return this
}
toString() {
return JSON.stringify(this)
}
}
// === オブジェクト
// 空ファイル作成
obj = new PathItemObject('a.txt')
obj.mkfile()
// ディレクトリ作成
obj = new PathItemObject('a')
obj.mkdir()
// 削除
obj.remove()
// パーミッション変更
obj.permission(0700)
// tempに名称変更
obj.move('./temp')
// temp内に移動
obj.move('./temp/')
// tempとして複製
obj.copy('./temp')
// temp内にコピー
obj.copy('./temp/')
// tempとしてハードリンク
obj.link('./temp')
// temp内にハードリンク
obj.link('./temp/')
// tempとしてシンボリックリンク
obj.symboliclink('./temp')
// temp内にシンボリックリンク
obj.symboliclink('./temp/')
// シンボリックリンクのオリジナルパス
obj.destinationOfSymbolicLink
// 展開済みのパス
obj.path
// コンストラクタ引数に指定されたパス文字列
obj.pathString
// 存在確認
obj.isExists
// パーミッション:読み込み可能
obj.isReadable
// パーミッション:書き込み可能
obj.isWritable
// パーミッション:削除可能
obj.isDeletable
// パーミッション:実行可能
obj.isExeutable
// ディレクトリか確認
obj.isDirectory
// シンボリックリンクか確認
obj.isSymbolicLink
// === 配列
// 空ファイル作成
ary = new PathItemArray('a.txt','b.txt','c.txt')
ary.mkfile()
// ディレクトリ作成
ary = new PathItemArray('a','b','c')
ary.mkdir()
// 削除
ary.remove()
// 移動
ary.move('./temp/')
// パーミッション変更
ary.permission(0700)
// 複製
ary.copy('./temp/')
// ハードリンク
ary.link('./temp/')
// シンボリックリンク
ary.symboliclink('./temp/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment