Referencing the current frontmost application window

Hi,

I am trying to write a script which references the current frontmost application window from a script on the Scripts menu. The trouble is when I do, the application “Script Events” becomes the front application, and I am unable to find a script which references my original app, which has now been relegated to second place!

I know it would be easy to reference my app by name ( e.g. tell app “Safari”) , but I need the script to work generically for any frontmost app I am working in.

I suppose I want to re-activate my app to be frontmost again. But how exactly?

Thanks!

Hello,

First of all, I have to confess two things: 1.) I am a newbie scripter myself, and 2.) I don’t fully understand what it is that you wish to do ( also you should probably state what version of Script Editor and OS/X you are currently using). Secondly, are you familiar with what the keystroke combinations Command-tab & and Control-d (assuming you have activated them in System Prefs.) can do for you in terms of application switching? I apologize that I can’t offer you more than the above at the moment and hope that this helps.

Sincerely,

Variable as the shade

Variable,

Thanks for replying. I’m using 10.3.2 and the latest Panther Script Editor.

I do know about these keys, but don’t use them much. I figured that, if I didn’t find a more elegant route, I would have to simulate these keystrokes using “System Events” - but using UI scripting always feels like a bit of a cheat!

Here’s the problem again. I’m in an active window, and I want to run a script that starts by getting the name of the app which owns this active window. But when I start my script this target app is now no longer the frontmost app, because now another process has taken over to run the script - namely “System Events”, (or “Script Editor” if I run the script from the editor window). I need to be able to reference this previous app in my code, but I haven’t found a way to.

This has been covered many times before on the board. A little searching would have turned up these threads (as well as others):

http://bbs.applescript.net/viewtopic.php?t=5874
http://bbs.applescript.net/viewtopic.php?t=4524

Jon

Jon,

First of all, many many thanks for the links - exactly what I wanted.

Secondly, as I stated, I am a newbie to scripting (hence the clearly flagged title) - but not to the etiquette and methods of forums. I have worked extremely hard not to bother more experienced users with this simple problem until I felt that I really needed help. I searched in many other places before coming to Macscripter. It is remarkable to me that such a basic element of scripting is not more widely documented.

Thirdly, I searched extremely hard on this forum for a post that would address this problem. I’m afraid that I didn’t find anything, hence my post. As you will know, these things are often a case of forming a query in a site search engine that describes exactly what the problem is in a manner of speech an earlier poster may have put it. This is not always easy and even a random search gave me no results.

Lastly, as a new macscripter user making a first post, I cannot be privy to the knowledge that comes from long experience of using this forum. Repeated posts, however tedious to the expert, are still new to somebody. (Note that the previous responder to this query was also not aware that this problem had been asked before) I am a very experienced user on various other forums, and always try to bear this in mind.

One man’s voyage of discovery is, I’m afraid, another man’s source of irritation. That’s why I place the word “newbie” in the title - so that those offended can choose to ignore it.

Many thanks.

G’day

Dammit the links posted above are redundant. I’ve got the same dilemms, so if anyone would care to post an answer to the top question, I’d be grateful.

Santa

maybe it works with a little trick.
In AppleScript’s own dictionary there is an “path to frontmost application” command.

set thePath to path to frontmost application as Unicode text
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set frontmost_app to text item -2 of thePath
set AppleScript's text item delimiters to oldDelims

frontmost_app contains the name of the frontmost application

[EDIT] or much easier:

set frontmost_app to name of (info for (path to frontmost application))

Model: G5 dual 2,5 GHz
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Jon8, above provided some tantalizing links to what appears to have been a solution to the vexing problem of determining the frontmost running application (the one most likely running the frontmost window on your screen). Unfortunately, Jon8’s links don’t work (as of 6-21-07). Therefore, I thought it might be helpful to show what I recall was the method to this useful task:


tell application "System Events"
       set visible of (first application process whose frontmost is true) to false
	set Name_App to item 1 of (get name of processes whose frontmost is true)
       activate
	display dialog Name_App
end tell

What this script does is to make your AppleScript window disapear (not close) and get the name of the front running process (application) running after the AppleScript. Note that your AppleScript will reapear if you activate it by clicking on the Script Editor icon.

