◀   |   |   ▶
◀   |   |   ▶

最前面のプロセスを得る



書式は

tell application "Finder" -- or "System Events"
get processes whose frontmost is true
end tell

結果は

{application process "Script Editor" of application "Finder"}

しかし、これって使い道がない?

解決しました! Hiro さん情報 THANX!!(10/30/2004 追記)


くきさんiBlogTagAssist v1.2 のスクリプトを拝見していて、iBlog に処理させる部分、

tell application "iBlog" to activate

でふとわいた疑問。
『スクリプトを実行する時点で最前面にあるアプリケーションは iBlog のはず。なぜ activate する必要があるのか?』(くきさんのスクリプトにケチをつけているのではありません。この処理がないと動作しないのです)

さきのエントリ『iBlogTagAssist v1.2を使ってみる』でも書いたように、私は mi を使ってエントリを書くことがあるので、iBlogTagAssist が iBlog 以外のアプリケーションでも動作するようにすればどうしたらいいのかを考えました。

試しに tell application "iBlog" to activate をコメントアウトしてみると動作しません。おや? と思いサンプルスクリプトを作ってみました。

tell application "System Events"
set theFrontApp to (name of processes whose frontmost is true)
theFrontApp
end tell
リスト1自動生成

スクリプトエディタの実行ボタンを押すと、結果は
{"Script Editor"}
が得られます。これでは次の処理に渡しづらいので、

tell application "System Events"
set theFrontApp to (name of processes whose frontmost is true) as text
end tell
リスト2自動生成

とします。すると
"Script Editor"
が得られます。で、もう一度サンプルを作ってみました。

tell application "System Events"
set theFrontApp to (name of processes whose frontmost is true) as text
display dialog theFrontApp & " is formt most"
end tell
リスト3自動生成

これをスクリプトメニューから実行すると、あれ?
System Events is front most
と表示されてしまいます。どうやら、スクリプトメニューからスクリプトを実行した場合、最前面のプロセスは System Events に固定されてしまうようです。これでは使い道がありません。

では、スクリプトが実行された段階で最前面のプロセス、つまり System Events を隠してしまってはどうでしょう?

tell application "System Events"
set visible of processes whose frontmost is true to false
set theFrontApp to (name of processes whose frontmost is true) as text
display dialog theFrontApp & " is front most"
end tell
リスト4自動生成

スクリプトエディタの実行ボタンから実行すると、スクリプトエディタが隠れた上で、その時点で最前面にあるアプリケーションが最前面と表示されるのですが、スクリプトメニューから実行すると、やはり『System Events is front most』と表示されます。

う〜ん、『不特定の最前面にあるアプリケーションに処理を実行させる』スクリプトは無理なのかなぁ。

(覚え書き (10/30/2004 追記))

最前面のアプリケーションへのパス

path to frontmost application
→alias "HardDisk1:Applications:AppleScript:Script Editor.app:"

最前面のアプリケーション名

tell application "System Events"
name of (path to frontmost application)
end tell
→"Script Editor.app"

最前面のアプリケーションの表示名

tell application "System Events"
displayed name of (path to frontmost application)
end tell
→"スクリプトエディタ"

これらをふまえて…
最前面のプロセスに System Event を送る

tell application "System Events"
set theFrontApp to name of (path to frontmost application)
end tell
tell application theFrontApp to activate
tell application "System Events"
key down {command}
keystroke "v"
key up {command}
end tell

これなら、スクリプトメニューからも実行できます。
つまり、iBlogTagAssist v1.2 の

tell application "iBlog" to activate

となっている箇所(各ファイル1カ所)を

tell application "System Events"
set theFrontApp to name of (path to frontmost application)
end tell
tell application theFrontApp to activate
リスト5自動生成

と置き換えれば、mi など iBlog 以外のアプリケーションでも動作します。


投稿: 2004年10月28日 (木) at 05:39   | | | |

◀   |   |   ▶
◀   |   |   ▶