Loading Apps upon start of macOS with individual Desktops

Hi Everybody,

as of today I am new to this forum and look forward to exchange tips and tricks, thoughts and ideas ... and to get also some help :wink:
Getting help is my first post about and I hope this is the right place.

I am trying to set up a routine, which runs every time I turn on my iMac and the macOS is loading. I want several applications to load in different desktops to keep it tidy. To do so, I am using a little Apple Script routine, which I have defined as a Service Routine triggered by ^⌥⌘D. The routine looks as follows:

on run {input, parameters}

do shell script "open -a 'Mission Control'"
delay 0.5
tell application "System Events" to click (every button whose value of attribute "AXDescription" is "add desktop") of group 2 of group 1 of group 1 of process "Dock"
delay 0.5
tell application "System Events" to key code 53

return input

end run

So what I am doing in Keyboard Maestro is to call the routine, give the iMac some time to cope with it and then call it again for three times.
Then I start loading the apps and sort on the different desktops. Between the different steps I have included some Pause functions, because I realized that the system needs some time to go ahead with the individual commands being executed.

My problems ist that the Keyboard Maestro routine shows up different results almost any time I test it.
Sometimes the Apple Script routine has problems, sometimes the Desktop are not being correctly addressed, sometimes the setup go the windows does not work.
What it never does is to load the script when I am starting the iMac - I have included Keyboard Maestro in the Login Items for my user, but it never executes upon start.

I have attached several print screens showing the routine hoping that somebody can help me to understand what I am doing wrong.

Again, I hope I am at the right space to ask for help - if not, just let me know and point me where to go, please.

For everybody a wonderful Christmas time!

Hi @Nebukadnezzar,

Welcome to the forum! You have definitely come to the right place. I'm far from the most experienced or technically savvy user here, but I think I can still help you out with your macro in a few areas.

Starting here:

This sounds to me like you have the Keyboard Maestro Editor set to launch upon startup, when what you really want is the Keyboard Maestro Engine. In case you haven't already, make sure that the "Launch Engine at Login" item is checked in KM's preferences:

and that your login item looks like this:

Next, I would use your AppleScript routine directly in an Execute an AppleScript action instead of trying to activate it as a service. In other words, I'd replace this action:

with this action:

On my Mac, I was able to consistently create two new desktops this way with only a 0.5 second pause in between:

Once it's time to launch applications, instead of an arbitrary pause, I'd suggest pausing until the app is at the front before executing your Move and Resize Front Window actions:

And with that, I think you should be able to significantly improve both the reliability of creating new desktops and your macro's overall speed. Feel free to post again if you have any further questions!

1 Like

Wow- thank you, that was already a big help. The idea to execute the Apps Script inside Keyboard Maestro is perfect and runs much smoother.

Now, I still have to play with the window management. The individual windows open based on their last condition, e.g. if a window was full screen before (like in Mail sometimes) it opens in full screen again and that mixes up the setup I have defined.
Also the move between the Desktops does not really work yet. I have - after opening the various Desktops- one right arrow keystroke defined to move to the next Desktop, but the system moves two Desktops :wink:
Funny enough: when I have it running in debugging mode, it runs perfectly and every window and program is in the Desktop I have defined it to be.

Not quite sure yet, what I am doing wrong!

Hey @Nebukadnezzar,

That sounds like a timing issue.

Use a Set Action Delay action at the beginning of your macro to set uniform pauses between actions.

Set Action Delay.kmactions (432 B)

Give it a decent sized pause time, and run your macro.

If the macro fails then increase the pause time.

If it works then decrease the pause time until it fails.

If you can get it working this way then you can fine-tune the timing manually.



Thank you - yes, it was a timing issue! Playing around with the Actions Delay helped a lot.
Now started the tweaking on the details:

  • I have to include a Pause for Slack to load - or is there a way to tell KBM to Pause until an application is fully loaded ?

  • While loading a program is there a way to check upon the window condition of the program (full screen, minimised, etc. ) and based upon the status define an action? I have some programs which restart in full screen mode and thus open a new Desktop and one remains unused.

Thanks a lot!

By fully loaded, I assume you mean until this window goes away and the proper Slack app appears?

This window appears to be titled just "Slack", so in this case, one way I can think of to wait until Slack is finished loading is to add a condition to Slack's "pause until frontmost" action to also checks that the front window title contains more than just "Slack":

Unfortunately there's no uniform way to determine whether an app has finished loading or not, so you need to get creative and think about what testable conditions are only in place once the app is in the state you want it.

I'm not sure about minimized, but there is an AppleScript-based way to check if the frontmost app is fullscreen or not, and to de-fullscreen it if it is. Combine that with KM's Pause Until action to ensure that the frontmost app is no longer fullscreen before proceeding, and I think you should be good to go:

###AppleScript to Set Fullscreen Apps to Non-Fullscreen

	tell application "System Events"
		tell (first application process whose frontmost is true)
			tell (first window whose subrole is "AXStandardWindow")
				set fullScreen to value of attribute "AXFullScreen"
				if fullScreen = true then
					set value of attribute "AXFullScreen" to false
				end if
			end tell
		end tell
	end tell
on error e number n
	set e to e & return & return & "Num: " & n
	if n ≠ -128 then
			tell application (path to frontmost application as text) to set ddButton to button returned of ¬
				(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
					default button "OK" giving up after 30)
			if ddButton = "Copy Error Message" then set the clipboard to e
		end try
	end if
end try

###AppleScript Used in Pause Until to Determine Fullscreen Status (i.e. wait until script returns "false")

tell application "System Events"
	tell (first application process whose frontmost is true)
		tell (first window whose subrole is "AXStandardWindow")
			value of attribute "AXFullScreen"
		end tell
	end tell
end tell

(full credit for these scripts goes to @ccstone; I just figured out a way to tweak them for this situation)

1 Like

Thank you! Though I am using the Pause function, the KM continues to execute - this is all about timing as when I am increasing the Action Delay to 1.5 it works.

By the way, the script looks very useful but delivers me an error message that says:

System Events got an error: Can’t get window 1 of application process 1 whose frontmost = true whose subrole = “AXStandardWindow”. Invalid index.

Num: -1719

which seems to be an invalid index …

But this forum is great, your help is tremendous and I am making much regress, thank you!

You’re quite welcome! I’m not sure why the script(s?) aren’t working for you though; I just tested them both again and they work fine for me. Is there a particular app that always makes them fail? The simplest way to troubleshoot this would be for you to upload your full macro; in case you don’t know how, you can find instructions here: https://wiki.keyboardmaestro.com/Forum#How_to_PostUpload_Your_Macro_to_the_Forum

Here it is ... the complete macro ... sure a rookie version :wink:

Keyboard Maestro 8.0.4 “Load macOS” Macro

Load macOS.kmmacros (24 KB)

So I have uploaded the complete macro - I have just started with KM and wanted to spend some hours the next few days with some tutorials to learn better the architecture of KM and the possibilities of the system. However, any suggestion highly welcomed!

Thanks! This made it much easier to figure out the problem: DEVONthink launches with a loading window similar to Slack that does not respond to the script, so you need to ensure that, like Slack, DEVONthink is finished loading before proceeding with the macro. This worked in my testing with zero delay between actions:

1 Like

THANKS A MILLION!!! Works like charme!

Thank to this forum I have my first macro up and running - great!

Last question I have: do you have any recommendation for a nice and effective tutorial (youtube?) or do you recommend to go through the library of macros and the forum ?

Again, my big thanks!

1 Like

Glad that did the trick! As for video tutorials, I’m afraid I don’t have any good recommendations, but if you haven’t already, I can suggest reading the Quick Start, looking over KM’s triggers and actions, perusing the Best Macro List post and Macro Library category to get an idea of what’s possible, and generally keep visiting this forum. The more ways you see how KM can be used, the better you’ll be able to think of how to harness it for your own needs. And don’t be afraid to keep posting here, even for questions that you feel are too simple to merit a response! (though don’t forget to also try searching the forum for those questions first, since chances are very good that whatever they are, someone else has posted about them before :slightly_smiling_face:)

I found a nugget here. Every time my iMac starts several apps are launching. I click some of those to minimise them. like iTunes and KM. I have looked in many places but never ventured into the Systems settings of macOS. But now, inspired by your screenshot, I have made iTunes and KM start up but not using pixels on my desktop. and wauw it does what I desired

curiosity is great, a good forum is even greater :slight_smile:


1 Like