LoginSignup
5
3

More than 5 years have passed since last update.

Dock のアプリを必要なもの以外まとめて終了するスクリプト

Last updated at Posted at 2015-01-26

Swift でスクリプトを書いてみた。

killdock.swift
#!/usr/bin/env swift

import Cocoa

// Dock に表示されるアプリケーションの一覧を取得
func getRegularApplications() -> [NSRunningApplication] {
    let runningApps = NSWorkspace.sharedWorkspace().runningApplications as! [NSRunningApplication]
    let regularApps = runningApps.filter { (app: NSRunningApplication) in
        app.activationPolicy == NSApplicationActivationPolicy.Regular
    }
    return regularApps
}

// ホワイトリストに含まれないアプリケーションをまとめて終了
func killApplications(apps: [NSRunningApplication]) {
    let allowed = getAllowedBundleIdentifiers()
    for app in apps {
        if contains(allowed, app.bundleIdentifier!) {
            println("Skipped \(app.localizedName!) (\(app.bundleIdentifier!))")
            continue
        }
        println("Killed  \(app.localizedName!) (\(app.bundleIdentifier!))")
        app.terminate()
    }
}

// 終了させないアプリケーションのホワイトリスト
// Finder 以外の項目はお好みで
func getAllowedBundleIdentifiers() -> [String] {
    return ["com.apple.finder",
            "com.omnigroup.OmniFocus2.MacAppStore",
            "com.apple.Safari"]
}

killApplications(getRegularApplications())
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3