Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした