Thursday, October 6, 2022

#1 2022-05-30 07:39:51 am

NMG
Member
Registered: 2019-09-29
Posts: 65

The application of the frontmost window

How to know what is the application of the frontmost window?

Something like:

if (frontmost window is Pages) then
    set program to "pages"
else if (frontmost window is Numbers) then
    set program  to "numbers"
else
        set program to "other"
end if

Offline

 

#2 2022-05-30 08:14:32 am

Otto
Member
Registered: 2020-09-18
Posts: 8

Re: The application of the frontmost window

Applescript:

tell application "System Events"
   get name of first process whose frontmost is true
end tell

Offline

 

#3 2022-05-30 08:54:49 am

NMG
Member
Registered: 2019-09-29
Posts: 65

Re: The application of the frontmost window

Ok, I get the answer "Script Editor" And if I export this as a program I will get that program as the frontmost. So, I suppose I should ask what is the second window?

Offline

 

#4 2022-05-30 09:39:13 am

NMG
Member
Registered: 2019-09-29
Posts: 65

Re: The application of the frontmost window

In fact, I only need to activate the last window from Pages or Numbers (That is the last active window apart from the script editor or the program generated by the script editor)

Offline

 

#5 2022-05-30 11:34:32 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1469

Re: The application of the frontmost window

NMG wrote:

In fact, I only need to activate the last window from Pages or Numbers (That is the last active window apart from the script editor or the program generated by the script editor)



I'm not certain if I understand what's required but you can give the following a try:

Applescript:

tell application "System Events" to tell process "Numbers"
   set frontmost to true
   perform action "AXRaise" of the last window
end tell


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#6 2022-05-30 01:35:53 pm

NMG
Member
Registered: 2019-09-29
Posts: 65

Re: The application of the frontmost window

I try to know if the frontmost window is Pages, Numbers, or something else

Last edited by NMG (2022-05-31 03:12:00 am)

Offline

 

#7 2022-05-30 01:57:53 pm

estockly
Member
Registered: 2009-01-03
Posts: 132

Re: The application of the frontmost window

NMG wrote:

I try to know if the frontmost window is Pages or Numbers



Applescript:

tell application "System Events"
   set frontmostName to name of application process 1 whose frontmost is true
end tell

if frontmostName is "Pages" then
--do some stuff
else if frontmostName is "Numbers" then
--do some other stuff
else
--do nothing or something else
end if

Offline

 

#8 2022-05-31 03:19:07 am

NMG
Member
Registered: 2019-09-29
Posts: 65

Re: The application of the frontmost window

That is similar to Otto answer. This is useful. Thank you. But as I said to him, the problem is that the frontmost is the Script Editor

Offline

 

#9 2022-05-31 05:40:24 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2535

Re: The application of the frontmost window

Something like this. Hide the current application.

Applescript:


set currentApp to name of current application

tell application "System Events"
   set visible of process currentApp to false
   set frontAppName to name of first process whose frontmost is true
end tell

Last edited by KniazidisR (2022-05-31 10:09:34 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#10 2022-05-31 06:40:33 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1469

Re: The application of the frontmost window

NMG wrote:

That is similar to Otto answer. This is useful. Thank you. But as I said to him, the problem is that the frontmost is the Script Editor



The frontmost app will vary, depending on how your script is run and its format.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#11 2022-05-31 09:13:34 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 243

Re: The application of the frontmost window

FWIW, if you run Otto's script from the general script menu, it will do what you want (at least it does in Sierra).


Applescript:

tell application "System Events"
   set xy to name of first process whose frontmost is true
end tell
display dialog xy

As an aside, if pages/numbers are the second app (when script editor or a saved script is the first), you could probably ask for a list of open apps with the desired app having an index of 2, and then get its name. Haven't tested this so maybe I'm out on a limb.

Offline

 

#12 2022-05-31 10:18:46 am

estockly
Member
Registered: 2009-01-03
Posts: 132

Re: The application of the frontmost window

NMG wrote:

That is similar to Otto answer. This is useful. Thank you. But as I said to him, the problem is that the frontmost is the Script Editor



I see. Okay, that happens when running the script from within Script Editor, because Script Editor is frontmost at that time.

If you save the script in the Scripts folder and run it from the Script menu or run it from an applet, then it will tell you which Application frontmost at the time.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)