Saturday, October 25, 2014

#1 2012-08-08 07:40:39 am

flex20
Member
From: Sydney, Australia
Registered: 2010-08-24
Posts: 161

Strange error from 'do shell script' in a try block within a tell one

Hi guys,
I stumbled over a strange error in one of my scripts.  Here is a simple script as an example:

Applescript:


tell application "Finder"
   try
       do shell script "afplay '/Applications/Sound Utils/Sound Done!.aiff'"
   on error
       do shell script "afplay '/System/Library/Sounds/Purr.aiff'"
   end try
end tell

The script works without any obvious error but, here are the script replies in AS Editor:

Applescript:


tell application "Finder"
   do shell script "afplay '/Applications/Sound Utils/Sound Done!.aiff'"
       --> error number -10004 -- A privilege violation occurred.
end tell
tell current application
   do shell script "afplay '/Applications/Sound Utils/Sound Done!.aiff'"
       --> ""
end tell
Result:
""

I checked many scripts with various 'do shell script' commands placed in a try block nested within a tell block.  All scripts work without problems but, all show that error in the replies.  Questions:
* What is the meaning of the error "-10004 A privilege violation occurred"?  For the purpose of this test, I gave the folder and sound file full privileges to everyone.
* Why doesn't it have any effect on the actual working of the scripts?
* If it is an error, why doesn't "on error" take over?
* If the try block is taken out of the tell block, then the error disappears.  Is there a rule that try blocks shouldn't be nested within tell blocks?  Here it is by itself:

Applescript:


try
   do shell script "afplay '/Applications/Sound Utils/Sound Done!.aiff'"
on error
   do shell script "afplay '/System/Library/Sounds/Purr.aiff'"
end try

and the replies:

Applescript:


tell current application
   do shell script "afplay '/Applications/Sound Utils/Sound Done!.aiff'"
       --> ""
end tell
Result:
""

Can somebody please explain what's going on here?

Best Regards,
Chris

Offline

 

#2 2012-08-08 08:44:55 am

mouramartins
Member
From: Algarve
Registered: 2010-11-21
Posts: 119

Re: Strange error from 'do shell script' in a try block within a tell one

http://developer.apple.com/library/mac/ … -10_6.html

For security reasons, most scripting addition commands now return a “privilege violation” error when sent between application processes. In order to preserve compatibility with existing scripts, AppleScript redirects most of these commands to the current application, that is, the process running the script. If a script sends events to a remote computer via EPPC (“Remote Apple Events”), AppleScript may redirect them to the System Events process on the target machine. AppleScript Editor’s Event Log will show when this redirection happens: you will see the event sent first to the original target process, return an error, and then sent again, often to the current application.

Offline

 

#3 2012-08-08 08:53:43 am

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

I think mouramartins found the correct snippet, which I find quite worrysome for my own purposes.

Why the error isn't trapped in the on error clause. It is however considered bad practice to do stuff in a tell block, that isn't absolutely necessary to do there. In your case, just move the block outside the tell block of finder. I think mouramartins nailed it!

Which is unfortunate for me big_smile as I really want to leverage on scripting additons for speed gains, above applications tell blocks! I'm quite anxious now… smile


Mercurial vcs is a joy to use for scripting.

Filed under: privilege violation

Offline

 

#4 2012-08-08 09:26:34 am

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

Re: Strange error from 'do shell script' in a try block within a tell one

flex20 wrote:

Can somebody please explain what's going on here?

I can try to explain. When a do shell script is executed by the Finder nothing really can go wrong because the Finder has the same (console) user as the script itself. But what happens if I use an application that runs as root? For instance application "Bit Slicer" (which I use for memory lookup) runs always as root and when I do:

Applescript:


tell application "Bit Slicer"
do shell script "whoami"
end tell

The result will be 'root'. That means that my script can bypass Mac OS X/Unix's security and I'm able to do everything I want. So what my script needed to do was looking for an process that runs as root and tell it what to do. One of the things to solve these leaks is just not allowing to do a shell script by another process than itself.

So what you should do is just:

Applescript:


--when you're already in current application you don't need a tell block
do shell script "whoami"
--or when you're inside another application tell block
tell application "Finder"
   tell current application to do shell script "whoami"
end tell


Kind regards

Offline

 

#5 2012-08-08 11:57:51 am

flex20
Member
From: Sydney, Australia
Registered: 2010-08-24
Posts: 161

Re: Strange error from 'do shell script' in a try block within a tell one

mouramartins, Bastiaan,
Thanks, that is quite clear now.

McUsr wrote:

Why the error isn't trapped in the on error clause. It is however considered bad practice to do stuff in a tell block, that isn't absolutely necessary to do there.

