Show and hide apps from the command line

Apr 01, '04 10:32:00AM

Contributed by: kirkmc

Recently, someone on a mailing list asked if it were possible to hide applications from the command line. This got a few people thinking, and it ended up with a fairly complex, feature-rich shell script that can hide and show applications from the command line. But the script does more than just that - it can hide all but a specific app, hide the Finder, or quit applications too.

#! /bin/sh
# hide an application in the Finder
osascript <<END
  tell application "Finder"
    if exists application process "$1" then
      set visible of application process "$1" to false
    end if
  end tell
END
As usual, save this script, with a name such as hide in a directory that's in your PATH, make it executable by typing chmod 755 hide, then you can run it as follows:
 $ hide application_name
You'll need to type the actual application name that the Finder recognizes; move your cursor over the Dock to see: for example, Microsoft Word is the full name of the Office word processor, and you need to use quotes around any application name that contains spaces:
 $ hide "Microsoft Word"
You don't need to respect case; the script hides the Safari web browser even if you type hide safari, for example. You can take this script a lot further, adding a second argument, which works like options in standard commands preceded by a dash, and add more functions. Read the rest of the hint to see how to do that...

In this enhanced script, you can not only hide a single application, but you can also show an application, hide all applications, hide all applications but a specific one, hide the Finder, and more.

#!/bin/sh
# This script lets you hide a specified application, the Finder, all
# applications but the Finder, or all applications other than a specified
# application, as well as bring any application to the front or toggle
# hidden and visible applications.
# Script written by Eugene Lee, Kirk McElhearn and Simon Forster.
#
# Syntax is as follows:
# 
# hide [appname]    - hides specified app
# hide -x [appname] - hides all apps but specified app; if no app is 
#                     specified, Terminal remains visible
# hide -a [appname] - hides all apps but Finder; if an application is 
#                     specified, it remains visible as well
# hide -f           - hides Finder
# hide -v [appname] - brings specified app to the front, whether it                       
#                     is hidden or visible already
# hide -t           - toggles visible/hidden apps
# hide -s           - shows all apps
# hide -q [appname] - quits specified app
# 
# Applications whose names contain spaces must be quoted, i.e., 
# hide "microsoft word". This command is case-insensitive.

osascript <<END
  set arg1 to "$1"
  set arg2 to "$2"
  tell application "Finder"
    if arg1 begins with "-" then
      # the next two if's are supposed to be one line. They've been
      # broken here for spacing purposes. Enter each as one line!
      if arg1 contains "o" then set visible of every process whose 
        visible is true and name is not arg2 to false
      if arg1 contains "a" then set visible of every process whose 
        visible is true and name is not "Finder" and name is not arg2 to false
      if arg1 contains "f" then set visible of process "Finder" to false
      if arg1 contains "v" then tell me to tell application arg2 to activate
      if arg1 contains "q" then tell me to tell application arg2 to quit
      if arg1 contains "t" then
        set theVisible to name of every process whose visible is true
        repeat with theProcess in every process
          if theVisible contains name of theProcess then
            set visible of contents of theProcess to false
          else
            set visible of contents of theProcess to true
          end if
        end repeat
      end if
      if arg1 contains "x" then tell application "Terminal" to activate
      else if exists application process arg1 then
        set visible of application process arg1 to false
    end if
  end tell
END
Save this script in a file called super_hide, then run chmod 755 super_hide, and you're ready to go. See the syntax description at the beginning of the script for instructions.

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2004032703240345