LoginSignup
8
3

More than 3 years have passed since last update.

[AppleScript] GmailをChromeで開くアプリを作った MacOS

Last updated at Posted at 2018-02-14

はじめに

GmailをMacOS純正のメールアプリで使うとタグ付けが不便、端末ごとに設定するのが面倒、...等々の理由から、完全にブラウザベースでメールを処理することを決意しました。
ただ、GoogleChromeを開いて、ブックマークからGmailを開いて...も面倒。
そこで、ドッグにGmailのショートカットがあれば便利!とのことで、初AppleScript挑戦。

通知はChromeの設定で、Chromeを開いている限り受け取ることが可能。
以下手順。

作成手順

エディタの起動

アプリケーション/その他/スクリプトエディタ
新規書類
image.png

ソースコード

以下を貼り付け (AppleScript初コーディングのため汚いのはご容赦ください。ついでにQiita投稿もこれが初。)

main.scpt
set addr to "https://mail.google.com/mail/" #表示アドレス
tell application "Google Chrome"
    activate
    set n to 1
    repeat with w in windows #すでにGmailを開いていないか確認
        set i to 1
        repeat with t in (tabs of w)
            if (URL of t as string) contains addr then
                set active tab index of window n to i
                set index of window n to 1
                if i > 1 then #タブを固定する
                    tell application "System Events"
                        keystroke "x" using {command down, shift down}
                    end tell
                end if
                error number -128   #プログラムの終了
            end if
            set i to i + 1
        end repeat
        set n to n + 1
    end repeat
    open location addr #Gmailを新規で開き、タブを固定する
    tell application "System Events"
        keystroke "x" using {command down, shift down}
    end tell
end tell

仕様

  • Gmailを重複して開かない。すでに開いている場合は最前面に表示。
  • Gmailタブを固定し、一番左に移動する。

保存

そして、メニューからファイル/保存する。
image.png
名前:Gmail
場所:アプリケーション
ファイルフォーマット:アプリケーション

これで完成。
あとはドッグにD&Dすれば、ドッグからのワンクリックでChrome経由のGmailが開く。
ただし、アイコンは初期設定のままなのはダサいため、変更する。

おまけ:アイコンの変更

Finder/アプリケーション/Gmailを右クリック →「パッケージの内容を表示」
image.png
そこからContents/Resources/applet.icnsを変更することでアイコンを書き換え可能。
ただ、見慣れない拡張子のためフリーでダウンロードが手っ取り早い。
https://www.easyicon.net/language.ja/iconsearch/mac/

image.png
Gmailアイコン
ちなみに自分はこれを使用。

ここからicns形式でダウンロードし、applet.icnsの名前に変更、デフォルトアイコンと同じ位置に配置することで完了。
ついでに、アプリの仕様変更は
Contents/Resources/Scripts/main.scptを書き換えると可能。

完成図

image.png

役立った資料

AppleScript入門(Introduction)
[Mac][Chrome]Google ChromeをAppleScriptで操作する


追記(2018/10/16)

macOS Mojave (10.14)以降、起動時にアクセシビリティの許可を求められます。
キーボードアクセスのため、許可をすることで使用可能になります。

追記(2020/02/07)

macOS Catalina (10.15)でも当時作ったアプリが問題なく動作することを確認しました。
(アプリの作成は保証していません)

8
3
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
8
3