Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Show and hide apps from the command line Apps
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.
    •    
  • Currently 1.75 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (4 votes cast)
 
[14,645 views]  

Show and hide apps from the command line | 9 comments | Create New Account
Click here to return to the 'Show and hide apps from the command line' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Show and hide apps from the command line
Authored by: MordEth on Apr 01, '04 03:02:25PM

this hint can be sped up by using:

tell application "System Events"

instead of:

tell application "Finder"

in both cases, especially for users who run Path Finder in place of the Finder. if Finder isn't running, the hide AppleScript has to open Finder to execute the application hiding, whereas System Events is always running.



[ Reply to This | # ]
Show and hide apps from the command line
Authored by: kupietz on Apr 23, '04 07:53:20PM

Caution: My experience in my iBook 600 running Panther is that the System Events background app eats between 40 and 80% of my CPU cycles, rendering any other open applications unusably slow. The only fix for this is using Activity Monitor to quit System Events.



[ Reply to This | # ]
Show and hide apps from the command line
Authored by: MordEth on Apr 01, '04 04:16:23PM

some additional notes, after playing with it a bit more...you can escape apps with spaces like "microsoft word" like "microsoft\ word" rather than having to quote it. (i.e. "hide microsoft\ word".)

also, for anyone who's interested, i've posted a somewhat rewritten version of the "super_hide" script at http://mordeth.pankurokku.com/files/scripts/hide. basically, the main difference is that the name of the script by itself returns the script's syntax, and contains a variable named $FINDER. it is written for Path Finder users, but if you use Apple's Finder instead, you can either delete the line:

FINDER="Path Finder"

and replace all instances of "$FINDER" with "Finder", or just change "Path Finder" in the variable to "Finder".



[ Reply to This | # ]
Show and hide apps from the command line
Authored by: sjk on Apr 01, '04 10:45:43PM
Also see Nicholas Riley's appswitch utility.

[ Reply to This | # ]
Show and hide apps from the command line
Authored by: pmccann on Apr 01, '04 11:01:16PM

See also "quicksilver" (my current love: hmm, sad, isn't it...)

cmd-space w tab h return

and you're done. (cmd-space activates quicksilver, w selects word (see below), tab activates the "action" palette, "h" selects the "hide" action from the list. No need to remember the name, or use quotes etc etc.

cmd-space w return

and your back (I'm assuming you've "told" quicksilver that "w" should point at Microsoft Word). Command line's nice, but this is *much* nicer. And my hands are on the keyboard at all times. Launchbar will do the same thing of course.



[ Reply to This | # ]
Show and hide apps from the command line
Authored by: DavidRavenMoon on Apr 02, '04 01:06:44PM

Seems like an awful lot of work when just right clicking on the app's Dock icon and selecting hide works just fine (and is faster)!



[ Reply to This | # ]
Show and hide apps from the command line
Authored by: Frederico on Apr 02, '04 08:48:15PM

Can anyone explain why, even after compiling this script and reducing it to typing one command and one app name, that this is preferable over Panther's built-in app-switcher? Or just the long-click or Control-click in Dock as indicated above?

Command-Tab to bring up App-Switcher; Tab, Shift-Tab, or Arrow-key your way to the target app; H or Q to hide/quit the target app.

This feature in Panther, stolen from Michael Kamprath's original App Switcher (now Keyboard Maestro, still useful in Jaguar), is one of the more attractive value-added features in Panther.

About the only use I could foresee for my purposes for the above app is to use it on a remote machine to which I have SSH access, but not VNC. I love alternative ways to do things, really, and I'm not interested in arguing, only in learning where this is useful over what GUI/keyboard tools we already have.

Anyone?



[ Reply to This | # ]
Show and hide apps from the command line
Authored by: kirkmc on Apr 03, '04 03:26:31AM

Because some people like to type things. :-) And because you can do it from the Terminal.

This all started out as a question on a mailing list, then turned into a little proof-of-concerpt sripting exercise among several people. If you don't like it, that's fine. But it does show how you can combine shell scripting and AppleScript to do something that is not generally a Terminal operation.

This said, typing hide bbedit is much faster than Command-Tab, arrow, H.



[ Reply to This | # ]
Show and hide apps from the command line
Authored by: Frederico on Apr 03, '04 08:19:24PM

Hmmm.... I type ~110wpm, and I think I can still keystroke an app away faster -- not to mention right-clicks with Fruit Menu to hide/show this or that app; or the global (yes, Virginia, you can make it global in Panther!) Command-Option-H to hide everything but the current app.

I guess my twenty years of GUI is showing; I've been using MK's App Switcher for years, so it's just truly second nature, anymore.

But, I do enjoy watching how people integrate AS and shell actions; though, coming from the AS side of things in a big way, I tend to write AS that does things in Terminal moreso than the other way around.

Thanks for the reply; I appreciate the discussion. (:

Cheers

F



[ Reply to This | # ]