LoginSignup
29

More than 5 years have passed since last update.

Swiftでシェルスクリプトを書く

Last updated at Posted at 2014-11-12

swift コマンドが Yosemite から /usr/bin/swift に標準で入っていた。
(手元にXcodeが入っていない環境が無いのでXcode必須かも?)
(→ Yosemite をクリーンインストールしたところちゃんと入っていました!)

$ which swift
/usr/bin/swift

まずは --help で引数をチェック。

$ swift --help
OVERVIEW: Swift compiler

USAGE: swift [options] <inputs>

OPTIONS:
  -assert-config <value> Specify the assert_configuration replacement. Possible values are Debug, Release, Replacement.
  -D <value>             Specifies one or more build configuration options
  -framework <value>     Specifies a framework which should be linked against
  -F <value>             Add directory to framework search path
  -g                     Emit debug info
  -help                  Display available options
  -I <value>             Add directory to the import search path
  -j <n>                 Number of commands to execute in parallel
  -L <value>             Add directory to library link search path
  -l<value>              Specifies a library which should be linked against
  -module-cache-path <value>
                         Specifies the Clang module cache path
  -module-link-name <value>
                         Library to link against when using this module
  -module-name <value>   Name of the module to build
  -nostdimport           Don't search the standard library import path for modules
  -Onone                 Compile without any optimization
  -Ounchecked            Compile with optimizations and remove runtime safety checks
  -O                     Compile with optimizations
  -sdk <sdk>             Compile against <sdk>
  -target-cpu <value>    Generate code for a particular CPU variant
  -target <value>        Generate code for the given target
  -version               Print version information and exit
  -v                     Show commands to run and use verbose output
  -Xcc <arg>             Pass <arg> to the C/C++/Objective-C compiler
  -Xlinker <value>       Specifies an option which should be passed to the linker

引数なしでインタラクティブなREPL(Read Eval Print Loop)モードに入る。

$ swift
Welcome to Swift!  Type :help for assistance.
  1>

(終了時は Control+d か、:q を入力。)

ということはシェバングに書けばシェルスクリプトにもできるのかなーと思ったら案の定できた。
import Cocoa 書けば OS X コンポーネントも使える。

#!/usr/bin/swift

import Cocoa

let alert = NSAlert()
alert.messageText = "エラー!"
alert.addButtonWithTitle("無視")
alert.addButtonWithTitle("了解")
alert.runModal();

スクリーンショット 2014-11-12 21.33.10.png

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
29