AppleScript: Workspace-based App Launcher

Post to Twitter

Welcome back to AppleScripting! To see what we’ve done so far, take a look at the AppleScript category. We’re jumping ahead a bit, but you should be able to catch on fine. As always, if you have questions you can ask in the comments and on Ask Different.

Today we’ll be making a workspace-based app launcher. Once we’re done, you’ll be able to choose a workspace (e.g., “Programming”) from a list and automatically have AppleScript launch the apps you use in that workspace.
The scirpt will use many different parts of AppleScript; including loops, lists of records, and tell blocks.

To begin, create a new script in AppleScript editor. See the Introduction to AppleScript post if you need a reminder of how to do that.
Let’s get right into the code.

-- Set Up Workspaces
set workspaces to {}
set workspaces to workspaces & {{name:"Writing", apps:{"TextEdit", "Mou"}}}
set workspaces to workspaces & {{name:"Music", apps:{"iTunes", "I Love Stars"}}}

-- Generate List of Workspace Names set spaceNames to {} repeat with aSpace in workspaces set spaceNames to spaceNames & name of aSpace end repeat
-- Ask Which Workspace set whichSpace to choose from list spaceNames with prompt "Which workspace would you like to load?"
-- Load That Workspace repeat with aSpace in workspaces if name of aSpace = (whichSpace as text) then repeat with anApp in apps of aSpace tell application anApp to activate end repeat end if end repeat

Let’s go over the code section by section. I’ve added comments (-- SectionName) to mark sections.

The first section is where we set up the workspaces. The first line creates an empty variable where we’ll store our workspaces, and the later lines define each workspace. I’ve made two examples, but you can make your own. The main variable, workspaces, is a list. Each individual workspace is an item in that list. At first, each workspace might appear to be its own list: it’s enclosed by curly braces and has multiple values separated by commas, right? Well, they are actually records, a special kind of list. Records are like lists, but their values have named categories. In our script, each workspace is a record that has a name and an apps value. The apps value is yet another list that contains the apps that are part of the workspace.

Making your own workspace is easy. Copy one of the existing workspaces and paste it into the script. Its position in the code determines its position when you select the workspace: the first workspace defined in the code will be at the top of the list when you’re selecting which to load. To change its name, edit the text in quotes after name:. To change its apps, edit the text variables in the apps: list. You can have as many apps as you want; just add them as more items in the list. You simply have to put a comma after the previous app and the name of the new app in quotes. Here’s another place where order matters: your apps will be told to launch in the order that they appear here.

Now that you’ve got your workspaces set up, let’s move on to the next section of code. This setction is where AppleScript asks the user which workspace they want to load. The fist step is to compile a list that we’ll present to the user of just the names of our workspaces. First we make an empty variable spaceNames to hold that info. We use a loop to actually retrieve the data. repeat with aSpace in workspacesis the beginning of that loop. That code tells AppleScript to loop through each item in workspaces. As it goes through, it’ll assign the current item to the variable aSpace. The indented line is performed on each iteration of the loop; to each of our workspaces. All it does is add the name of the current workspace to the list.

Once we’ve generated the list of names, we have to ask the user which workspace they want to load. This is done with the choose from list command:

Select the workspace you want and click OK. The apps in that workspace will open.

The next section of code actually loads the selected workspace. We’ve got another of those repeat with loops. This one does something very similar to the first one: it loops through workspaces and checks if the workspace’s name matches the name the user selected. If it does, the content of the if statement is executed. In there we have yet another repeat with loop. This one loops through each app in the workspace’s apps: list and tells it to open.

There we go! You now have a workspace-based app launcher that lets you create workspaces and easily launch the apps for that workspace.

If you have any questions, feel free to comment on this post and I will respond. If you have any technical questions about AppleScript or AppleScript Editor, feel free to post on Ask Different!

Filed under AppleScript Automation

5 Comments

Subscribe to comments with RSS.

  • daviesgeek says:

    That’s pretty cool! I have one question, though. Why does it wait until the first application is completely loaded to open the second application?

    • Unfortunately, that’s the nature of tell application ... to activate.

      I’ve looked for a way to launch apps without waiting for completion, but haven’t seen a pure AppleScript way yet.

      If you’ve seen one, I’d love to hear about it 🙂

  • Conrad Goodwin says:

    Hii, I’m completely new to Mac and have been looking at making my own app in XCode that could launch shell scripts for terminal commands I couldn’t be bothered to look up every time (such as show/hide dashboard). Anyway turns out it’s MUCH harder than I thought, and now I’ve stumbled upon your script here I’ve tried it out and it does everything that I wanted (more or less) however I get an error “Connection Invalid” when it runs, even though it works fine.

    Here’s a paste of the code:

    — Set Up Workspaces set workspaces to {} set workspaces to workspaces & {{name:”Show Dashboard”, apps:{“Show Dashboard”}}} set workspaces to workspaces & {{name:”Show Desktop Items”, apps:{“Show Desktop Items”}}} set workspaces to workspaces & {{name:”Show Full Path”, apps:{“Show Full Path”}}} set workspaces to workspaces & {{name:”Show Hidden Files”, apps:{“Show Hidden Files”}}} set workspaces to workspaces & {{name:”Hide Hidden Files”, apps:{“Hide Hidden Files”}}} set workspaces to workspaces & {{name:”Hide Dashboard”, apps:{“Hide Dashboard”}}} set workspaces to workspaces & {{name:”Hide Desktop Items”, apps:{“Hide Desktop Items”}}} set workspaces to workspaces & {{name:”Hide Full Path”, apps:{“Hide Full Path”}}}

    — Generate List of Workspace Names set spaceNames to {} repeat with aSpace in workspaces set spaceNames to spaceNames & name of aSpace end repeat

    — Ask Which Workspace set whichSpace to choose from list spaceNames with prompt “Which script do you need?”

    — Load That Workspace repeat with aSpace in workspaces if name of aSpace = (whichSpace as text) then repeat with anApp in apps of aSpace tell application anApp to activate end repeat end if end repeat

    The apps are in the same directory. Thanks if you can provide any help.

    • Conrad Goodwin says:

      So it didn’t retain for formatting, lovely. I’m not sure how to post it properly, is [code][/code] enabled?

  • Friso says:

    After all these years: still a great introduction to Applescript. I learned: – the power of telling applications simple things – usefull scripting techniques (generating and interacting with lists) – how I can automate the boring task of setting up my workspaces on every clean restart Thanx!

  • Comments have been closed for this post