Skip to content

Instantly share code, notes, and snippets.

import Cocoa
protocol AXWindowArray {
static func getWindows(processIdentifier pid: pid_t) -> [AXUIElement]
static func getWindows(bundleIdentifier bid: String) -> [AXUIElement]
}
extension AXWindowArray {
static func getWindows(processIdentifier pid: pid_t) -> [AXUIElement] {
let elm = AXUIElementCreateApplication(pid).takeUnretainedValue()
/**
Run executable.
- parameter command: path to an executable file.
- parameter withAdminPrivileges: execute with administrator privileges(sudo use case). defaut value is false.
- returns: standard output in one string or nil when executable error.
*/
public func sh(command: String, withAdminPrivileges: Bool = false) -> String? {
guard let res = NSBundle.mainBundle().resourcePath else {
NSLog("Unexpected error while initializing resource path.")
return nil
@uchcode
uchcode / OSAScriptProtocolExample.swift
Last active December 10, 2015 15:54
Protocol-Oriented Programming in Swift
import Foundation
// ErrorType
public enum OSAScriptError: ErrorType {
case InitializingFromSource(source: String?)
case InitializingFromContents(error: NSDictionary)
case Executing(error: NSDictionary)
case Unknown
}
@uchcode
uchcode / jxa-arg.js
Created March 13, 2016 17:47
JavaScript for Automation (JXA) Arguments example.
#!/usr/bin/osascript -l JavaScript
ObjC.import('Foundation')
const argv = $.NSProcessInfo.processInfo.arguments.js
argv.forEach(function(arg, idx) {
if( idx < 4 ) return
console.log( arg.js )
})
@uchcode
uchcode / protocol-extension-struct-example.swift
Created April 10, 2016 18:09 — forked from c9iim/protocol-extension-struct-example.swift
ユーザー定義に依存したプロトコル拡張例。依存部分の初期設定は拡張外の責務。
//: ユーザー定義に依存したプロトコル拡張例。依存部分の初期設定は拡張外の責務。
protocol FooSpec {
var fizz : Int { get }
func buzz() -> Int
}
extension FooSpec {
func buzz() -> Int {
return fizz * 2
@uchcode
uchcode / non_c-style_loop_example.js
Created April 11, 2016 05:28
次の再初期化式の指定がなく、刻みが不定の場合
// Non C-Style for statement
var i = 0; while (i < 10) {
console.log(`${i}`)
if ((i * i) % 2 == 0) {
i += 1
} else {
i += 2
}
}
@uchcode
uchcode / file1.txt
Created May 19, 2016 20:53
el capitan の osascript には " -l JavaScript" と言語指定するとプロトコルを認識しないバグがある? ref: http://qiita.com/tom-u/items/f0eab3066c2623ebc7ba
$ osascript -l JavaScript objc-sandbox.js
objc-sandbox.js:0:23: execution error: Error on line 1: Error: protocol does not exist (-2700)
@uchcode
uchcode / bar.scpt
Created June 22, 2016 10:54
runScript() のwithParametersバグとその回避・代替案 ref: http://qiita.com/tom-u/items/a9335672f21e91a4a69d
function run(argv) {
$.NSLog('%@', JSON.stringify(argv))
return argv
}
@uchcode
uchcode / virtualTyping.scpt
Created June 27, 2016 08:06
JXAで日本語入力のエミュレーション ref: http://qiita.com/tom-u/items/e06143e048dde7839c89
ObjC.import('Foundation')
ObjC.import('Carbon')
function virtualTyping(source) {
if (!source) {
throw new Error('Missing source')
}
if (typeof source !== 'string' || source.trim().length === 0) {
throw new Error('Invalid source')
}
@uchcode
uchcode / 0_reuse_code.js
Created June 27, 2016 14:20
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console