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
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
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?
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.