Thursday, August 11, 2022

#1 2022-07-30 03:30:54 pm

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 199

Second frontmost Application

I'm having issues with "select second frontmost application", if that even exists.  I'm using Launchbar, which takes the focus when I launch an AS, which foils my world. 

Is there a way I can get the second frontmost application?  I'm using this, which doesn't work, since it points to Launchbar

Applescript:

set frontmostAppPath to (path to frontmost application) as text

So, I'm thinking since Launchbar is frontmost, I need to choose the app I was in, which would now be second in line.  That's an assumption.

Any insight appreciated. 

Cheers


Filed under: frontmost

Offline

 

#2 2022-07-31 07:05:32 am

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

Re: Second frontmost Application

Something like this:

Applescript:


tell application "System Events"
   tell process "Launchbar" to set its visible to false
   set frontmostAppPath to (path to frontmost application) as text
end tell


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

Offline

 

#3 2022-07-31 09:02:09 am

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

Re: Second frontmost Application

KniazidisR.  I don't have launchbar installed but thought I might simulate the situation by creating and saving a script to the desktop as an app named "Test". With Safari active, I double-clicked on the app icon, which returned "Macintosh HD:Users:Robert:Desktop:Test.app:".

Applescript:

set frontmostAppPath to (path to frontmost application) as text
delay 1
display dialog frontmostAppPath --> Macintosh HD:Users:Robert:Desktop:Test.app:

I then modified the script as follows and, with Safari active, double-clicked on the icon, which returned "Macintosh HD:System:Library:CoreServices:Finder.app:". I tried that with other apps active and always got Finder. My point is that setting the visible property of the frontmost app to false may not make the previously active app frontmost. Perhaps there is a flaw in my thinking?

Applescript:

tell application "System Events" to tell process "Test" to set its visible to false
delay 1
set frontmostAppPath to (path to frontmost application) as text
delay 1
display dialog frontmostAppPath --> Macintosh HD:System:Library:CoreServices:Finder.app:

BTW, to directly respond to the OP's question, it's a simple matter to get the frontmost application and a list of running (visible) applications, but I've never come across a way to get "second frontmost application".


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#4 2022-07-31 09:14:48 am

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

Re: Second frontmost Application

@peavine

When you open your saved script application with double clicking, the Finder.app opens your application, and as result Finder.app becomes the 2nd frontmost application. So, my script returns correct result following your actions.

You should open the app some other way, avoiding double-clicking.

Or, if you want open your app with double-clicking, then you should set visible of Finder.app as well:

Applescript:


tell application "System Events" to tell process "Test" to set its visible to false
tell application "System Events" to tell process "Finder" to set its visible to false

tell application "System Events" to set aName to name of (1st process whose frontmost is true)
set frontmostAppPath to (path to frontmost application) as text

tell application aName to display dialog frontmostAppPath

NOTE: when launching the app from the Dock, no need to set visible of Finder.app to false.

Last edited by KniazidisR (2022-07-31 09:39:26 am)


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

Offline

 

#5 2022-07-31 09:51:55 am

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

Re: Second frontmost Application

KniazidisR wrote:

@peavine

When you open your saved script application with double clicking, the Finder.app opens your application, and as result Finder.app becomes the 2nd frontmost application. So, my script returns correct result following your actions.

NOTE: when launching the app from the Dock, no need to set visible of Finder.app to false.



KniazidisR. I tested both of your above suggestions by double-clicking on the desktop icon and by running the app by way of the dock. Both work great. Thanks smile

