13

私がやろうとしていること:

テキスト エディタ (TextEdit、Byword、FoldingText) を使用しているときに、この AppleScript にファイル パスを表示させたいと考えています。

最前面のウィンドウ アプリを要求すると、アプリ名がわかりやすく簡単に取得できるので、次のステップで POSIX パスを要求できると考えました。

問題:

スクリプトは既に 99% 完成していますが、何かが欠けています。変数を使用しようとするactiveAppと機能せず、次のエラーが発生します。

Error Number:System Events got an error: Can’t get application {"TextEdit"}.
-1728

スクリプトは次のとおりです。

tell application "System Events"
     set activeApp to name of application processes whose frontmost is true

     --This doesn't work either:
     --do shell script "php -r 'echo urldecode(\"" & activeApp & "\");'"

     tell application activeApp
         set myPath to POSIX path of (get file of front document)
     end tell
     display dialog myPath
end tell

activeApp私が"TextEdit"すべての作品と交換すれば。助けていただければ幸いです。

Applescriptを使用して、アプリケーション名からプロセス名を取得し、その逆も同様です。

4

4 に答える 4

10

ドキュメントのプロパティを取得するか、pathシステム イベントを使用して取得しますvalue of attribute "AXDocument"

try
    tell application (path to frontmost application as text)
        (path of document 1) as text
    end tell
on error
    try
        tell application "System Events" to tell (process 1 where frontmost is true)
            value of attribute "AXDocument" of window 1
        end tell
        do shell script "x=" & quoted form of result & "
        x=${x/#file:\\/\\/}
        x=${x/#localhost} # 10.8 and earlier
        printf ${x//%/\\\\x}"
    end try
end try

最初の方法は Preview、TextMate 2、Sublime Text、iChm では機能せず、2 番目の方法は Acorn では機能しませんでした。2 番目の方法では、補助デバイスを有効にするためのアクセスが必要です。

于 2013-07-12T13:01:51.670 に答える
4

あなたが求めているのは...

set activeApp to name of application processes whose frontmost is true

「プロセス」に注意してください。これは複数形で、応答で複数のプロセスを取得できるため、applescript は名前のリストを提供します。返されるアプリケーションは 1 つだけですが、リスト形式のままです。エラーに {"TextEdit"} が含まれていることも確認してください。名前の周りの括弧はリストであることを意味するため、エラーは問題を示しています。

名前のリストを次のコード行に渡すことはできません。そのため、いくつかの選択肢があります。1) すべてのプロセスではなく、1 つのプロセスのみを要求できます。これは、リストではなく文字列を返します。このコードを試してください...

set activeApp to name of first application process whose frontmost is true

2) 「リストの項目 1」を使用して、リストを操作できます。このコードを試してください...

set activeApps to name of application processes whose frontmost is true
set activeApp to item 1 of activeApps

最後に、アプリケーションに通知するようにシステム イベントに通知するべきではありません。これら 2 つの Tell ブロッ​​クのコードを分離します。これが私があなたのコードを書く方法です。

tell application "System Events"
    set activeApp to name of first application process whose frontmost is true
end tell

try
    tell application activeApp
        set myPath to POSIX path of (get file of front document)
    end tell

    tell me
        activate
        display dialog myPath
    end tell
on error theError number errorNumber
    tell me
        activate
        display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
    end tell
end try

「フロント ドキュメントのファイルを取得」コードが機能するとは約束できません。それはアプリケーションによって異なります。すべてのアプリケーションがその要求を理解できるわけではありません。そのため、try ブロックを使用しました。いずれにせよ、適切なアプリケーションに対処していることは確かです。幸運を。

于 2013-07-12T12:23:24.840 に答える
4

私はしばらくこのスニペットを使用してきましたが、すべての Cocoa アプリで動作するようです (X11 についてはわかりません)。

set front_app to (path to frontmost application as Unicode text)

tell application front_app
    -- Your code here
end tell
于 2014-08-12T07:56:25.243 に答える
0

これはどれも、アプリケーションとして保存され、Dock に配置されたコンパイル済みの AppleScript では機能しないようです。アプリケーションを実行するときはいつでも、最前面にウィンドウを表示しているアプリケーションではなく、IT が最前面に表示されます。Applescript が実行されると、そのアプリケーションは非アクティブになります。実行時にアクティブでない Applescript アプリケーションを作成するにはどうすればよいですか?

上記の問題の解決策を見つけたかもしれません。目的のアプリケーションを再アクティブ化するようにユーザーに伝え、時間を与えてください。

tell application "Finder"
   activate
   say "Click front window of your application"
   delay 5
   set myapp to get name of first application process whose frontmost is true 
-- etc.
-- your code
end tell
于 2015-01-05T18:03:24.990 に答える