4

現在のアプリをログに記録し、別のアプリに切り替え、何らかのタスクを実行し、元のアプリに戻るスクリプトを作成しようとしています。これは私が持っているものです

set currentApp to my getCurrentApp()
activate application "Safari"
# Some task
activate application currentApp

to getCurrentApp()
set front_app to (path to frontmost application as Unicode text)
set AppleScript's text item delimiters to ":"
set front_app to front_app's text items
set AppleScript's text item delimiters to {""} --> restore delimiters to default value
set item_num to (count of front_app) - 1
set app_name to item item_num of front_app
set AppleScript's text item delimiters to "."
set app_name to app_name's text items
set AppleScript's text item delimiters to {""} --> restore delimiters to default value
set MyApp to item 1 of app_name
return MyApp
end getCurrentApp

奇妙なことに、文字列リテラルを入力すると activate application コマンドが機能しますが、文字列変数を渡すとアプリケーションがアクティブになりません。理由はありますか?

4

1 に答える 1

7

あなたのスクリプトは私のために働きます。文字列変数を使用してアプリケーションをアクティブ化することは、OSX のどのバージョンでも常に機能していたため、別の問題が発生しています。問題は、表示しているコードにはありません。

コードは機能しますが、次のように getCurrentApp() サブルーチンを短縮できます...

set currentApp to my getCurrentApp()
activate application "Safari"
delay 1
activate application currentApp

to getCurrentApp()
    return (path to frontmost application as text)
end getCurrentApp

アクティブ化行から「アプリケーション」も削除すると、サブルーチンで「テキストとして」も必要ありません...

set currentApp to my getCurrentApp()
activate application "Safari"
delay 1
activate currentApp

to getCurrentApp()
    return (path to frontmost application)
end getCurrentApp

結局のところ、コードは次のようになります...

set currentApp to path to frontmost application
activate application "Safari"
delay 1
activate currentApp

編集:最前面のアプリケーションを取得しようとすると、実行中のapplescriptが最前面にあると思われるアプリではなく、最前面のアプリケーションになることがあります。これがいつ発生するかを検出するのは非常に困難ですが、これがあなたに発生している可能性があると思います. そこで、最前面のアプリを取得するために使用するサブルーチンを次に示します。これにより、applescript が最前面のアプリとして返されなくなります。試してみて、それが役立つかどうかを確認してください...

on getFrontAppPath()
    set frontAppPath to (path to frontmost application) as text
    set myPath to (path to me) as text

    if frontAppPath is myPath then
        try
            tell application "Finder" to set bundleID to id of file myPath
            tell application "System Events" to set visible of (first process whose bundle identifier is bundleID) to false

            -- we need to delay because it takes time for the process to hide
            -- I noticed this when running the code as an application from the applescript menu bar item
            set inTime to current date
            repeat
                set frontAppPath to (path to frontmost application) as text
                if frontAppPath is not myPath then exit repeat
                if (current date) - inTime is greater than 2 then exit repeat
            end repeat
        end try
    end if
    return frontAppPath
end getFrontAppPath
于 2012-10-27T06:57:32.590 に答える