I am trying to write a script that uses System Events to display a dialog which warns a user to wait while other actions continue in the background, by using this technique:
do shell script "osascript -e 'tell application \"System Events\" to activate'"
do shell script "osascript -e 'tell application \"System Events\" to display dialog \"Large files may take a long time to convert.\" & return & return & \"Please wait.\" buttons {\"Conversion in process.\"} with title \"My File Converter\" giving up after 10' &> /dev/null &"
The string " &> /dev/null &" allows execution of the next line of the Applescript to proceed immediately.
Here is my question:
The dialog created by the shell script has the dialog “giving up after 10” (or some other number). But I would prefer to close the dialog as soon as the background process is complete, at a later point in my Applescript.
I can see that System Events starts a new thread when it displays my dialog. Is there any way to determine the identity of that thread, put it into a variable, and, later, tell System Events to terminate that thread?
Many thanks for any help with this.
EDIT: replaced original post with a simpler technique
That would be quiting the process that shows the dialog, in this case system events.
Well the problem is not solved by killing a thread. When an Application starts a new window running modal a new event loop will be started in a new thread and the main event loop will pause (therefore the rest of the interface feels unresponsive). If you find a way to eliminate this thread I doubt that it would trigger the main event loop to run again. The main event loop is triggered by code from the thread and when killing it, it won’t get triggered.
You can kill system events or another processes that show the dialog like applescript runner (they both support applescript, are UI agents and NSSuddenTermination). It will remove the dialog.
Thank you for that very clear explanation. I thought I should not kill “System Events” because I thought it might be running something else at the same time. But it seems that I can kill the process without any bad effects.
I didn’t know about “Applescript Runner,” which works equally well. Thanks to your help, I now have my script working smoothly. Before this, I had been using TextEdit to display a message, and it was ugly and time-consuming and inefficient.