fastest way to get the name of the frontmost application?

I’m always getting the name of the frontmost application in my scripts… so what’s the fastest way to do it? Here’s my best effort, can you beat it?
Note: the speed test I’m using can be seen in post #4 here.

set frontApp to my getFrontApp()
on getFrontApp()
	set AppleScript's text item delimiters to ":"
	set appname to text item -2 of (path to frontmost application as text)
	set AppleScript's text item delimiters to ""
	set frontApp to text items 1 thru -5 of appname as Unicode text
	return frontApp
end getFrontApp

For example: my old method of getting the frontApp was:

set frontApp to displayed name of (info for (path to frontmost application))

When I tested the 2 methods, the first method is 10 times faster.

It often seems that the direct approach is the fastest, even when the code is longer.

What’s really interesting is that I thought this might be quicker because it eliminates a variable. It’s actually 2% slower.

on getFrontApp1()
	set AppleScript's text item delimiters to ":"
	set appname to text item -2 of (path to frontmost application as text)
	set AppleScript's text item delimiters to ""
	return text items 1 thru -5 of appname as Unicode text
end getFrontApp1

And something like this takes 38 times as long:

on getFrontApp1()
	tell (info for (path to frontmost application)) to return name's text 1 thru ((my (offset of ("." & name extension) in name)) - 1)
end getFrontApp1

The old motto KISS [keep it simple, stupid] rings true again! :wink:

I’m asking for the fastest way, not the slowest! :smiley:

I haven’t tested for speed, but I’ve been using this:

tell application "System Events" to set frontApp to name of (processes whose frontmost is true)

Nope! 31 times slower. :slight_smile:
Plus your code actually returns a list value instead of a string, so then you have to take our result and convert it to a string which adds another operation. A better way to get a string directly using your method would be…

tell application "System Events" to set frontApp to name of (first process whose frontmost is true)

But that’s just as slow.

I knew that, and actually do think that a straight-forward parsing of the path to the frontmost application is the fastest. I tried several others and just for fun posted the slowest of those I tried. :cool:

on a related note, how would one fetch a list of all running applications? (preferably in order from front to back)

There’s no way to get the processes in an ordered fashion. You can only get the “front/first process”, but none others. You can hide the first process, and then the next process becomes the “first” process (which could be considered the second process). But here’s how to get all the processes, but it’s not an exhaustive list because it doesn’t include system level processes. You’d have to use unix commands if you wanted more.

tell application "System Events" to set allProcesses to name of every process

This should return all processes for the current user:

do shell script "/bin/ps -xco command"
set processList to paragraphs 2 thru -1 of result

This should return all processes for all users:

do shell script "/bin/ps -Axco command"
set processList to paragraphs 2 thru -1 of result

It needs to be noted that an Application’s name can be different from its display name. Quicksilver is a good example.

Can I ask what you use the name for?

thanks, for the help.

I need the list of apps, for a script im writing to put a server to sleep after a certain time of inactivity but only if certain apps aren’t running, eg azurius (for some reason it ignores the usual sleep timer in the energy saver preference pane)

:stuck_out_tongue:

And from its process name (eg. Firefox). Also, that a ‘displayed name’ depends on the user’s preferences for displaying name extensions or not in the Finder.

Improving the robustness of regulus’s TIDs script gets a very slight speed increase too on my machine: :slight_smile:

set frontApp to my getFrontApp()
on getFrontApp()
	set colon to ":" as Unicode text
	set dot to "." as Unicode text
	set appPath to (path to frontmost application as Unicode text)
	considering case
		if (appPath ends with colon) then
			set n to -2
		else
			set n to -1
		end if
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to colon
		set appname to text item n of appPath
		if (appname contains dot) then
			set AppleScript's text item delimiters to dot
			set appname to text 1 thru text item -2 of appname
		end if
		set AppleScript's text item delimiters to astid
	end considering
	
	return appname
end getFrontApp

Holy tricky scripting Batman! He did it! I knew one of you would find something. :smiley:

Wow, I guess the big difference was in my SLOW line of…
set frontApp to text items 1 thru -5 of appname as Unicode text

I see now that it should have been…
set frontApp to text 1 thru -5 of appname

I think that’s what made your script a little faster??? Congrats Nigel!

I like to tell the front application to display dialogs so they don’t popup in the background. Another reason is when you launch a script application it becomes the front application, so I can no longer tell the real front application to do anything. So in this case I get the name of the frontApp, comapre it to the name of the script, and if the frontApp is the name of the script application then I hide it… then I can tell the real frontApp to do something… like copy/paste text etc.

Plus I have 2 “stay-open” applications that continually get the name of the front app. They do things when the front app becomes a certain app. That’s why I was looking for the fastest code, because they get the front app every few seconds so I wanted to lessen the burden of their processing time when they’re just idle waiting to do something.

So yea, I will use this a lot!