I agree generally with "not doing stuff" there.  In my initial example, I actually "did stuff" but just for the purpose of showing that "on error" doesn't trap that particular error.  I still don't know why.

However, most of the times, I like/need to put there more than just a short message for the user.  Usually, I insert a "display dialog" with a more detailed message/explanation and, depending on the context, options to cancel, or do this or that.

Guys,
Regarding a "display dialog" inserted in a try block outside the Finder, do I have to tell the current app to tell the Finder to display the dialog, or put the dialog in a nested tell the Finder block?

Best Regards,
Chris

Offline

 

#6 2012-08-08 12:28:37 pm

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

Re: Strange error from 'do shell script' in a try block within a tell one

flex20 wrote:

Guys,
Regarding a "display dialog" inserted in a try block outside the Finder, do I have to tell the current app to tell the Finder to display the dialog, or put the dialog in a nested tell the Finder block?

Display dialog is an 'odd' command. For normal commands in a tell application block an event is send to the current target application. When the application can't/doesn't want to handle the event it is send to the parent (following the parenting chain). For a display dialog the command is send immediately to the system and a dialog is displayed in the target application context. Similar to commands like activate.


Kind regards

Offline

 

#7 2012-08-08 12:57:36 pm

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

flex20 wrote:

Guys,
Regarding a "display dialog" inserted in a try block outside the Finder, do I have to tell the current app to tell the Finder to display the dialog, or put the dialog in a nested tell the Finder block?

I think the display dialog is to be handled by SystemUIServer so I use a construct like this, I think it also speed up things a little!

Applescript:


tell application "SystemUIServer"
   activate
   display dialog "Testing"
end tell

I use to have try error block in there, but then I set a failed variable to true, and deals with the error outside the context of SystemUIServer, if the script is to quit, etc.

I think such an approach is wise, also for other tell blocks, as to not have events "pending" inside running applications.


Mercurial vcs is a joy to use for scripting.

Filed under: tell

Offline

 

#8 2012-08-08 02:10:20 pm

flex20
Member
From: Sydney, Australia
Registered: 2010-08-24
Posts: 161

Re: Strange error from 'do shell script' in a try block within a tell one

Thanks guys,

McUsr wrote:

I think the display dialog is to be handled by SystemUIServer so I use a construct like this, I think it also speed up things a little!

Applescript:


tell application "SystemUIServer"
   activate
   display dialog "Testing"
end tell

I think such an approach is wise, also for other tell blocks, as to not have events "pending" inside running applications.

I'll use this construct in my current attempt to optimise a longer script for speed.

Regarding speed, could you point me to options to actually time the execution of a script between dialogs precisely.  I haven't done actual measurements yet, other than rough manual timing.

Cheers,
Chris

Offline

 

#9 2012-08-08 02:57:59 pm

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

I don't think that displaying dialogs that way will speed up your script much, but you will direct the events directly to where they'll end up anyway. And you can be pretty sure that the dialog will turn up, whatever space or screen you are in, it will be visible, and rendered fast, since it is SystemUIServer that takes care of such things, and no pending events on the way, in for instance a busy finder, or busy SystemEvents, or whatever app you are using will slow it down.


Look Here! post #1 and post #14 for timing tools.

Edit I see below that there are diverging opinons about this matter, but I'll stick to my way, as I am happy with it. I think I have experienced that the display dialogs can be slowed down, at least when addressed through System Events, or Finder.

Last edited by McUsr (2012-08-08 03:05:16 pm)


Mercurial vcs is a joy to use for scripting.

Filed under: getMilliSec

Offline

 

#10 2012-08-08 02:59:40 pm

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

Re: Strange error from 'do shell script' in a try block within a tell one

flex20 wrote:

McUsr wrote:

I think the display dialog is to be handled by SystemUIServer so I use a construct like this, I think it also speed up things a little!

Applescript:


tell application "SystemUIServer"
   activate
   display dialog "Testing"
end tell

I think such an approach is wise, also for other tell blocks, as to not have events "pending" inside running applications.

I'll use this construct in my current attempt to optimise a longer script for speed.
Regarding speed, could you point me to options to actually time the execution of a script between dialogs precisely.  I haven't done actual measurements yet, other than rough manual timing.

Like i said in my previous post, display dialog doesn't follow the parenting chain like normal events do, so there are no 'events pending'. The event is sent immediately to the window server, not to systemUIServer, and display dialog for current application, finder or any other application shouldn't have any measurable time differences.


Kind regards

Offline

 

#11 2012-08-08 04:04:06 pm

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

