はじめに
iPhone(iPad)のショートカットアプリで、「Obsidianを開く」をトリガーにPull、「Obsidianを閉じる」をトリガーにCommitとPushを自動で実行する方法についてまとめました。
これにより、いちいちPullやCommitとPushをしなくても複数デバイスで常に最新のデータにアクセスできるようになります。
実現したいゴール
PCとiPhone(iPad)間のVaultの変更が自動で同期されている状態
前提条件
本記事は、Working Copyというアプリを使用してPCとiPhone(iPad)間のVaultを手動で同期できている状態を前提としています。詳しい設定方法は下記を参照してみてください。

用意するもの
- iPhone(iPad)
- Obsidianアプリ(App Store)
- iPhone(iPad)のショートカットアプリ
- Working Copyアプリ(App Store)(買い切りの有料プラン 2023年12月現在で¥4,000)
- Obsidian VaultがバックアップされたGitレポジトリ
※本記事で説明する手順はiPhoneでもiPadでもできます。適宜読み替えてね。
バージョン情報
- iPhone SE 第3世代 ver 17.1.1
- iPad Air 第3世代 ver 17.1.2
- Working Copy ver 5.8.8
- Obsidian ver 1.4.16
Obsidianを開いたら自動でPullを実行する
まずPullを実行するショートカットを作成してから、Obsidianを開いたらショートカットを実行するオートメーションを作成していきます。
ショートカットアプリを開き、+をタップし新規作成する

「アクションを追加」をタップする

検索フォームに「Pull」と入力し、候補から「Pull Repository」をタップする

「Repository」をタップしObsidianがバックアップされたレポジトリを選択する


目的のレポジトリが選択されていることを確認し、ページ上部で任意の名前を付けて「完了」をタップする(「Remote」は「Default」のままでOK)

ショートカットアプリで「オートメーション」を開き、+をタップして新規作成する

検索窓に「アプリ」と入力し、表示された「アプリ」をタップする

「アプリ」に「Obsidian」、実行タイミングに「開いている」、「すぐに実行」を設定し、次へ。

マイショートカットから、STEP1で作成したPullを実行するショートカットを選択する

「完了」で保存し、実際にObsidianを開いてPullが実行されていればOK(変更差分がない場合は実行されない)。エラーの場合はその旨の通知が来る。

Obsidianを閉じたら自動でCommitとPushを実行する
自動Pullと同様に、まずCommitとPushを実行するショートカットを作成し、次にObsidianを閉じたらショートカットを自動実行するオートメーションを作成する。
自動Pullと同様に、新規ショートカットを作成する


コミット時のメッセージに含めるデバイス名を保存する変数を設定する。
どのデバイスからコミットされたかわかるようになるため、複数デバイスを使用している場合は設定するのがおすすめ。

任意の変数名をつけて「入力」に「デバイスの詳細」>「名前」の順に設定する。



検索フォームで「Commit」を検索し、候補から「Commit Repository」を選択し、「Repository」にObsidianがバックアップされたレポジトリを設定する。

「Message」に任意のコミットメッセージを入力する。今回の例では「Vault autocommit on」とテキストを入れて、↑で作成した変数(Device)をつなげることで「Vault autocommit on iPhone SE」といった感じでメッセージが残るようにしている。

Fail when nothing to Commit、Signはオフ
Commitの下に「Push Repository」アクションを追加し、「Repository」にレポジトリを設定する(「Remote」は「Default」のままでOK)。
ページ上部で任意のショートカット名を付けて「完了」。

自動Pullと同様に新規オートメーションを作成する。
「アプリ」に「Obsidian」、実行タイミングに「閉じている」、「すぐに実行」を設定し、次へ。

マイショートカットから、STEP1で作成したCommitとPushを実行するショートカットを選択する

「完了」で保存し、実際にObsidianを閉じてCommitとPushが実行されていればOK(変更差分がない場合は実行されない)。エラーの場合はその旨の通知が来る。

まとめ
これでショートカットアプリを駆使することでObsidianを自動でバックアップ、ほかのデバイスでの更新を反映することができるようになりました。Working Copyに課金する必要はありますが、買い切りなのでObsidianを使うならこれが一番リーズナブルな自動同期の方法だと思います!
たまにエラーが起きることもありますが再実行すれば解決するので、今のところかなり快適にObsidianを使えています。
コメント