14
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AppleScript & Xcodeで カラオケしつつGet Wild And Buildする

Posted at

GetWild Advent Calendar 2016 23日目担当の@SatoshiN21です。

皆さん、楽しくGet Wildしてますか?
ひとりで解けない愛のパズルを抱いているんじゃないでしょうか。
僕は毎日チープなスリルに身をまかせています。

ビルド時間に おびえていたよ

普段僕はiOSのアプリを開発をメインにお仕事をしているのですが、
普段Xcodeで開発していて最も無駄な時間は長過ぎるビルド時間です。
iOS系勉強会やQiitaにも都度ビルド時間改善の話が持ち上がり、
swiftになってからは更に顕著になりました。
Embedded Frameworkを用いて差分ビルドを最適化したり、Swiftの型推論によって伸びたビルド時間を細かく調整したりと、
皆さんいろいろ苦心しているかと思います。

今回は、そんなエンジニアにとっての無駄な時間を誤魔化すGetWildするTipsを紹介します。

※先に言っておくと、今回の内容を実践したところで1msもビルド時間は早くなりませんのでご注意下さい

カラオケで歌えるのなら 何も 怖くはない

一番テンションが上がるのはいつでしょうか?
そう、Get Wildカラオケしている時ですよね。
今回は、Xcodeのビルド実行しつつ、iTunesでGet Wildを再生し、Notificationでカラオケするscriptを作りました。

完成形はこちらです

pic.twitter.com/FU2w7hmGF2

— SatoshiN21 (@SatoshiN21) 2016年12月14日

Scriptを実行すると、

  1. Xcodeの最前面に表示しているプロジェクトをビルド
  2. iTunesでGet Wild再生
  3. Notificationで歌詞を表示
  4. ビルド完了後、再生をストップ
    これらの処理を実行します。

チープなScriptに身をまかせていても

今回はiTunesなどを簡単に操作できるAppleScriptで書きました。
実行環境は以下の通りです

  • macOS Sierra 10.12.2
  • Xcode 8.2
  • iTunes 12.5

まずは、おもむろに以下のアプリを立ち上げます。

/Applications/Utilities/Script Editor.app

New Documentを選択後、エディタに以下のScriptを入力します。

-- Xcodeの最前面に開いているプロジェクトをビルド
tell application "Xcode"
	try
		set actionResult to build workspace document 1
	on error errorMes
		return
	end try
end tell

-- Get Wild
tell application "iTunes"
	stop track
	play track "Get Wild"
end tell

-- 経過時間を保持
set currentTime to 0

repeat
	--  一定間隔でビルドの終了を判定する
	tell application "Xcode"
		if completed of actionResult is true then
			exit repeat
		end if
	end tell
	
	delay 0.1
	
	-- iTunesから経過時間を取得
	tell application "iTunes"
		set trackTime to player position as integer
	end tell
	
	-- 経過時間が更新された時に歌詞情報を取得
	if currentTime is not trackTime then
		set lyric to getWildAndTough(trackTime)
		display notification lyric
	end if
	set currentTime to trackTime
	
end repeat

-- ビルドに失敗した場合アクシデントを再生
tell application "Xcode"
	set resultStatus to status of actionResult
	if resultStatus is failed then
		tell application "iTunes" to play track "アクシデント"
	else
		tell application "iTunes" to stop track
	end if
end tell

-- 経過時間から歌詞を取得
on getWildAndTough(trackTime as integer)
	
	if trackTime = 29 then
		return "アスファルト タイヤを切りつけながら"
	else if trackTime = 33 then
		return "暗闇 走り抜ける"
	else if trackTime = 36 then
		return "チープなスリルに身をまかせても"
	else if trackTime = 40 then
		return "明日におびえていたよ"
	else if trackTime = 44 then
		return "It's your pain or my pain or somebody's pain"
	else if trackTime = 48 then
		return "誰かのために生きられるなら"
	else if trackTime = 51 then
		return "It's your dream or my dream or somebody's dream"
	else if trackTime = 54 then
		return "何も こわくはない"
        (* 続きの歌詞を入力... *)
	end if
	return ""
end getWildAndTough

⌘ + Rで実行すれば、右上に歌詞が表示され
思う存分カラオケを楽しむことができます。
逆に差分ビルドが走ってビルド時間が短いと、最後まで歌いきれない為に少しさびしい気分になります。
ちなみに、**ビルドに失敗するとTM NETWORK「アクシデント」が再生されます。**ビルドに失敗したことがいち早くわかり、最高ですね!

AppleScriptはmacOS上の様々なアプリとの連携が可能です。

tell application “Xcode” to build workspace 1

で最前面に表示されているXcodeのプロジェクトをビルドすることが可能です。
このコマンドはビルドの完了を待たずに処理が終わるので、

set actionResult to build workspace document 1

repeat
	if completed of actionResult is true then
		exit repeat
	end if
	
	delay 1
end repeat

このような形でビルドの完了をリピート文で待つ必要があります。

iTunesでは、音楽の再生、停止、及び経過時間の取得を行っています。

tell application "iTunes"
	set trackTime to player position as integer
end tell

iTunesアプリからはplayer position プロパティで現在の経過時間をreal値で取得できます。
後は、経過時間に合わせた歌詞をdisplay notification {lyric}で出すだけです。

君だけがまとめられるものがどこかにあるさ

正直、AppleScript練習の為のネタスクリプトになってしまいました。
個人的には、mac上で動いているアプリと連携して様々な事ができるので、
何かしら業務効率化用スクリプトを作るのであれば、一つの選択肢として考えてもいいかと思います。

ただ、今回Xcodeとの連携では、AppleScriptと連携するにはリファレンスに情報が足りない部分もあるし、
呼び出し方が明記されていないものもあったので
もしAppleScript上でXcodeと連携してゴニョゴニョやる時は
素直にxcodebuilddo shell scriptで呼び出して使った方がいいかもしれません。

Get Wild And Script!!

14
5
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
14
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?