Skip to content

Instantly share code, notes, and snippets.

@uchcode
uchcode / file0.js
Last active July 21, 2017 11:51
JXAのコードをnode.jsへ移譲する例 ref: http://qiita.com/tom-u/items/a2b1ad4d208c4720aaba
function nodejs(fn,lp='/usr/local/bin/node') {
let code = `console.log(JSON.stringify((${fn.toString()}).apply(null,JSON.parse(process.env.NODE_ARGS))))`
let data = $(code).dataUsingEncoding($.NSUTF8StringEncoding)
return function(...args) {
let i = $.NSPipe.new
i.fileHandleForWriting.writeData(data)
i.fileHandleForWriting.closeFile
let o = $.NSPipe.new
let e = $.NSPipe.new
let t = $.NSTask.new
@uchcode
uchcode / FileHandle.scpt
Created July 16, 2017 00:38
巨大なUTF8テキストファイルを一行ずつ読み込む ref: http://qiita.com/tom-u/items/fdfb8e4b38e2aefdb442
on readline(theFile)
return read theFile before "\n" as «class utf8»
--return read theFile before return as «class utf8»
end readline
@uchcode
uchcode / General.js
Last active July 12, 2017 14:30
JXA General functions
const General = (()={
function StringFromData(data=$.NSData.data,encoding=$.NSUTF8StringEncoding) {
return $.NSString.alloc.initWithDataEncoding(data,encoding).js
}
function StringToData(value='',encoding=$.NSUTF8StringEncoding) {
return $(value).dataUsingEncoding(encoding)
}
@uchcode
uchcode / DoShell.js
Created July 12, 2017 14:27
JXA DoShellScript
const DoShell = (()=>{
const app = Application.currentApplication()
app.includeStandardAdditions = true
function doShell(script, opt={}) {
return app.doShellScript(script, {
administratorPrivileges: !!opt.withPrompt,
withPrompt: opt.withPrompt || '',
alteringLineEndings: opt.alteringLineEndings || false
@uchcode
uchcode / FileManager.js
Created July 12, 2017 13:09
JXA - FileManager
const FileManager = (()=>{
function pwd() {
return $.NSFileManager.defaultManager.currentDirectoryPath.js
}
function cd(path='~') {
if (path==='') path = '.'
let p = $(path).stringByStandardizingPath
let r = $.NSFileManager.defaultManager
@uchcode
uchcode / read_utf8_example.js
Created July 11, 2017 02:34
UTF-8 テキストファイルを読み込む ref: http://qiita.com/tom-u/items/a076a77b0cc023a29f65
function app_read(handle, scpt='') {
let a = Application.currentApplication()
a.includeStandardAdditions = true
return a.runScript(`on run argv
read (item 1 of argv) ${scpt}
end`, {in:'AppleScript', withParameters:handle})
}
app = Application.currentApplication()
app.includeStandardAdditions = true
@uchcode
uchcode / jxa-chocoshell.js
Last active July 4, 2017 23:13
ChocoShell.js
ChocoFileManager = {
pwd() {
return $.NSFileManager.defaultManager.currentDirectoryPath.js
},
cd(path='~') {
let p = $(path).stringByStandardizingPath.js
let r = $.NSFileManager.defaultManager.changeCurrentDirectoryPath(p)
if (!r) {
throw new Error(`cd(): No such file or directory: "${path}"`)
}
@uchcode
uchcode / file0.js
Last active July 4, 2017 09:12
JXA $.NSFileManager 使用例 ref: http://qiita.com/tom-u/items/d64969a6793a455d2523
Applet = Application.currentApplication()
Applet.includeStandardAdditions = true
function sh(script, opt={}) {
return Applet.doShellScript(script, {
administratorPrivileges: !!opt.withPrompt,
withPrompt: opt.withPrompt ? opt.withPrompt : '',
alteringLineEndings: opt.alteringLineEndings ? opt.alteringLineEndings : false
}).trim()
}
@uchcode
uchcode / pathitem.js
Last active July 3, 2017 12:19
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
fm = $.NSFileManager.defaultManager