2

This should be a simple enough question to answer (I assume).

Say I want to activate window 1 of process "ScreenFlow", how would I do this?

I do know that Application('ScreenFlow').activate() brings Processes['ScreenFlow').windows[0] to the front. But how then to switch to Processes['ScreenFlow').windows[n]?

Thanks

Sancarn
  • 2,575
  • 20
  • 45
  • Do you want to bring that window to the front always, even if the user has focus in another application? (I'm not sure if macOS allows that - it's a bad idea to interrupt the user, and I personally had it when applications steal focus). – Dai Jan 13 '17 at 20:42
  • Note there is a difference between "bring to front" (e.g. displaying an always-on-top window) and stealing focus - which is what I believe `activate()` will do. – Dai Jan 13 '17 at 20:43
  • yeah sadly activate() doesn't do that with windows, at least for this application: `>> Application('ScreenFlow').activate()=> true` `>> Application('ScreenFlow').windows[1].activate() !! Error on line 1: TypeError: undefined is not an object (evaluating 'Application('ScreenFlow').windows[1].activate')` – Sancarn Jan 13 '17 at 22:46
  • that's not an error with `activate()`, that's a script logic error. What happens if you use Cocoa directly from within Swift or ObjC? – Dai Jan 13 '17 at 23:09
  • Is there a way to execute it from ObjC from JXA? - I've never done any coding in ObjC before... – Sancarn Jan 13 '17 at 23:42

1 Answers1

1

The action required to do this job is AXRaise:

function setWinFront(n) {
    var system = Application('System Events')
    var ScreenFlow = system.processes['ScreenFlow']
    Screenflow.windows[n].actions['AXRaise'].perform()
}

In the future when wanting to perform any changes such as this it maybe wise to look in process.windows[1].actions() to see if actions exist, and if they do - try them first.

Alternatively one can also look in the object's attributes using process.windows[1].attributes()

Sancarn
  • 2,575
  • 20
  • 45
  • Three years later, but (newbie?) question if you're around: I've tried a couple of what I thought might be obvious things but I'm not getting anything helpful. Probably missing something obvious... How does one "look in `process.windows[1].actions()` "? – DavidT Jun 24 '20 at 01:10
  • @DavidT iirc `process.windows[0].actions()` will return an array of actions, e.g. `[Application("System Events").applicationProcesses.byName("ScreenFlow").windows.byName("Untitled").actions.byName("AXRaise")]` note that Windows is indexed at 0 (0 is the first item) – Sancarn Jun 24 '20 at 23:31