I may hope compiled C code; machine code. besides that, a good hash table is only a few lines of code.
Application by name or (bundle) id is as different as searching a mail by subject or sender. The search will only be done by different strings on different columns in the database. Depending on the indexing of the database, which with no doubt will be fine, there is no difference between finding an application by it’s name or bundle id.
There is actually a difference as I have understood it. Using the application name, is similar to using the just a command name in a shell script, launchd will resolve the path of the command by searching for it, taking height for verson numbering here and there, which copy of the app is to be addressed by the name etc.
Using bundle id, or creator type, short circuits all that, and can be viewed, as specifying a command with an absolute path in a do shell script.
That is my understanding, and then, when you know the creator type, then you see the advantage if you are iterating some times, if not just for the principle, of not doing unecessary operations.
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.
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. 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.
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.
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 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.
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.
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.
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.
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. 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.
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 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.