user2547859
user2547859

Reputation: 1

How do I choose between two applications to be opened? (Automator)

I am using Automator to write an application (I'm a complete newbie with programming with only some very basic understanding) that will give the user a prompt to choose between two different applications. So far I have this, which I found in a different post on this site

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

When the user chooses one of the two options and hits "OK", the idea is for the corresponding application to open. However, I can't figure out how to get the application to read which option is chosen and open the corresponding application. Is this even possible from within Automator?

Upvotes: 0

Views: 681

Answers (1)

Kaydell
Kaydell

Reputation: 474

I believe that the following code will do what you want to do. I've listed three apps, two of which you may have: "Calendar" (formerly known as "iCal") and "Contacts" (formerly known as "Address Book". And a third app called "Web Editior" which doesn't come with the Mac, but I wanted to test this script on an app with a space in its name.

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

I believe what you were missing though was that you were having your "Run AppleScript" action return the app's name which would pass it on to the next action in the workflow.

return the result as string

What you need to do is to capture the selected app's name in a variable as follows:

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

Once you have the app's name in a variable, then you can use it as follows to open the app:

tell application app_name
    launch
    activate
end tell

I don't know what kind of value to return. What you return here in the AppleScript will be passed on to the next Automator action in this workflow. The only thing that makes sense to me is to pass the app name that was selected.

return app_name

We could either return that, or another thing that is commonly passed as the output of an action is its own input:

return input

You'd have to have "input" defined such as it is when you create a new Run AppleScript action:

on run {input, parameters}
    return input
end run

The above script just passes its input to be the output and doesn't really do anything, but this is just a starting point.

I'm putting together a website to be an online tutorial and I could use your help. I need people who are new to programming that will take the tutorials that I'm putting together and I'll assist you for free asking only that you give me feedback on my website. If you're interested, you can visit my website and/or send me an email.

Beginning Mac Automation and Programming

[email protected]

Upvotes: 1

Related Questions