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 / Building OS X shell script with JavaScript.md
Last active April 14, 2022 13:22
Building OS X shell script with JavaScript

JXAでシェルスクリプト

OS X 10.10 から JavaScript for Automation (JXA) が利用可能になった。ここでは、その Automation 機能をシェルスクリプトとして利用するステップを記述する。

シバン

シェルスクリプトとして利用するので、その前提はターミナルでテキストファイルとして記述する。その際にはファイルを実行するコマンドが何かを記載するシバンが必要。JXA の場合は osascript コマンドを利用する。

#!/usr/bin/env osascript -l JavaScript
@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 / jxa_status_bar_app.js
Created March 13, 2016 18:57
Statusbar App in JXA
#!/usr/bin/osascript -l JavaScript
ObjC.import('Cocoa')
app = Application.currentApplication()
app.includeStandardAdditions = true
ObjC.registerSubclass({
name: 'MenuAction',
methods: {
@uchcode
uchcode / calling_osascript_from_ruby.rb
Last active December 17, 2020 21:10
calling osascript from ruby
require "open3"
def osascript language, script
Open3.capture3 "osascript", *["-l", language, :stdin_data => script]
end
def applescript script
osascript "AppleScript", script
end
@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)