0

私は Automator を使用して、ユーザーに 2 つの異なるアプリケーションのどちらかを選択するプロンプトを表示するアプリケーションを作成しています (私は非常に基本的な理解しかないプログラミングの完全な初心者です)。これまでのところ、このサイトの別の投稿で見つけたこれがあります

on run
choose from list {"Old", "New"} with prompt "Choose which launcher you want to use" without multiple selections allowed and empty selection allowed
return the result as string
end run

ユーザーが 2 つのオプションのいずれかを選択して [OK] をクリックすると、対応するアプリケーションが開かれます。ただし、どのオプションが選択されているかをアプリケーションに読み取らせ、対応するアプリケーションを開く方法がわかりません。これはAutomator内からでも可能ですか?

4

1 に答える 1

1

次のコードは、あなたがやりたいことをすると思います。3 つのアプリを挙げましたが、そのうちの 2 つは "Calendar" (以前は "iCal" と呼ばれていました) と "Contacts" (以前は "Address Book" と呼ばれていました) です。 t は Mac に付属していますが、名前にスペースが含まれるアプリでこのスクリプトをテストしたかったのです。

on run
    choose from list {"Calendar", "Contacts", "Web Editor"} with prompt "Choose which launcher you want to use" without multiple selections allowed and empty selection allowed
    if result is equal to false then
        error -128
    else
        # convert the list return from choose list to a single string
        set app_name to result as string
        # run the selected app
        tell application app_name
            launch
            activate
        end tell
        return app_name
    end if
end run

# the following line of code cancels the rest of the workflow when the user clicks the "Cancel" button
error -128

あなたが見逃していたのは、「Run AppleScript」アクションでアプリの名前を返して、ワークフローの次のアクションに渡すことだったと思います。

return the result as string

必要なことは、次のように、選択したアプリの名前を変数にキャプチャすることです。

# convert the list returned from the "choose list" command to be a single string
set app_name to result as string

アプリの名前を変数に取得したら、次のように使用してアプリを開くことができます。

tell application app_name
    launch
    activate
end tell

どのような値を返すかわかりません。AppleScript でここに返す内容は、このワークフローの次の Automator アクションに渡されます。私にとって理にかなっている唯一のことは、選択されたアプリ名を渡すことです。

return app_name

それを返すか、アクションの出力として一般的に渡される別のものを独自の入力として返すことができます。

return input

新しい Run AppleScript アクションを作成するときのように、「入力」を定義する必要があります。

on run {input, parameters}
    return input
end run

上記のスクリプトは入力を出力に渡すだけで、実際には何もしませんが、これは単なる出発点です。

オンライン チュートリアルとなる Web サイトを作成しているので、あなたの助けが必要です。プログラミング初心者で、私がまとめているチュートリアルを受講してくれる人が必要です。私のウェブサイトでフィードバックをいただければ、無料でお手伝いします。興味があれば、私のウェブサイトにアクセスするか、メールを送ってください。

Mac オートメーションとプログラミングの開始

kaydell@yahoo.com

于 2013-07-03T21:09:08.213 に答える