DJ Bazzie Wazzie wrote:

Like i said in my previous post, display dialog doesn't follow the parenting chain like normal events do, so there are no 'events pending'. The event is sent immediately to the window server, not to systemUIServer, and display dialog for current application, finder or any other application shouldn't have any measurable time differences.

One thought has struck me: what if SystemUIServer is just another synonym for Window Server?

And I'd really love to see a log of the aetes travelling to the windowserver in both cases, to see if there are any difference! smile

I'll come back to this subject, as I want to perform some other kind of tests, regarding localization, to see what kind of information is displayed, when calling display dialog through different applications.

Last edited by McUsr (2012-08-08 04:07:04 pm)


Mercurial vcs is a joy to use for scripting.

Filed under: Window server

Offline

 

#12 2012-08-08 04:16:25 pm

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

When I tell Finder, SystemUiServer, or System Events to display a dialog, I get the buttons localized in my lingo. When I tell BBEdit to display dialog, then I get it in english. I can hardly interpret that in any other way, than that something more happens, than merely passing it over to the window server.


Mercurial vcs is a joy to use for scripting.

Filed under: Window server

Offline

 

#13 2012-08-08 07:09:47 pm

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

Re: Strange error from 'do shell script' in a try block within a tell one

McUsr wrote:

One thought has struck me: what if SystemUIServer is just another synonym for Window Server?

Window Server = WindowServer process including Quartz Compositor in it
SystemUIServer = the server for the menu at the top right corner of your screen.


Kind regards

Offline

 

#14 2012-08-08 07:20:45 pm

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

I see! Well, I actually read in the book Applescript the missing manual big_smile that routing it through "SystemUIServer" was the fastest way. I have gotten complaints here, about routing the calls to display dialog through System Events.

And as I pointed out, there is a difference in the way the display dialog is localized, when calling it from BBEdit, than in the other cases.

I don't know what to believe really, until someone drags up a reference to some documentation, or shows me a dump of the aete traffic.


Mercurial vcs is a joy to use for scripting.

Filed under: Window server

Offline

 

#15 2012-08-08 07:50:22 pm

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

Re: Strange error from 'do shell script' in a try block within a tell one

McUsr wrote:

Hello!

Hi Again! big_smile

McUsr wrote:

I don't know what to believe really, until someone drags up a reference to some documentation, or shows me a dump of the aete traffic.

There is no magic whatsoever: Show a dialog (with another process than SystemUIServer) and kill SystemUIServer and see what happens;  the dialog remains and top menu will dissappear. Not recommended but look what happens if you kill WindowServer... aqua will die


Kind regards

Offline

 

#16 2012-08-09 03:39:48 am

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

I figured it all out! Before Coffee! Maybe SystemUIServer, is the app with the smallest dictionary for one thing, so the least overhead in resolving a tell block  for a target application for AS is to call SystemUIServer.

That display dialog is implemented in Standard Additons.osax is a fact! But there are data passed along as well, from the current application, as my test with BBEdit showed.

I don't want to kill my window server right now. I take your word for it! But the dialog, is killed when I kill SystemUIServer!

The one thing I seem to disagree with you about at this moment, is that data is passed a long, before calling Standard Additions.Osax directly, avoiding the parent chain. But that was never mentioned really.

Last edited by McUsr (2012-08-09 03:41:19 am)


Mercurial vcs is a joy to use for scripting.

Filed under: Window server

Offline

 

#17 2012-08-09 03:49:37 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10610
Website

Re: Strange error from 'do shell script' in a try block within a tell one

McUsr wrote:

Hello!

Maybe SystemUIServer, is the app with the smallest dictionary for one thing, so the least overhead in resolving a tell block  for a target application for AS is to call SystemUIServer.

Dictionary size doesn't matter at all, because terminology is resolved at compile time.


regards

Stefan

Offline

 

#18 2012-08-09 03:59:15 am

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello Stefan! smile

StefanK wrote:

Dictionary size doesn't matter at all, because terminology is resolved at compile time.

