Search code examples
macosapplescriptautomator

How to launch an application after a specific Disc was mounted using Automator


I'm new to Automator.
There are many examples for simple actions.
But I couldn't find examples or documentation for launching some applications after a specific disk was mounted.
It will be very useful at work.
Has someone done this?


Solution

  • Ok, you want the Automator way, you get it :-D

    1. Create a new Automator action of type Folder Action
    2. Choose the Volumes folder of your System as input, I think you'll have to use Go to folder and type /Volumes
    3. As first action choose Execute Applescript
    4. Use the following script and define the first two variables to match your needs:

      on run {input, parameters}
      
          -- define the volume name and the application to start
          set triggeringVolumeName to "YOUR_VOLUME_NAME"
          set applicationToStart to application "Microsoft Word"
      
          -- walk through all newly mounted volumes
          repeat with aMountedVolumeAlias in input
      
              -- get the volume name from the given alias
              tell application "System Events" to set mountedVolumeName to name of aMountedVolumeAlias
      
              -- compare the volume name with the defined trigger name
              if mountedVolumeName is triggeringVolumeName then
      
                  -- launch the target application
                  launch applicationToStart
      
                  -- all is done stop checking
                  exit repeat
      
              end if
      
          end repeat
          return input
      end run
      

    The trick is to watch for changes inside the default mount point of your system (/Volumes). Everytime something is added to the folder, the AppleScript will be executed and aliases of the newly added items (aka the new volumes) will be inside the input parameter given to the script. We walk through the list of all item aliases and get the real name of the alias, compare it with our trigger name and in case of a match we start the application.

    Have fun with Automator, Michael / Hamburg