ユーザ用ツール

サイト用ツール


applescript

AppleScript

シェルコマンドを実行

do shell scriptでターミナルを起動せずに実行できる

ChromeをAppモードで起動し、GMailを開く

do shell script "open -na 'Google Chrome' --args --app='https://mail.google.com/mail/u/0/#inbox'"

サンプル

iTermを立ち上げて、docker-composeを起動する

tell application "iTerm"
  activate
 
  set _targetWindow to current window
 
  if _targetWindow is null then
    set _targetWindow to create window with default profile
  end if
 
  tell current session of _targetWindow
    write text "cd ~/Documents/hoge"
    write text "docker-compose -d up"
  end tell
end tell

アラート

display alert "アラート"

確認ダイアログ

display dialog "続ける?"

デフォルトはOKとキャンセルボタン。キャンセルを選ぶとスクリプト実行が打ち切られる

display dialog "どれ?" buttons {"A", "B", "C"}
set ans to button returned of result

buttonsでボタンの中身を変更できる。button returned of resultで選択したボタンのラベルが取得できる

display dialog "どれ?" buttons {"A", "B", "C"} default button "A" cancel button "C"

default buttonでデフォルトボタンを指定。cancel buttonでキャンセルボタンを指定(選択するとスクリプト実行が打ち切られる)

キャンセル

途中で処理をキャンセル (エラーコード-128はユーザによるキャンセル扱い)

error number -128

TerminalからAppleScriptを呼び出す

osacriptコマンドで呼び出すことができる

/usr/bin/osascript -e 'display notification "メッセージ" with title "タイトル" subtitle "サブタイトル' sound name "Glass"

シェルスクリプトとして呼ぶことも可能

#!/usr/bin/osascript
 
on run argv
	set msg to "notification"
 
	if (count of argv) > 0 then
		set msg to (item 1 of argv)
	end if
 
	display notification msg with title "From Terminal" sound name "Glass"
end run

これをnotifyなどと名前をつけて保存し、実行権限を付与すると通知センターに任意のメッセージを表示するコマンドになる。

./notify 任意のメッセージ

/usr/local/bin などパスのとおった場所に設置すると便利に使える

applescript.txt · 最終更新: 2024/05/05 16:32 by nullpon