( Unless you are using terms from. But that isn't the question here. )

How sloppy of me! Well Stefan is there any rational reason for having the display dialog in a tell "SystemUIServer" block to invoke the display dialog as fast as possible?

I 'm thinking that maybe the aetes sent to SystemUIServer, either has higher priority, or that SystemUIServer, is running all the time, so it is latent in memory, and faster to call, and even given priority when it comes to memory as it deals with pages of it, in the user interface. That such factors may make it a faster renderer of display dialogs, as the over all process goes quicker with it run time.

Last edited by McUsr (2012-08-09 04:07:08 am)


Mercurial vcs is a joy to use for scripting.

Filed under: Window server

Offline

 

#19 2012-08-09 04:14:40 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10610
Website

Re: Strange error from 'do shell script' in a try block within a tell one

McUsr wrote:

( Unless you are using terms from. But that isn't the question here. )

Not even then. Terminology will only be resolved at runtime with a run script command.

McUsr wrote:

is there any rational reason for having the display dialog in a tell "SystemUIServer" block to invoke the display dialog as fast as possible?

Not for me because the reaction of the user is always much slower wink


regards

Stefan

Offline

 

#20 2012-08-09 04:53:26 am

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

StefanK wrote:

Not even then. Terminology will only be resolved at runtime with a run script command.

I see, At least I believe, that the process being adressed in a tell block, is addressed in some way; that even if no resources of the targeted app is used, ( I am talking about applications targeted by tell blocks only), a process of sending applevents are started. And the time for this communication channel to be established, would be dependent of the process status in real memory (by real memory, I mean active physical memory). I believe SystemUIServer to always be in physical memory, since it is about updating the UI. Therefore I believe SystemUIServer to be the fastest. Being the process that it is fastest to establish contact with, and therefore the fastest to delegate the display dialog request away to Standard Addtions.Osax. Even faster than the current application, as the current application may (theoretically) need to load some pages into real memory first.

Having pondered this, I think this hack, was established some years ago, when having less than a gig of memory was the norm! smile

But, say if the system is bogged down with some serious downloading, and updating spotlight meta data, while the macports ports tree is updated, (rebuilt), under such conditions, I think it to be noticable.


StefanK wrote:

Not for me because the reaction of the user is always much slower wink

True! smile


Mercurial vcs is a joy to use for scripting.

Filed under: Window server

Offline

 

#21 2012-08-09 05:00:13 am

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 10610
Website

Re: Strange error from 'do shell script' in a try block within a tell one

consider that current application is NOT the frontmost application but the current AppleScript runner environment


regards

Stefan

Offline

 

#22 2012-08-09 05:34:47 am

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

StefanK wrote:

consider that current application is NOT the frontmost application but the current AppleScript runner environment

That is really something to consider! To be honest, I couldn't find Apple Script Runner when using the ps utility to check out the priority, but SystemUIServer, has the standard  priority of an application 48, so there is nothing special there. But the process is small! It takes up only 17.2 Mb of physical memory.

As I can't stat AppleScript runner, or doesn't know how to, I'll continue using SystemUIServer

Last edited by McUsr (2012-08-09 05:36:34 am)


Mercurial vcs is a joy to use for scripting.

Filed under: Window server

Offline

 

#23 2012-08-09 05:54:24 am

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

Re: Strange error from 'do shell script' in a try block within a tell one

McUsr wrote:

I don't want to kill my window server right now. I take your word for it! But the dialog, is killed when I kill SystemUIServer!

I said don't show a dialog with systemUIServer. When I show a dialog with the Finder and kill it, the dialog is gone as well. But when I show a dialog in the Finder and kill systemUIServer the dialog stays on my screen and the upper right menu is gone (except for spotlight). The window server (where every application is, must, connected to) notice if an process/application lost connection with it or died. When an application died there is no use still showing objects for it so the window server removes those objects, that's the reason why the dialog is gone. This simple test proofs that dialogs aren't handled by systemUIServer but are handled directly by the window server.


Kind regards

Offline

 

#24 2012-08-09 06:07:24 am

McUsr
Member
From: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Strange error from 'do shell script' in a try block within a tell one

Hello!

I figured out that along the way!

There is some points here though, one of them being, that there are some kind of communication, between the app, and the window server, in the form of aete events.

What's next, I am not sure, or can't explain. But what you have from before inflicts on the speed of the scripting component. (Otherwise there wouldn't be any gain in run script from within a run handler, this is proven to be faster, because then Scripting Addtions takes over the IPC (inter process communication), which it does faster than the original scripting component of an applet).

I want to make a point of that I am not just stubborn here! big_smile

And, there is no real reason for using the SystemUIServer to speed up the display of the display dialog, I believe it to be the context change, (memory wise) to a smaller app, with a smaller footprint, with no pending aetes that makes up any difference, if any, which I really can't stat. Maybe it is a one way communication with the SystemUIServer through aetes But, having said that, I still believe it to make a difference on a machine with a heavy load.

I'm sorry I had to go to this extent, to bring the facts on the table.

I am still going to use this way of displaying dialogs, since ı believe in it! big_smile

Last edited by McUsr (2012-08-09 06:20:01 am)


Mercurial vcs is a joy to use for scripting.

Filed under: Window server

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.030 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)