[X-Unix] Hiding apps from the command line

Eugene Lee list-themacintoshguy at fsck.net
Fri Mar 26 04:15:53 PST 2004


On Fri, Mar 26, 2004 at 12:12:33PM +0100, Kirk McElhearn wrote:
: 
: Very cool discussion, this. Here's the text I've written up for the next
: edition of Mac OS X Hints:
: 
: 15-1          Hide an Application from the Command Line
: Here's the scene: you're working in Terminal, and you've got lots of other
: windows behind the Terminal window. You want to get some of them out of the
: way, and you can naturally do so from the Finder. But why not find a way to
: do this from the command line?
: 
: Here's a shell script, by Eugene Lee, that fits the bill:

Kewl, I'm famous.  :-)

: #! /bin/sh

No spaces here, it should be "#!/bin/sh" (without the quotes).

: # 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

To be a proper here document, you must include the ending "END" string.
Otherwise, you cannot follow the AppleScript with more shell commands.
For example, let's show the time when the app was hidden.  This script
hides an app but unexpectedly prints the string "date".

	#!/bin/sh
	osascript <<END
	      tell application "Finder"
	      if exists application process "$1" then
		    set visible of application process "$1" to false
	      end if
	      end tell

	date

The corrected script hides an app and then runs the "date" command to
show the current time, which is what we intended.

	#!/bin/sh
	osascript <<END
	      tell application "Finder"
	      if exists application process "$1" then
		    set visible of application process "$1" to false
	      end if
	      end tell
	END
	date

: 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; this works even if you type hide safari, for
: example.
: 
: *********
: 
: You'll not that I put END on the first line, and that you don't need to have
: another END at the end.

You really should.  See above.

: Can we take this further and find a way to hide all other applications from
: this script?

Yep.  Here's the basic script.

	#!/bin/sh
	osascript <<END
	tell application "Finder"
		set visible of every process whose visible is true and name is not "Finder" and name is not "$1" to false
	end tell
	END

Remove the 'and name is not "Finder"' string if you want to hide the
Finder as well.

Of course with a bit more work, one could make an all-purpose shell
script that could something like this:

	hide [-oaf] appname

Without options, it will hide the application named "appname".

The options might be like:

	-o	Hide all applications other than "appname".
	-a	Hide all applications.
	-f	Hide the Finder.

where -o and -a by default do *not* hide the Finder.

Just an idea.


-- 
Eugene Lee
http://www.coxar.pwp.blueyonder.co.uk/



More information about the X-Unix mailing list