Starting and hiding applications

Hi Scripters,

I’m quite new to the Apple programming/scripting language as I had a Windows machine long time and used VB, VBA and VBS there.

Recently, I created a script, which starts 2 applications (Adium and Skype) simultaneously. The cause is simple: I have a Logitech Elite keyboard with exact one Messenger/SMS button and I wish to put this into use effectively, starting not one but all used IM applications (in the current situation 2).

Starting an application on its own is quite simple and I achieved it already. The tough part is now to get the windows hidden by closing them. Both applications support this without quitting and the commands I also already figured out. Giving enough delay after starting, this works fine. However, it is not exactly what I had in mind as the script runs for quite some time to give the applications enough time to start up even under busy conditions (letting it run too long under non-busy conditions).

As I experienced that, if an application is already open, the script is not opening the primary window by ‘re-launching’ it. Hence, the script runs into an error.

Trying further, I created not only a condition to verify, if the window exists, but also a loop to shorten the script runtime. So, the script retries every 5 seconds to close the windows putting in a variable that indicates, whether the script already closed the window of that application to provide the information if all windows have been closed to end the loop.
That, however, led into other issues: Not only does Skype obviously ‘emulate’ the open buddy window even as it is closed running through that condition and sets the variable without closing the window as it appears after the script comes to that line, I, as of now, fail to understand how to increase a variable in a loop to prevent the loop from running indefinitely*).

*) As either or even all applications are open, the main (buddy) windows are not opening, and hence the script is not running through the condition to close the windows and remains running.

After much explanation, this is the current state of the script:

global Adium, Skype, delaysecs, counter, countermax
set Adium to true
set Skype to true
set delaysecs to 5
set counter to 0
set countermax to 5

tell application "Finder"
	launch application "Adium"
	launch application "Skype"
end tell

	if Adium is false and Skype is false then exit repeat
	(* I wonder how to increase the below variable *)
	(* counter = counter + 1 *)
	display dialog "Counter: " & counter buttons {"Ok"} default button 1 (* Just checking the variable during debugging *)
	if counter is greater than countermax then exit repeat
	delay delaysecs
	(* ========== Adium ========== *)
	tell application "Adium"
		if Adium then
			if window "Contacts" exists then
				close window "Contacts"
				set Adium to false
			end if
		end if
	end tell
	(* =========================== *)
	(* ========== Skype ========== *)
	tell application "Skype"
		if Skype then
			if window "Skypeâ„¢ - Phoenix" exists then
				close window "Skypeâ„¢ - Phoenix"
				set Skype to false
			end if
		end if
	end tell
	(* =========================== *)
end repeat

If anyone has a hint on how to shorten the script runtime to do the necessary tasks (5 to be precise), while flawlessly executing them is much appreciated:

  • Opening Adium
  • Opening Skype
  • Hiding Adium
  • Hiding Skype
  • End script after application windows are hidden (or at latest after the maximum repeat cycles have been run)


Model: MacBook Pro
AppleScript: 1.10.7
Browser: Firefox
Operating System: Mac OS X (10.4)

Hi Phoenix,

I don’t use Adium, but for Skype you can take this

activate application "Skype"
tell application "System Events"
	tell process "Skype"
		repeat until exists group 2 of window 1
			delay 1
		end repeat
		set visible to false
	end tell
end tell

it hides the application when the buddy window appears

Hi StefanK,

Thank you for your reply! :slight_smile:

I don’t worry too much about Adium either as this application is quite good doing what I expect. :slight_smile:
I will have a look for Skype with your code. Though, I got the impression that with your solution you supply a +H command to Skype, which would rather hide than close the buddy window (if not the entire application).

Also, as I am rather new to AppleScript, I would like to know what “group #” stands for. Could be handy for the future. :wink:

Another thing is that, when you ‘re-open’ Skype or Adium when they are already running, neither will show a window to be closed/hidden. At that point the script would run until you actually use Skype/Adium and would close the window from directly under your mouse, so you are forced to re-open it manually to get the window another time.

The scenarios I am thinking for this script is:

  • Starting all IMs at once after I woke my computer from Sleep (I usually quit the programs before to prevent misbehavior after the computer returns)
  • Re-starting a crashed or unstable program (either Adium or Skype), while the other program is still running*)
  • Accidentally hitting the button while both of the programs are running

*) For Skype, it is very convenient that the previous chat window re-appears when you re-launch the application, if that was open. Therefore, I would like to just close the buddy window and do not want to hide the program alltogether.

So, basically, I like to have the script closing the buddy window only and only for a short period of time (e.g. up to 30 seconds). After that, the script shall end (assuming that both applications are running and are not intending to show their respective windows to close them).

Example #1:
I am starting both applications. After 10 seconds both windows appeared and the script closed both of them. Then it shall end immediately.

Example #2:
I am starting the script while either or both applications are running. At least one program will never display its window and will let the script run in circles. After 30 seconds, the script gets interrupted by the cycle maximum as it is presumed that both applications are running and hidden now.

Any help is very appreciated. :slight_smile:

Kind Regards,

I only tried to follow your intention :wink:

It’s just an element of the user interface: I used it to specify a certain window.

Closing a window causes in almost every case that you must reopen it by shortcut or menu.
Hiding an application restores the current window set when you click the icon in the dock or double-click the application in the Finder

Hi Stefan,

I see now. Yes. I obviously made the summary a bit too short. :wink: Sorry for that. I hope my second post made it a bit more clear what I was up to.

Thing is, that I currently do it manually with each application. Opening the application, then closing the buddy window (not the re-appearing chat window, if Skype crashed with that open as I usually then continue to chat). So, I thought to automate this. Unfortunately, the script is not waiting for the window to open, so a delay needs to be in place. And as the system is busy differently, the waiting period needs to be variable as well. And to not let the script run indefinitely, there needs to be a break somewhere to make sure that the script gets interrupted after e.g. 30 seconds runtime, if no window appears.

If I would know how to increase a variable, I would be set, I guess.

  • In VB/VBA it usually went like: variable = variable + 1
  • In C++ it is usually: variable++;

Question is now: How does it work in AppleScript? Or are there other (easier) methods to take the time to break a loop? Or are there even complete other ways to complete this entire 5-step-task?

Kind Regards,

set variable to variable + 1

PS: in the case of Skype my script above waits until the buddy window has been opened

Hi Stefan,

Thanks for your fast answer(s). :slight_smile:

Exactly. Even that this may be 5 minutes or 2 hours later. :wink: At that point, Skype might never have presented its window (as it may be running already).

And that would be happening at that point:
After 15 minutes you decided to actually use Skype actively and are clicking on the Dock window icon to get the buddy window in the foreground to select the buddy of your choice to chat with. However, forgetting that the script is still running in the background just waiting for the buddy window to appear. :wink: Now the inevitable happens and Skype gets hidden by your script and you have to select the Skype icon from the Dock a second time.

That is, why I want to have a script break after a short time. So that, if the script has not done anything after a specific time, it simply quits. As in my first post, I have the script execution on a key of my keyboard. So, re-execution is quite fast, should I need it again.

Thank you again for your help for a peculiar person like me. :wink: I will heed your advices and try it out. Will let you know about the result.

Kind Regards,

Hi Stefan,

Yes. The increase of the variable works. Though currently, I face some other issues not been there before. However, in general, it works now as it should. At least internally. Just need to figure out, why it suddenly goes through both routines despite no window displayed. At first, it was just Skype.

Thank you for your support! :slight_smile: You were a real great help! :cool:

Kind Regards,