Automator でタイムスタンプを入力する
某所で書き込みした Automator でタイムスタンプを入力する方法について解説します。サービス、またはショートカットとして登録すると、システムワイドで使用できるショートカットキーを設定できます。
コード
AppleScript で書かれています。OS X ならバージョン関係なく大体動くはずです。
tell application "System Events" set ps to application processes where its frontmost is true tell item 1 of ps set save_clip to the clipboard set the clipboard to (do shell script "date '+%Y-%m-%d'") set edit_menu_bar to menu bar items of menu bar 1 where (its name is "編集") or (its name is "Edit") set edit_menu to menu 1 of item 1 of edit_menu_bar set paste_menu to menu items of edit_menu where (its name is "ペースト") or (its name is "貼り付け") or (its name is "Paste") click item 1 of paste_menu set the clipboard to save_clip end tell end tell
最初に、どのアプリケーションに入力するのかを決めます。例えば、Safari で入力中に F1 を押したら、 Safari に入力してほしいので、それを特定します。 AppleScript では普通は current application と書けばよいのですが、 サービスから呼び出した場合、Service Launcer みたいなやつが current application になってしまうので、うまくいきません。 そこで、現在動作中のアプリケーション(application processes)の中で、ウインドウが一番手前(frontmost)のアプリケーションを探してきます。 そのアプリケーションの「編集」メニューの中の「ペースト」をクリックしたことにします。 「ペースト」メニューは Google Chrome では「貼り付け」(Windows 流?)なので、それにも対応しておきます。 日本語未対応のアプリを使う場合は、「Paste」とかの場合があるので、それも追加しましょう。