Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

classicalken

macrumors newbie
Original poster
Jun 13, 2023
3
1
Boston, MA
Hi. I wrote a little bash script that runs in the background (launched by crontab) which uses osascript's (AppleScript's) 'display dialog' command to throw up a dialog box on my screen. Works great. Trouble is, the dialog box also takes the input focus. I want the dialog box to appear, but I don't want it to steal the input focus. Is this even possible? If not (as I suspect), how about this for a Plan B? Is it possible my background script could ... (1) detect and save the window ID or the app name that currently has input focus, (2) throw up the dialog box, then (3) give input focus back to where it was? If Plan B is possible, how to do (1) and (3)? [Title of this thread assumes Plan B.]

Context: This 'little bash script' is a custom reminder app. The dialog boxes, which remind me of my appointments, etc, have an input field. I can type things like '20m' (or '1900' or 'tue') into this input field, which my app will interpret as 'remind me again in 20 minutes' (or at 7pm or next Tuesday). Now, imagine this. I type a lot. I type fast. So it often happens that I'm typing something and suddenly my keystrokes show up inappropriately in the dialog box's input field. Grrr.

TIA for any help.
 
You want to keep the dialog box until you manually discard it but it should not appear in the front?
Does this help?
Bash:
#!/bin/bash
/usr/bin/osascript -e 'tell application "Finder" to display dialog "This window does NOT steal your focus unless Finder was active." with title "window" giving up after 5'

By using tell application "Finder" instead of "System Events" you only see the dialog box when Finder is the active application. Otherwise the Finder icon in the dock will jump and signal a new event.

To [Plan B], here's how I get the current active application, display something and re-activate the old app sequentially. This is NOT directly applicable for you because you have to discard the dialog box before the old application is re-activated.
AppleScript:
tell application "System Events"
    set appOfInterest to name of application processes whose frontmost is true
    set appOfInterestName to appOfInterest as string
    display dialog appOfInterestName & " was active before." & return & "Will be restored in 2 seconds." with title "window" giving up after 2
end tell
tell application appOfInterestName to activate
 
Last edited:
  • Like
Reactions: classicalken
Hey arw,
Your response was VERY helpful, more helpful than I had hoped for. Thank you!!!
I was trying to avoid using 'tell application "Finder" to display dialog...'. I was thinking that brings the Finder to the fore. Not at all desirable for me. So I was using just 'display dialog...' instead.
Well, I tried your suggestion anyway, and discovered that it does NOT bring the Finder to the fore. It bounces the Finder icon in the dock. At least in my usage scenario, where the applescript is invoked from a .sh launched by crontab.
So this is like Plan C. In this Plan C, which is my new favorite plan, when it's time to display a reminder, I do not see the dialog box immediately. I see instead the bouncing Finder icon. I see this even though my dock is invisible. I like this because it gets my attention in a minimally invasive way. My fast typing is not rudely interrupted. Not interrupted by losing input focus, and ... now here is the Plan C bonus ... also not interrupted by obscuring a chunk of my screen real estate.
Your final thoughts, on Plan B, I also found to be instructive. I suspect they may yet have led to an acceptable Plan B solution. But I will not be pursuing this. As I say, I like Plan C best.
Thanks again!!!
 
  • Like
Reactions: arw
As you see, I am a macrumors newbie. I just joined today. Is there anything I should do to flag this issue as solved?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.