Script to Hide Apps

This is my first script, and I plan to use it as a startup script. I’ll either put it in Startup Preferences (10.3) or Dock for one click launch of e-mail, browser, and Finder. It’s very simple and currently works, except I’d like to add Hide command so that after Entourage and Safari launch, they are then hidden.

launch application “Microsoft Entourage”
launch application “Safari”
tell application “Finder”
activate
open folder “Macintosh HD:Users:jcrosby:Documents”
end tell

I will keep Finder active, so any thoughts about how to insert “hide” command following launch command for both Entourage and Safari?

I’ve tried:

hide application “Safari”

and other, seemingly, intuitive commands, but no success. As an aside, information about hiding has been difficult to find in both books and on websites. This site being one of the exceptions.

Thank you in advance,

Formac

First, you can just add the applications to the startup items in your account in the Accounts preference pane and set them to be hidden–no scripting necessary. If you want to hide applications using AppleScript, however, try searching this site for the words “hide all applications” (make sure you set it to search for all terms and limit the search to the OS X forum)–the search feature makes this site much more powerful and could probably answer most questions people have.

Jon

This works in Jaguar to hide all apps…

http://scriptbuilders.net


[This script was automatically tagged for color coded syntax by Script to Markup Code]And to un-hide them…

http://scriptbuilders.net


[This script was automatically tagged for color coded syntax by Script to Markup Code]

Jon, thanks and I am aware of Startup items (10.3) for launch and hide function. I am also looking for an Applescript equivalent.

Greg, thank you for the script. I’ll try it. Yet, one simple question: If launching an application is as simple as–

launch application “Safari”

why is the hide function so verbose?

Since Applescript is so “language based,” it seems that hiding could also be relatively simple–

hide application “Safari”

Thanks,

-Formac

Hey, sorry so late to the game… actually, I came here looking for the answer to the same question, but didn’t quite find it. Instead, I just came up with my code based on the few pre-installed scripts on the Mac. I wanted to create a script that would start up Mail, Safari and iCal, because sometimes I need to quickly log in to work on something and I don’t want to wait for these three (or more) apps to start up automatically everytime…

Okay, so, yeah - the script:

launch application “Mail”
launch application “iCal”
launch application “Safari”
tell application “Finder”
set visible of every process whose name is “iCal” to false
end tell

I’m definitely interested if anyone knows a way to do this much more succinctly.

Hey, wow! My first script! Why did I wait so long to get started on this stuff???

Well, anyways, glad sites like this are around…

kid A,

Welcome to the world of AppleScript and to the BBS! Your script might be more efficient if Finder isn’t forced to look at all processes to find iCal. You can also send the same command to System Events. This might be the preferred method since, if I recall correctly, the Finder hands the command to System Events anyway (OS X only). This change probably won’t offer a visually noticeable difference but every little bit of resource preservation helps. :slight_smile:

tell app "System Events" to set visible of process "iCal" to false

Here’s an example that uses System Events, demonstrates how to use a repeat loop to work on a list of items and how to use a repeat loop to wait for a condition to be met.

set apps_ to {"iCal", "Mail", "Safari"}

repeat with app_ in apps_
	launch application app_
end repeat

-- Give iCal time to fully launch and then hide it
set iCal_visible to true
tell application "System Events"
	repeat until iCal_visible is false
		delay 1 -- pause script for 1 second
		set visible of process "iCal" to false
		set iCal_visible to visible of process "iCal"
	end repeat
end tell

Have fun!

– Rob

Rob,

Much thanks! The “System Events” thing is definitely good to know! That worked like a charm…

And thanks for your rewrite of the script you sent along as well… You even answered some of my unasked questions. I was looking too for a way to open all the apps in one line (comma delimited, or however). So you’re opening bit of code, is that an array (not too familiar with tons of scripting jargon, apple or otherwise)?

So I’m looking over the script your wrote, and I’m trying to break it down, bit by bit. You have the second half (the part where you kindly comment “Give iCal time to fully launch and then hide it”). I think I follow what’s going on in there… basically you’re creating a loop to tell “System Events” to check through the open apps until it finds iCal, then give a second delay, and then tell it to set the visible to false… am I right so far? I’m curious about the “giving it time” bit… Is this truly necessary? Is it essentially another way of saving on resources? I don’t doubt you in the least, I truly want to know how this benefits… what are its advantages over merely putting in the line:

tell application “System Events”
set visible of process “iCal” to false
end tell

… as you had mentioned earlier?

Also, in your line “set iCal_visible to true”, what exactly is “iCal_visible?” Is that a variable you’re defining, or is it a term (or format of attributes) that AppleScript already knows?

Thanks again! Good stuff! I just started trying to learn this stuff only minutes before I sent my post last night, and now I’m completely hooked!

  • kid A

…or even…

could you merely put in the following…

set apps_ to {“iCal”, “Mail”, “Safari”}

repeat with app_ in apps_
launch application app_
end repeat

delay 1

tell application “System Events”
set visible of process “iCal” to false
end tell

??? Would that do?

And I meant to ask earlier, do more verbose scripts take longer by any significance? And (I know this question might be more philosophical than anything) which is better: concise scripts or “clean” scripts… By “clean” I mean that I know that apps can do things you want them to do even with “ugly” code, but said apps sometimes would really prefer that you play more nicely… bad syntax/screwy coding might not cause the apps to choke exactly, but to sort of stutter…

Much gratitude again,

  • kid A

kid A,

The entity that you refer to as an array is commonly referred to as a list in AppleScript.

Regarding the delaying of the script during the second repeat loop, my tests indicated that the script would sometimes fail if iCal wasn’t finished with its initialization. If it can happen on my setup, it can happen on others, so I decided to play it safe. :slight_smile:

Regarding iCal_visible, it is a variable, as you surmised. Once you get used to working in a script editor, you’ll be able to identify various entities (variables, keywords, etc.) based on their text format. Tip: Take a look at Script Editor’s prefs (Formatting) to get an idea of what the various formats indicate and, if desired, change them.

Regarding verbosity, I can’t really speak with authority on its relation to code execution. Much of the extraneous stuff (such as the word “the”) is provided simply to make the code more readable (English-like) during the editing process and it doesn’t have an impact when the code is executed. I prefer to keep my code as clean as possible while still maintaining enough verbosity to understand a script if I open it months or years later. Other scripters sometimes prefer single character variable names which provide no immediate indication, to others, of what they might hold. I prefer to be descriptive when choosing variable names. :slight_smile:

I hope this makes sense but, if it doesn’t, feel free to ask for clarification.

– Rob