1
1

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 3 years have passed since last update.

入力する句読点の「、」と「,」を切り替えるAppleScript

Posted at

日本語入力の読点、普段は「、」を使っている人でも公文書や引用文では「,」を使わなきゃいけなかったりして、入力方法をサッと切り替えたい時があるかもしれません。
OSXの標準日本語入力IMEではシステム環境設定から変更する必要があるため、めんどくさい。
自動化しましょう。

読点切り替え.scpt
tell application "System Preferences"
	activate
	tell pane "com.apple.preference.keyboard"
		tell anchor "InputSources"
			reveal
			delay 1
			tell application "System Events"
				tell window 1 of application process "System Preferences"
					(* OS標準日本語入力を使ってる時だけ。 *)
					tell splitter group 1 of group 2 of tab group 1
						repeat with theRow in rows of table 1 of scroll area 1
							if name of the UI element 1 of theRow is "日本語 – ローマ字入力" then
								set selected of theRow to true
								(* 句読点入力を切り替える。 *)
								tell pop up button 3 of scroll area 2
									click
									if selected of (menu item "。と 、" of menu 1) then
										set selected of (menu item "。と ," of menu 1) to true
									else
										set selected of (menu item "。と 、" of menu 1) to true
									end if
									keystroke return
									
								end tell
								exit repeat
							end if
						end repeat
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

細かい説明

システム環境設定の「入力ソース」を表示させるところまでは、System Preferences.appがAppleScriptでの制御に対応しているので、サクッと表示させることができます。

tell application "System Preferences" (* システム環境設定.appを *)
	activate (* アクティブ化し *)
	tell pane "com.apple.preference.keyboard" (* 「キーボード」環境設定の *)
		tell anchor "InputSources" (* 「入力ソース」を *)
			reveal (* 表示する *)
(* 以下略 *)

しかし、表示したその先の部分は直接いじることができない。(ユーザーが任意のIMEをインストールできるから、SystemPreferences.appの責任範囲ではないってことかもしれません。)
そこで、System Eventsを使ってUIを操作する方法で操作してみます。このやり方はOSのバージョン変更等によってUI構成が変わると動かなくなるので、変更に弱いのが難点ですが、仕方ない。
どこに何があるかは、every UI elementを表示させたりしながら頑張って絞り込んでいきましょう。ファイト!調べた結果、ざっくりこんな感じでした。
imputsource.png
そこでまず、scroll area 2 に日本語 – ローマ字入力の設定パネルを表示させましょう。

(* 前略 *)
tell application "System Events"
	tell window 1 of application process "System Preferences" (* システム環境設定.appのウィンドウの *)
		tell splitter group 1 of group 2 of tab group 1 (* 真ん中らへんに表示されてる部分の *)
			repeat with theRow in rows of table 1 of scroll area 1 (* scroll area 1 の中の表の各行のうち
				if name of the UI element 1 of theRow is "日本語 – ローマ字入力" then (* OSX標準日本語IMEを *)
					set selected of theRow to true (* 選択する *)
(* 以下略 *)

そしたら、設定パネルから「句読点の種類」の選択肢を、開いて、選ぶ。

tell pop up button 3 of scroll area 2 (* "句読点の種類"ポップアップボタンを *)
	click (* クリックして選択肢を表示させ *)
	if selected of (menu item "。と 、" of menu 1) then (* 今「。と、」だったら *)
		set selected of (menu item "。と ," of menu 1) to true (* 「。と,」に変更 *)
	else (* それ以外の場合は *)
		set selected of (menu item "。と 、" of menu 1) to true (* 「。と、」に変更 *)
	end if
	keystroke return (* 確定 *)

で、あとは、タイムラグでの操作ミス対策に適当にdelayを入れたり、ちょうど良いタイミングでループを抜けたりさせたら完成です。
これを「スクリプトメニュー」に登録しておけば、入力する読点を「、」⇄「,」で切り替えられるってなもんよ。
Enjoy!

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?