Thank you - I was wracking my brain to recall that, given that Jonn8 hasn’t been around for a while.

Hello!

I have tinkered the code above a little, and this works for me, and that is just so great! :slight_smile:
Now I can which process was the previous active one, i think this is a little bit better than ui scripting, not much, but a little!


-- http://www.macscripter.net/viewtopic.php?pid=25133#p25133

tell application "System Events"
	tell (first application process whose frontmost is true)
		set visible of it to false
		set active_name to name of it
	end tell
	set Name_App to item 1 of (get name of processes whose frontmost is true and visible is true)
end tell
tell application active_name
	activate
	display dialog "previous active app: " & Name_App
end tell

Hello

I’m puzzled.

Why the script doesn’t behave the same if I replace the instruction

	set visible of it to false

by

	set its visible to false

In this script borrowed from ApplescriptLanguageGuide « of it » and « its » behave the same.


tell application "Finder"
	it --result: application "Finder" (target of tell statement)
	set myFolder to path to home folder
	--result: alias "Leopard:Users:myUser:"
	files in myFolder --result: a list of Finder document files
	files in myFolder where name of it contains "AppleScript"
	(* result: document file "AppleScriptLG.pdf" of folder "myUser"
        of folder "Users" of startup disk of application "Finder"}*)
	
	files in myFolder where its name contains "AppleScript" -- same result
	
	files in myFolder where name contains "AppleScript" -- same result
	every file in myFolder whose name contains "AppleScript" -- same result
	every file in myFolder where name of it contains "AppleScript"
	-- same result
end tell

Yvan KOENIG (VALLAURIS, France) lundi 13 août 2012 17:12:25

I am not the right person to answer, but personally, I tend to use of it, instead of its at least when concious, like the same way I use get, probably a lot more than I should! :slight_smile:

For all I know, it is a System Events quirk.

Thank you

I’m hitting a wall with the same kind of instruction.
As I’m curious, I tried :


tell application "Finder"
	every folder of folder "Macintosh HD:usr:share:locale:" where name of it contains ".UTF8"
end tell

It returns {}

Yvan KOENIG (VALLAURIS, France) mercredi 15 août 2012 16:57:58

Hi, Yvan.

Try ‘. whose name contains “.UTF-8”’. :slight_smile:

Hello Nigel

Thank

What fool I am ?
The problem wasn’t the instruction, it was the searched string.
I dropped a dash and of course the code found nothing.

Yvan KOENIG (VALLAURIS, France) mercredi 15 août 2012 21:06:30

Hello.

This is a corrected version of the script I posted in post # 10, the getting of setting of application process properties are reversed here, so that it works as anticipated. I am not sure if it is due to some new behaviour of Mavericks, but now at least, this is the way to do it. :slight_smile:

-- revised 2013.11.23
-- http://www.macscripter.net/viewtopic.php?pid=25133#p25133

tell application "System Events"
	tell (first application process whose frontmost is true)
		set active_name to name of it
		set visible of it to false
	end tell
	set Name_App to item 1 of (get name of processes whose frontmost is true and visible is true)
end tell
tell application active_name
	activate
	display dialog "previous active app: " & Name_App
end tell

Hi McUsr,

In Mavericks, when I run this script:

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

I get “Safari”.

Hello.

I don’t think you need to use System Events for that from Leopard (10.5) onwards:

set nm to name of application (path to frontmost application as text)

By the way, I have just discovered, that the creator typ of an application, is what you really use for application id, and not bundle identifier. At least is is most robust.

if you call an application in a loop, then using application id, instead of application (name), should save the execution, of at least 1000 lines of C code per call. :slight_smile: (You short circuit the work of getting the path to the application by launchd then.)

Except that it’s deprecated, and lots of apps don’t have a creator type.

Which saves how much time exactly?

FWIW, I think this is a particularly bad piece of advice.

Well, it actually worked, telling application id “bundleidentifier” failed.

You may not notice the time time, but the truth is, is that the operating system may be relieved of some work, and that is allways a good thing, I wrote in a loop, and I don’t see the any point of having the operating system perform a really uneccessary action for every iteration of the loop.