Getting the Current Application

I’ve written a script to reorganize windows, but I want it to be applicable for all windows of whatever application owns the front window. For example, if the front window belongs to BBEdit, I’d like to be able to run the script from the menu bar and have all BBEdit windows resized/repositioned.

In order to make the script applicable to more than one app, I have to be able to find out which application owns the current/front window. Anyone know how to do that?

TIA.

tell application "System Events"
	set myVis to the name of processes whose frontmost is true
end tell

Result
{“Script Editor”}

Or, quickest:

set AppleScript's text item delimiters to ":"
set appname to text item -1 of (path to frontmost application as text)
set AppleScript's text item delimiters to {""}

:oops:

Thanks for the tips. Both solutions return the name of the current app, but for some reason Applescript chokes on it. When I tried:

tell application myVis

I got:

tell application “System Events”
get name of every process whose frontmost = true
(Script Editor)
→ Can’t get application {“Script Editor”} of «script».

When I tried putting quotes around the process name, and ran the script from script editor, the dialog box to select the application came up. Selecting “Script Editor” from the list cause the script to continue, with these results in the Event Log:

tell current application
choose application as file specification with prompt “Where is “Script Editor”?”
activate
count every window of current application
end tell

Here’s the code. Using “appname” instead of myVis gives the same results.

tell application “System Events”
set myVis to the name of processes whose frontmost is true
log (myVis)
set myVis to “”" & myVis & “”"
end tell
tell application myVis
try
activate
set s to count of windows
set delta to 0
set {b1, b2, b3, b4} to the bounds of window 1
repeat with i from 1 to s
if the name of window i is not “Unix Script Output” then
set bounds of text window i to {b1 + delta, b2 + delta, b3 + delta,b4 + delta}
set delta to delta + 20
else
set bounds of window i to {9, 718, 473, 853}
end if
select window i
end repeat
end try
end tell

So now I know another wrong way to do it. Any insight on a RIGHT way?

TIA.

K.

P.S. How do I turn on the html for this list? The flag in the lower left says it’s off, but when I check my profile, it says to allow HTML. For now I can only apologize for the really UGLY text. Sorry.

since the applescript will be the frontmost app you will always get that app running as the frontmost process

you can get a list of processes that are not frontmost and then parse that for an app


tell application "System Events"
	set myVis to the name of every process whose frontmost is not true
end tell

this will return a list

{“loginwindow”, “Dock”, “SystemUIServer”, “Finder”, “Mail”, “TextEdit”, “iChat”, “Navigator”, “System Events”, “FileMaker Developer”, “Image Capture Extension”, “Console”}

then you can search that list to see if an app matches what you want and activate it

Actually, your original solution worked. The name of the correct application was returned. But when the script tried to activate the app, the app selection dialog came up.

When I tried it with BBEdit, it actually crashed BBEdit. (The error was reproducible.) But when I put in a dialog box to report the appname captured by your solution, the text string was correct.

K.

this works just fine


set myVis to "Mail" as string


tell application myVis
	activate
end tell


You don’t need quote the app name, because it is already a string.

set myVis to the name of processes whose frontmost is true
--> "Script Editor"
set myVis to """ & myVis & """
--> ""Script Editor""

In fact, quotes won’t coerce no value to string, but simply add a graphic quote.

""" & 2
--> ""2"

Concatenate class string & class integer, coerces to string, and class string is not some text within quotes. Quotes are a graphical representation of this class, but not part of it… :smiley:
If you see a string in a result window, string is “some text within quotes”; if you see a string in a log window, string is “some text within long comment flags” (* and *) But both values are the same values.
Oh! :!: If you use system events with “set myVis to the name of processes whose frontmost is true” you need coerce it to string, because you receive a list of processes whose frontmost is true → {“Script Editor”}. You can re-syntax it to “set myVis to name of first process whose frontmost is true”, which won’t return a list, but a string.
Oh! :!: Both “Finder” & “System Events” will return class Unicode text, but this is not relevant if you’re going to use it in a simple “tell app myVis…”

HTML is turned off for the entire BBS so your profile setting has been overruled. :wink:

When posting AppleScript code, you can use the formatting tools provided above the editing window to add colored, bold or italicized text. To retain indentation in scripts, and to allow others to spot code easily, wrap it in the ‘code’ tags provided by the BBS. The code tags have somewhat the same behavior as ‘pre’ tags. Check out this BBCode link for more info.

Example of text wrapped in ‘code’ tags:

tell application "Finder"
	set x to "Here's what it looks like."
end tell

While it isn’t colorful and fancy, I find that it makes finding and copying scripts much easier. :slight_smile:

Hi everybody,

Are you saying that they’ve changed things and you can now get the last frontmost process. Suppose you open up 3 apps in a row. Then bring the second one to the front. Now try to get the last frontmost app. Do you get the second one? I’ll be testing this out in OSX but, I am skeptical that they changed it so that the sytem maintains a frontmost app list.

Later,
Kel.

No, no, no…
I think that Finder (or system events), in this line of code:

set myVis to the name of processes whose frontmost is true
  1. Get a list of running processes
    → {“…”,“…”,“…”}
  2. Delete process from list if frontmost is not true
  3. Return result
    → {“…”}So:
set myVis to name of first process whose frontmost is true

will return the same result than

set myVis to name of last process whose frontmost is true

WAIT!!! :shock:

set myVis to name of third process whose frontmost is true

returns the same result! A bug?
Anyway, “name of processes” will return a list of “unicode text”, and “whose frontmost is true” will filter this list, depending on its “frontmost” property, returning a reference to this item.
Perhaps this is why OS 9 returns an empty list when requesting “name of third process whose frontmost is true”… :?: