最終更新日:2010/10/27

postKeyEventToApp - 指定したアプリケーションにキーイベントをポストするコマンドラインアプリケーション

postKeyEventToAppとは

指定したアプリケーションにキーイベントをポストするコマンドラインアプリケーションです。普通にキーイベントポストするのと何が違うかと言うと、アプリケーションをアクティブにしないでも指定したアプリケーションにキーイベントを送ることができます。

使い方

postKeyEventToApp -app "AppName" [-keyCode #keyCode] [-key 'char'] [-command up/down] [-option up/down] [-shift up/down] [-control up/down]

修飾キーはupがデフォルト値です。

例:夜フクロウを操作してツイートするAppleScript。夜フクロウがバックグラウンドのままでも動作します。

my tweetByYoruFukurou("postKeyEventToApp-指定したアプリケーションにキーイベントをポストするコマンドラインアプリケーション。これを使って夜フクロウを操作してツイートするAppleScriptもあります。 夜フクロウがバックグラウンドでも動きます。http://memogaki.soudesune.net/postKeyEventToApp.html")

on tweetByYoruFukurou(tweetMessage)
(*
夜フクロウを使ってtwitterに投稿するスクリプト。
だいたい調整したけど、まだ調整不足なところもあるかもしれません。
UI Elementを指定する部分は夜フクロウの表示設定によっては環境によって違ってくるかもしれません。
夜フクロウがアクティブじゃなくても動作します。
投稿時にURL短縮もします。
command + リターンキーで投稿する設定で動くようになっています。。

postKeyEventToApp/Applicationsに入れてください
*)
tell application "System Events"
set activeAppName to name of item 1 of (every process whose frontmost is true)
set tweetInputUIElement to UI element 1 of scroll area 1 of window 1 of process "YoruFukurou"
tell tweetInputUIElement to set currentValue to value
if currentValue is not "" then
-- 何か入力中のテキストがある場合の処理
if activeAppName is "YoruFukurou" then -- アクティブだったら使用中かもしれないので投稿しない
beep
return -1
end if
-- beep
-- display dialog "tweetStringByYoruFukurou" & return & "入力中のテキストがありますが一時的に退避して続けますか?" buttons {"Cancel", "OK"} default button 2
end if
--set value to ""
do shell script "/Applications/postKeyEventToApp -app YoruFukurou -keyCode 45 -command down" -- cmd + n
delay 0.2
tell tweetInputUIElement to set value to tweetMessage
delay 0.2
-- URL短縮
do shell script "/Applications/postKeyEventToApp -app YoruFukurou -keyCode 1 -command down" -- cmd + s
delay 0.5
-- URL短縮完了まで待つ
if tweetMessage contains "http://" then
repeat 20 times
tell tweetInputUIElement to set checkValue to value
if checkValue is tweetMessage then
delay 0.2
else
exit repeat
end if
end repeat
end if
delay 0.2
-- 文字数チェック
tell static text 1 of window 1 of process "YoruFukurou"
set Character_Counter to value
end tell
set Character_CounterNumber to Character_Counter as number
if Character_CounterNumber < 0 then
return -140
end if
-- 投稿
do shell script "/Applications/postKeyEventToApp -app YoruFukurou -keyCode 36 -command down" -- cmd + return
delay 0.2
if currentValue is not "" then
-- 投稿し終わったら空になるので、その時元に戻す
repeat with i from 1 to 50
tell tweetInputUIElement
if value is "" then
set value to currentValue
exit repeat
else
delay 0.2
end if
end tell
end repeat
end if
end tell
return 0
end tweetByYoruFukurou

ダウンロード