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.

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:

[format]#!/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[/format]

In your terminal, run this first:

[format]gem install clipboard[/format]

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.

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 :[format]lsappinfo visibleProcessList[/format] 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).

1 Like

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


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()

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

In Ruby:
[format]choice = lsappinfo visibleProcessList`.split(‘: ‘)[1].split(’"’)[1].gsub(‘_’,’ ')[/format]

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


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

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


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()

CK’s suggestion is excellent and does exactly what the OP wants. Very nice :slight_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.

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

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:

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

which produces the following output:[format]{“Script Editor”, “Vivaldi”, “Mail”, “Messages”, “Finder”, “Sidekick”, ¬
“Telegram”, “Usenapp”, “Raycast”, “Music”, “Messenger”, “Automator”, ¬
“kitty”, “Messages”, “Karabiner-Elements”, “Amazon Music”, “Safari”}[/format]The most recently active application is, then, item 2.

1 Like

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.

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()

Thanks so much for this! You just saved me a lot of time. I’ll use that in a window management LaunchBar action/script.

I have a very similar version to stocky but mine uses the ‘visibleProcessList’ option for ‘lsappinfo’

It is 40% faster according to “Script Geek”

on getFrontToBackProcessName()
	local theInfo, processList, tid, i
	set tid to text item delimiters
	set text item delimiters to quote
	set theInfo to text items of (do shell script "lsappinfo visibleProcessList")
	set processList to {}
	set text item delimiters to {" ", "_"}
	repeat with i from 2 to count theInfo by 2
		set end of processList to (text items of item i of theInfo) as text -- this replaces the "_" with a space
	end repeat
	set text item delimiters to tid
	return processList
end getFrontToBackProcessName

Also whenever I see “Applescript’s text item delimiter” I know the code is of an older vintage.
Apple removed the need to preface ‘text item delimiters’ with ‘Applescript’s’ a long time ago.
I’m also persnickety in that I always have a ‘local’ line in my procedures just incase the variable names I’ve used are in conflict with other code. (ie. I always declare my variables, probably OCD)

1 Like

FWIW, I get a 10006 error when I attempt the following:

tell application "Safari" to set text item delimiters to {"… ", "... ", "…", "..."}

Nothing worse than code that’s off. Of course, I’m using v2.5.

Why are you telling Safari.
Setting text item delimiters is a basic command. no need to be in a ‘tell’

This works…

tell application "Safari" to set AppleScript's text item delimiters to {"… ", "... ", "…", "..."}

Text item delimiters belong to appleScript, but an app can have their own. It’s OK to set them in a tell application block, but you need to specify you mean AppleScript’s.

I don’t think so. (not trying to be snarky)
Run this and you’ll see it changed the AppleScript delimiter, not some value just for Safari

get text item delimiters
tell application "Safari" to set AppleScript's text item delimiters to {"… ", "... ", "…", "..."}
get text item delimiters

if you are going to set the delimiters in a tell block, you should use:

tell application "Safari" to set my text item delimiters to {"… ", "... ", "…", "..."}

‘AppleScript’s’ was deprecated a long time ago

I don’t believe it was deprecated. I believe it’s no longer required. There’s a difference.

(I still use “AppleScript’s” in all my scripts because it’s in all my clippings and old scripts and handlers.)

By “my” works too, and there’s less to type.

1 Like

I made a text shortcut using atid which expands into the full text. FWIW, I haven’t seen it deprecated anywhere — the last time apple used that word in their release notes was OS 10.4, so before applescript 2. I don’t get errors using it and don’t have to think about them so….

My too!

I have several saved in Script Debuggers Text Substitutions (each uses AppleScript’s):

tid
tid,
tid/
tid:
tid;
tidr
tidt

1 Like