Macアプリの自動起動

展示とかでアプリの自動起動・終了を設定したいときは、launchd でやります。
ユーザー設定からぽちぽちもできるけど、launchdのほうが一気に設定とかできて便利。

1. 実行スクリプトを用意する

Macアプリを起動するならこんな感じ

startup.command

open /Application/Calendar.app

2. plist ファイルを用意する

スクリプトの実行時間とかを書いたXMLファイル。
生成するサイトとかもあるので適当に作る。

Launched

com.umaibow.startup.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.umaibow.startup</string>
    <key>ProgramArguments</key>
    <array>
      <string>sh</string>
      <string>-c</string>
      <string>/my/path/to/script/startup.command</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

3. plist を登録する

plist を ~/Library/LaunchAgents/ にコピーして、コマンドから登録する

launchctl load -w "~/Library/LaunchAgents/com.umaibow.startup.plist"

解除するときは unload

launchctl unload "~/Library/LaunchAgents/com.umaibow.startup.plist"

テンプレを作った

github.com