Tuesday, October 27, 2020

#26 2013-11-23 08:34:37 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: Referencing the current frontmost application window

DJ Bazzie Wazzie wrote:

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.


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#27 2013-11-23 08:37:43 pm

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: Referencing the current frontmost application window

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:

Applescript:

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


Os 10.10.3
Mbp

Offline

 

#28 2013-11-23 08:39:36 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Referencing the current frontmost application window

Shane Stanley wrote:

I find it hard to believe that in a repeat loop, AS would resolve it again each time through the loop.


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.

Offline

 

#29 2013-11-23 08:42:11 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Referencing the current frontmost application window

kel1 wrote:

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:


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.

Offline

 

#30 2013-11-23 08:50:58 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Referencing the current frontmost application window

Shane Stanley wrote:
DJ Bazzie Wazzie wrote:

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.


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.


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. 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.

Offline

 

#31 2013-11-24 12:55:25 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: Referencing the current frontmost application window

McUsrII wrote:

this is a run time environment, so I am rather thinking it will execute the find app call every time


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

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#32 2013-11-24 04:25:55 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Referencing the current frontmost application window

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. smile

Offline

 

#33 2013-11-24 05:30:32 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: Referencing the current frontmost application window

McUsrII wrote:

What I meant is that I think it will try to resolve the name at run time


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#34 2013-11-24 05:43:06 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Referencing the current frontmost application window

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. 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. smile

Last edited by McUsrII (2013-11-24 05:55:20 am)

Offline

 

#35 2013-11-24 06:00:48 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Referencing the current frontmost application window

The weather here suggests cocoa instead of a milkshake anyway. smile

Offline

 

#36 2013-11-24 06:21:19 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Referencing the current frontmost application window

Shane Stanley wrote:
McUsrII wrote:

this is a run time environment, so I am rather thinking it will execute the find app call every time


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

Applescript:

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.


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.

Offline

 

#37 2013-11-24 11:16:45 am

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Referencing the current frontmost application window

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. 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.
smile

Offline

 

#38 2013-11-24 11:29:48 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Referencing the current frontmost application window

McUsrII wrote:

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.)


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.

Offline

 

#39 2013-11-24 01:38:52 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Referencing the current frontmost application window

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

Now it is Sunday!

Have a nice one! smile

Offline

 

#40 2013-11-24 05:18:17 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: Referencing the current frontmost application window

DJ Bazzie Wazzie wrote:

When you do that the application name is just changed in your script.


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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#41 2013-11-24 05:58:02 pm

McUsrII
Member
Registered: 2012-11-21
Posts: 3046
Website

Re: Referencing the current frontmost application window

Shane Stanley wrote:
DJ Bazzie Wazzie wrote:

When you do that the application name is just changed in your script.


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. smile

Offline

 

#42 2013-11-24 06:14:02 pm

kel1
Member
Registered: 2013-01-11
Posts: 2188

Re: Referencing the current frontmost application window

Personally, I'd use the application file id. smile


Os 10.10.3
Mbp

Offline

 

#43 2013-11-25 06:03:21 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Referencing the current frontmost application window

Shane Stanley wrote:
DJ Bazzie Wazzie wrote:

When you do that the application name is just changed in your script.


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.


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.

Offline

 

#44 2013-11-25 06:43:31 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: Referencing the current frontmost application window

DJ Bazzie Wazzie wrote:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)