Last edited by peavine (2022-07-31 09:52:25 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#6 2022-07-31 10:42:15 am

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 199

Re: Second frontmost Application

KniazidisR wrote:

Something like this:

Applescript:


tell application "System Events"
   tell process "Launchbar" to set its visible to false
   set frontmostAppPath to (path to frontmost application) as text
end tell



I see what you did there, but AS didn't like it:

Applescript:

64:88: execution error: System Events got an error: Can’t set process "Launchbar" to false. (-10006)

As for a list of running applications, that would be great, only if they were sorted by last time of focus, from which I could get the second entry in that array.  This is what I've found, and tried:

Applescript:

tell application "System Events" to set visibleAppsToKill to name of every application process where visible is true

The apps I'm using are all visible, as I'm interacting with them.  I tried moving in an order from Finder to Console, then Chrome and run that script in the Script Editor.  The choice of apps has no bearing on the sort order:

Applescript:

{"Finder", "Activity Monitor", "Messages", "Google Chrome", "iTerm2", "BBEdit", "TuneIn", "Signal", "Mail", "SpamSieve", "LanScan", "Console", "Script Editor"}

I'm not sure if this is doable.

Offline

 

#7 2022-07-31 10:54:41 am

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

Re: Second frontmost Application

daBee wrote:

The apps I'm using are all visible, as I'm interacting with them.  I tried moving in an order from Finder to Console, then Chrome and run that script in the Script Editor.  The choice of apps has no bearing on the sort order:

Applescript:

{"Finder", "Activity Monitor", "Messages", "Google Chrome", "iTerm2", "BBEdit", "TuneIn", "Signal", "Mail", "SpamSieve", "LanScan", "Console", "Script Editor"}

I'm not sure if this is doable.



The list reflects the order in which the processes were initially launched, not the order in which you opened the process windows. Therefore, I recommend that you forget about this "order".

Better, explain to me what is this Launchbar application that I don't see in your list of processes. Otherwise, I do not understand how it is activated (that is, takes focus).

Maybe, the name of process of application Launchbar is LanScan or Signal?

Last edited by KniazidisR (2022-07-31 11:03:12 am)


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

Offline

 

#8 2022-07-31 11:12:40 am

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 199

Re: Second frontmost Application

No, it is Launchbar.  It isn't in the invisible list either.  Lanscan and Signal are different applications.

What is it you want to know about Launchbar?  Looks like I can't post a small screenshot.

Last edited by daBee (2022-07-31 11:13:39 am)

Offline

 

#9 2022-07-31 11:39:46 am

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

Re: Second frontmost Application

I downloaded Launchbar app for free, to play with it.
You say "I'm using Launchbar, which takes the focus when I launch an AS"

launch HOW?  The AS tells to Launchbar app to do what?

Last edited by KniazidisR (2022-07-31 11:48:09 am)


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

Offline

 

#10 2022-07-31 11:43:42 am

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 199

Re: Second frontmost Application

Enjoy your play.  One of the best apps I've ever used.

Offline

 

#11 2022-07-31 12:09:45 pm

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

Re: Second frontmost Application

I'm not sure yet, but as I understand it, this is a wrapper application for other applications to use.

I think you want the last app that Launchbar.app visited. Try extracting its name from the
/Library/Application Support/Launchbar/Recent Documents.plist file of the user domain.

In this file, your application records which applications it has used last time, in the order in which they were used.

I'm still not sure I understood your question well.


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

Offline

 

#12 2022-07-31 01:40:03 pm

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 199

Re: Second frontmost Application

No, it's just a launcher.  ⌘-Space and you get to call certain items, files, applications, search websites, etc.  They have their own actions where you can embed Ruby, Bash and AppleScript scripts.  I use Ruby with embedded AppleScripts

So I have copied some text that I want to format using some replacements, so that I can post some text that's safe for StackOverflow.  Essentially I copy text into the Clipboard.  Then I use Launchbar to select a script that takes the Clipboard contents and pastes the converted text over what's currently selected text in whatever app I am in.  Doing so, puts the focus on Launchbar, which loses its focus.  It can't paste into Launchbar

Launchbar doesn't visit applications.  It's agnostic.  It can make apps launch or become active.

Offline

 

#13 2022-07-31 11:13:02 pm

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

Re: Second frontmost Application

I seems to me, the Launchbar.app application is very useful but I need time to learn it.

At this moment I created script application Test.app on my desktop. Then I launched it with Launchbar. The script app worked and returned its path to me.

Here is my Test.app application's content:

Applescript:


tell application "LaunchBar" to hide
tell application "System Events" to set frontmostAppName to name of 1st process whose frontmost is true

set frontmostAppPath to (path to frontmost application) as text

tell application frontmostAppName to display dialog frontmostAppPath

The same does following script saved as application:

Applescript:


tell application "LaunchBar" to hide
set frontAppPath to (path to frontmost application) as text

display dialog (get name of application frontAppPath) & ".app"

Last edited by KniazidisR (2022-07-31 11:27:10 pm)


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

Offline

 

#14 2022-08-01 07:43:30 am

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 199

Re: Second frontmost Application

OK, two things:

1.  Your choice of application, like you said, is from a list that is sorted by relative launch time.  Hence still not a way of finding that focus onto the relevant app.

2.  I'm launching from Actions.  On your system look for Action Editor.  Create an action with an applescript in it.  This way, it's a way to get a continuation from either the selection in the "frontmost app", or what's in the Clipboard.  I also want to obfuscate this so that it is applicable while in any application.  The text manipulations that I'm caught up on (upcase, downcase, convert one character to another), should work anywhere.  Browser, text editor, Messages, etc. 

If you need some quick guidance for that Action, let me know.

Offline

 

#15 2022-08-01 08:31:46 am

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

Re: Second frontmost Application

I suspect you want me, who is new to using the Launchbar app, to jump straight into the hard stuff. And even if I learn in a week how to create an action, it will not be the one that you are testing.

Better post 3 screenshots of the 3 tabs of the New Action window (which I have already opened), as well as the script (as simple as possible). So that we test the same thing, not different things. Otherwise, the topic will quickly reach 100 posts without solving the problem.

Also:

1. Pay special attention to the app's hide command (because for some reason, Launcbar.app doesn't lose focus in any other way (for example, it has property visible=false persistently, maybe it is bug). It should be the key to solving your problem.

2. Most likely, you do not get the application you need from my last scripts, because it is not 2nd, but 3rd in the window queue (or even 4th). Therefore, I recommend you: hide also the processes found by my script and then get the front process again. Repeat this until the desired application is found.

Last edited by KniazidisR (2022-08-01 08:32:11 am)


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

Offline

 

#16 2022-08-01 11:10:18 am

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 199

Re: Second frontmost Application

No, I thought you were doing this out of curiosity.  I don't expect anything.

General:  Doesn't matter. 
Scripts: "Run in Background" only.  Scripts choose Ruby.  I'm assuming you have it installed.  An version will do.  That script:

#!/usr/bin/ruby
# downcase selected text

require 'clipboard'
def osascript(script) = system 'osascript', *script.split(/\n/).map { |line| ['-e', line] }.flatten

mys=<<-TEXT
tell application "System Events" to keystroke "c" using command down
TEXT
osascript(mys)

Clipboard.copy(Clipboard.paste.downcase)

mys=<<-TEXT
tell application "System Events" to keystroke "v" using command down
TEXT
osascript(mys)

__END__

In your terminal, run this first:

gem install clipboard

In Resources, nothing matters. 

Then select some text with capitals anywhere, and it should turn it all lowercase, regardless where you are.  You can monitor this in the Console, searching for Launchbar.

Offline

 

#17 2022-08-02 01:27:20 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 176

Re: Second frontmost Application

The only way that I'm aware of to obtain a list of running applications sorted by most recently active is by way of the shell command lsappinfo.  It returns such a list when run on the command line like so :

lsappinfo visibleProcessList

You'll need to do some text manipulation to extract the names, which you need to bear in mind are process names rather than application names (although this works out well if it’s intended to be used with System Events).

Last edited by CK (2022-08-03 11:55:58 am)

Offline

 

#18 2022-08-02 01:44:54 am

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

Re: Second frontmost Application

CK wrote:

The only way that I'm aware of to obtain a list of running applications sorted by most recently active is by way of the shell command lsappinfo.  It returns such a list when run on the command line like so :

lsappinfo visibleProcessList

You'll need to do some text manipulation to extract the application names.


Wow, very useful post!  Thank you! smile
As I checked, Indeed this command lists applications in window activating order.

Applescript:


on getSecondFrontProcessName()
   set theInfo to do shell script "lsappinfo visibleProcessList"
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to quote
   set secondFrontProcess to text item 4 of theInfo
   set AppleScript's text item delimiters to "_"
   set secondFrontProcess to text items of secondFrontProcess
   set AppleScript's text item delimiters to space
   set secondFrontProcess to secondFrontProcess as text
   set AppleScript's text item delimiters to ATID
   return secondFrontProcess
end getSecondFrontProcessName

set secondFrontAppProcessName to getSecondFrontProcessName()

Last edited by KniazidisR (2022-08-02 01:55:14 am)


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

Offline

 

#19 2022-08-02 04:34:16 am

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 199

Re: Second frontmost Application

CK wrote:

The only way that I'm aware of to obtain a list of running applications sorted by most recently active is by way of the shell command lsappinfo.  It returns such a list when run on the command line like so :

lsappinfo visibleProcessList

You'll need to do some text manipulation to extract the application names.



Whoah, excellent post.  Out of nowhere.  Cheers, I can work with this.

Offline

 

#20 2022-08-02 04:41:33 am

daBee
Member
From:: Toronto
Registered: 2010-02-09
Posts: 199

Re: Second frontmost Application

KniazidisR wrote:



Applescript:


on getSecondFrontProcessName()
   set theInfo to do shell script "lsappinfo visibleProcessList"
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to quote
   set secondFrontProcess to text item 4 of theInfo
   set AppleScript's text item delimiters to "_"
   set secondFrontProcess to text items of secondFrontProcess
   set AppleScript's text item delimiters to space
   set secondFrontProcess to secondFrontProcess as text
   set AppleScript's text item delimiters to ATID
   return secondFrontProcess
end getSecondFrontProcessName

set secondFrontAppProcessName to getSecondFrontProcessName()


In Ruby:

choice = lsappinfo visibleProcessList`.split(': ')[1].split('"')[1].gsub('_',' ')

Last edited by daBee (2022-08-02 04:41:49 am)

Offline

 

#21 2022-08-02 09:33:41 am

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

Re: Second frontmost Application

This is a nice idea. A minor tweak, to account for appleScript applets (.app) and any apps (including appleScript applets) with _ in their names.

Applescript:


property visibleList : {}
property secondAppInfo : {}
on getSecondFrontProcessName()
   set ATID to AppleScript's text item delimiters
   set visibleList to do shell script "lsappinfo visibleProcessList"
   set AppleScript's text item delimiters to {"-\"", "\": "}
   set visibleList to text items of visibleList
   set secondFrontProcessID to item 3 of visibleList
   set secondAppInfo to do shell script "lsappinfo info -app " & secondFrontProcessID
   set AppleScript's text item delimiters to {"\"", ".app"}
   set secondFrontProcessName to text item 2 of secondAppInfo
   set AppleScript's text item delimiters to ATID
   return secondFrontProcessName
end getSecondFrontProcessName

set secondFrontAppProcessName to getSecondFrontProcessName()


display dialog secondFrontAppProcessName

Last edited by estockly (2022-08-02 09:43:42 am)

Online

 

#22 2022-08-02 09:40:22 am

robertfern
Member
Registered: 2011-11-29
Posts: 172

Re: Second frontmost Application

Here is a slightly shorter version where you can convert "_" to spaces in one line

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on getSecondFrontProcessName()
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to quote
   set secondFrontProcess to text item 4 of (do shell script "lsappinfo visibleProcessList")
   set AppleScript's text item delimiters to {" ", "_"}
   set secondFrontProcess to (text items of secondFrontProcess) as text
   set AppleScript's text item delimiters to ATID
   return secondFrontProcess
end getSecondFrontProcessName

set secondFrontAppProcessName to getSecondFrontProcessName()

Last edited by robertfern (2022-08-02 09:43:43 am)

Offline

 

#23 2022-08-02 03:27:45 pm

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

Re: Second frontmost Application

CK's suggestion is excellent and does exactly what the OP wants. Very nice smile

The following script returns all visible apps, which may be helpful on occasion. Finder is included in the list only if a Finder window is open. The timing result with five active apps and a Finder window was 12 milliseconds.

Applescript:

set appList to getAppList()

on getAppList()
   set appData to do shell script "lsappinfo visibleProcessList"
   tell application "Finder"
       if exists Finder window 1 then
           set finderWindowExists to true
       else
           set finderWindowExists to false
       end if
   end tell
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {"\""}
   set appData to text items of appData
   set AppleScript's text item delimiters to {" ", "_"} -- as suggested by robertfern
   set appList to {}
   if finderWindowExists then
       repeat with i from 2 to (count appData) by 2
           set end of appList to ((text items of (item i of appData)) as text)
       end repeat
   else
       repeat with i from 2 to (count appData) by 2
           if (item i of appData) is not "Finder" then set end of appList to ((text items of (item i of appData)) as text)
       end repeat
   end if
   set AppleScript's text item delimiters to ATID
   return appList
end getAppList

Last edited by peavine (2022-08-04 06:40:32 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#24 2022-08-03 03:50:50 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 176

Re: Second frontmost Application

Since we're going through the trouble of calling out to do shell script, it makes sense to milk it for everything it's worth, including having it perform the necessary text manipulations:

Applescript:

set runningApplications to the paragraphs of ¬
               (do shell script "lsappinfo metainfo |
               tail -1 | grep -Eo '\"[^\"]+\"' | tr -d '\"'"
)

which produces the following output:

{"Script Editor", "Vivaldi", "Mail", "Messages", "Finder", "Sidekick", ¬
"Telegram", "Usenapp", "Raycast", "Music", "Messenger", "Automator", ¬
"kitty", "Messages", "Karabiner-Elements", "Amazon Music", "Safari"}

The most recently active application is, then, item 2.

Offline

 

#25 2022-08-03 08:15:50 am

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

Re: Second frontmost Application

CK wrote:

Since we're going through the trouble of calling out to do shell script, it makes sense to milk it for everything it's worth, including having it perform the necessary text manipulations:

Applescript:

set runningApplications to the paragraphs of ¬
               (do shell script "lsappinfo metainfo |
               tail -1 | grep -Eo '\"[^\"]+\"' | tr -d '\"'"
)

...



Very cool. Especially that the metainfo correctly handles app names that have "_" and applets without adding the ".app" to the end.

And here's the same thing, without GREP.

Applescript:

property visibleList : {}
on getSecondFrontProcessName()
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {"\""}
   set secondFrontProcessName to text item 4 of (do shell script "lsappinfo metainfo | tail -1")
   set AppleScript's text item delimiters to ATID
   return secondFrontProcessName
end getSecondFrontProcessName

on GetRunningProcessNames()
   set runningProcessNames to {}
   set ATID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {"\""}
   set visibleList to text items of (do shell script "lsappinfo metainfo | tail -1")
   repeat with x from 2 to count of visibleList by 2
       set the end of runningProcessNames to item x of visibleList
   end repeat
   set AppleScript's text item delimiters to ATID
   return runningProcessNames
end GetRunningProcessNames

display dialog getSecondFrontProcessName()

choose from list GetRunningProcessNames()

Last edited by estockly (2022-08-03 08:28:09 am)

Online

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)