Referencing the current frontmost application window

From where I understand it is not the version number but the AppleEvent manager will send it’s event to the first running process. To get running processes by job or by name from the process database (manager) is imo equally fast.

If the process isn’t running then it uses launch services’s meta database. In this database (you can see it when you dump it) the bundle id, name or creator code have all the same multiple values. In here there is still not another record having more advantage of the other. So when I look for org.mozilla.firefox for example, I will see all the version loaded from teh downloaded DMG files and installed versions, if I look by bundle id or name.

Creator code is a no go because it’s old legacy code coming from Mac OS systems. It’s an last resort

While that is true, the two forms appear to take a different code route – when an app can’t be found, one generates the “Find app” dialog, while the other generates an error you can trap. One could be quicker than the other.

Even so, in a running app (that is, outside a script editor), I’d be very surprised if AS resolved it more than once for each app used. I find it hard to believe that in a repeat loop, AS would resolve it again each time through the loop.

One thing about using the name of a process is that some processes don’t use the same name as it’s application. That’s why I changed my process quitter to bundle identifier:

tell application "System Events" to launch
set my_name to my name
tell application "System Events"
	set visible of every process to true
	delay 0.5
	set app_ids to (bundle identifier of every process whose visible is true and name is not "Finder" and name is not my_name)
end tell
repeat with app_id in app_ids
	tell application id app_id to quit
end repeat

It’s not the running instance of AppleScript that communicates with the Application directly. It’s communication through the AppleEvent manager. That means the AppleEvent manager can’t predict how many times it needs to send an event to the application. Still I’m sure, much like you, that the AppleEvent manager has an clever way to resolve this.

Name is related to the name field of the bundle info inside the bundle, file name has nothing to do with it. The same name is also used by LS.

I just wanted to add that I omitted to my first recommendation, that this technique should not be used with third party apps, but with Finder, TextEdit and such you are really on firm ground for applying it.

If I got a dollar for every time I thought better of the AppleScript by now, I am sure I could have bought two milkshakes in Norway. :slight_smile: I mean that now, it would be natural if this stuff was cached some place wouldn’t it, if if it was looked up, but thing is, this is a run time environment, so I am rather thinking it will execute the find app call every time, since the user in theory may change name of it in between calls.

I note there is no emoticon for shrugs shoulders.

Easy enough to check. Write a script app like this:

repeat 1000 times
tell application id "blah.blah.blah"
-- some code in here
end tell
end repeat

where “blah.blah.blah” is the id. Then delete the app, but put a copy on an external drive.

Now run the script app, and when the “Find the app” dialog comes up, choose the copy on the external drive. If the dialog comes up another 999 times, you earned a milkshake. If it doesn’t, it’s being cached.

Hehehe.

By find app, I didn’t mean to find the app that way Shane.

What I meant is that I think it will try to resolve the name at run time, and that too is also easy to test.
If I have a loop of some kind, calling a handler that calls application “Finder” and times that one versurs one calling application id “MACS”, then there should be a measureable difference if my hypothesis are right.

I’ll be back with such results later on. :slight_smile:

I understood. But the fact that the dialog only appears once, even though you might have several tell statements to the same app, suggests that the result of the first call is cached somewhere and used for the others – if not, it would begin the search again each time.

Hello.

I noticed that point too, but then I thought it would make a difference between not finding any app, and finding some.

I actually did test this, and I didn’t get any results indicating that I was right.

I’ll do one more test, and that is running it in debug mode in Script Debugger, if I am right, then in debug mode, it should be measurable. :slight_smile:

Edit

I can’t conclude with anything when running the tests in ScriptDebugger either. But at least there is nothing that says in my results that the opposite is true.

I tested this by having TextEdit make 1000 paragraphs containing some text the iteration number an a line feed.
(I did this way, so other AppleEvents should influence as litte as possible on the outcome, But I had to do something in the tell block.)

There is nothing here indicating that I am right, but I am still going to use the creator type, when I am iterating, because at least it doesn’t hurt, and maybe there are some small gains in it. I have a hard time believing there is a binding towards an app with a tell appname block, I swear I have read somewhere in Apple’s docs that using the bundle identifer / creator type, bypasses some operations by launchd to figure out which app to addres.

But then again, if it is so, it doesn’ seem to have any real practical value. It is more of an attitude. :slight_smile:

The weather here suggests cocoa instead of a milkshake anyway. :slight_smile:

When you do that the application name is just changed in your script. The next call will be the correct application name, also after rebooting you system. Nothing cached here, just modifying your script at run time.

Hello.

I agree to what happens of the script side of this, but I have lived in the belief, that two different apple events are sent if you use the id, or the name of the application. I also belive that when the target is resolved, by the eventmanager, or whatever, then two different things will happen according to which event was sent.

if you sent the id, then a simple lookup in the launchd db would be performed, but if you used the name, then launchd would have a look for the most plausible target. (Finding the app with the highest version number for instance.)

That’s what I believed at least, and I am actually (stubborn as I am!) believing that until I read something contrary in Apples doc, or, looks at the events that are sent in the two cases.

But I agree on one thing, there is no measurable difference in this, at least not until a name gets changed on an app during runtime. :slight_smile: And I have a hard time believing that you would be able to measure that as well.

There is also that, that if you avoid code execution, then it is more efficient, even if you can’t measure it.
:slight_smile:

My point is that when you lookup in launchd there can still be multiple values. For instance, when I’m running quark 6, 7, 8 or 9 on my machine, they all have the same bundle ID. The question, or similarity, that pops up in my mind, why does bundle id as name of process both bind to the highest running version. So when I have both quark 7 and 8 running, in both situations it won’t launch 9 but pick quark 8. That said it means both use launchd (Launch Services), one is looked up by job (bundle id) while the other is looked up by name.

for the record: Unix process name is something else.

I’ll post you if I find something on this in the Docs.

Now it is Sunday!

Have a nice one! :slight_smile:

I think you’re missing the point, which is that after AS resolves the reference to the application once, it doesn’t do it again for the next tell block it comes across. This is using id, not name.

And it will of course work the same way if the tell block is inside a library, since it is also resolved once and for all during compilation.

I have to bother myself, to look at the apple events now, but I’m reluctant. :slight_smile:

Personally, I’d use the application file id. :slight_smile:

I think you’re right Shane, I was focusing on the wrong subject and forgot the “id” part. Still, apart from the differences in AppleScript, the name of application is just an info.plist entry like an bundle id. So to me, in general, there would be no difference in performance when you ask for an process by name or bundle from launchd (not when I’m in C/Objective-C). AppleScript, again, behaves against all odds. My mistake.

I suspect the explanation is that the name-based references pre-date launched, and are still using pre-launchd APIs, whereas id-based references were introduced after launchd, and use the newer, but perhaps fractionally slower, API. At the same time they introduced the ability to skip the “find the app” dialog.