SOLVED: Get number of Space of active application

Hi!

I need to get the number of the space where a certain (or the active) application has its window (to pass it on to Keyboard Maestro).
I basically don’t know any ruby so it would be great if somebody could help me out here.
I know I can get the current space with TotalSpaces2.current_space.
If the target application has a window open on a space that is not the current one, how would I write the space number to a variable or print it?
TotalSpaces2.window_list lists the Application with its corresponding space correctly. It’s just a matter of getting this number :confused:

Any help is highly appreciated!

Edit:

I am able to get the Space with

windows.select {|window| window[:app_name] == 'ApplicationName.app'}

However, is it possible to get the space of the active application so that I don’t have to specify the application name?

Is not the space of the active application the current space?
(I think I don’t quite follow what you want)

Sorry for not being very clear. Let me give an example:
I’m on space one and the active application is Calculator - which happens to have its window open on space 2 atm, is there a way for me to ‘ask’ totalspaces where the window of Calculator currently is? This should return ‘2’ even though I’m on space ‘1’ and Calculator is the active application.
Thanks for trying to understand me :wink:

Ah, now I understand, I forgot that some folks do not have “When switching to an application, switch to a Space with open window for the application” ticked in Mission Contol prefs.

Maybe use applescript?

In ruby:

%x{osascript -e 'tell application "System Events"\nset frontmostApplication to name of the first process whose frontmost is true\nend tell'}.chomp

eg

irb(main):007:0> %x{osascript -e 'tell application "System Events"\nset frontmostApplication to name of the first process whose frontmost is true\nend tell'}.chomp
=> "Terminal"

OK, applescript works as well for me. But this gives me the name of the application rather than the space number where its window is living… or am I doing something wrong here?

Thank you for your help btw, it is appreciated!

Now you have the name automatically, can you use the technique from your first post to get the space number?

Of course. Thank you so much for your help, works beautifully and saves me from creating a ***load of scripts for every single application that I need in my workflows.