1

I want to have an alert / dialog box to fire up periodically in foreground.

I tried a combination of AppleScript and launchd and managed to have the script (actually converted to a app) fired up periodically via launchd. However, the alert box does not show up in front of all other windows (rather totally hidden until I tab to it – though it does get started via launchd). How can I get it in front?

My script is as follows:

on run
    activate me --> tried to get alert in foreground
                --> didn't help, though
    display alert "Should show up in foreground..."
end run

My launch.plist is (in /Users/bernhard/Library/LaunchAgents/):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>bernhard.sitstraight.plist</string>

    <key>Program</key>
    <string>/Users/bernhard/programming/periodic.app/Contents/MacOS/applet</string>

    <key>RunAtLoad</key>  
    <true/>

    <key>StartInterval</key>
    <integer>1200</integer>
</dict>
</plist>
1

Try using open instead of launching the program directly. open more closely matches double-clicking to launch the AppleScript application.

open your application

To do this, replace the Program key value pair in your launchd job ticket with a ProgramArguments array to provide the path to your AppleScript application:

<key>ProgramArguments</key>
<array>
    <string>/usr/bin/open</string>
    <string>/Users/bernhard/programming/periodic.app</string>
</array>

By default, the open command attempts to bring the launched application to foreground.

|improve this answer|||||
  • Thanks! That's almost the solution. However, the ProgramArguments need some program name (passed as argv[0]) as first argument (see launchd.info -> Configuration). With this modification it works :) – Bernhard Röttgers May 25 '15 at 12:18
  • Great to hear the open approach worked. I have updated the answer. – Graham Miln May 25 '15 at 13:13
0

Your code seems to work ok on my 10.6.8 (Snow Leopard), though I didn't launch it from launchd.

I did something very similar to monitor my battery. The script automatically hibernates my system after giving a 60s count down using a 'flashing' dialog box.

Because, like you, the dialog box would sometimes get buried, I gave the dialog box a timeout and would re-display it.

Here is the relevant code applescript code from my bash schell script...

   userResp="`/usr/bin/osascript 2>&1 <<-EOF
    tell application "System Events"
        activate
        display dialog \
            "Battery below min safe level of ${minBattLvl}%" \
            & "\\n\\nForcing hibernation in: ${cntDown} seconds." \
            as text buttons {"Cancel","$btntext"} \
            default button "$btntext" \
            with icon caution \
            with title "WARNING: Battery Monitor" \
            giving up after $dlgTimeout
    end tell
EOF`"
|improve this answer|||||
  • My problem is that the box does not show up in front in the first place (actually not at all until I tab to it). Without launching it via launchd it works fine for me as well. I guess it has something to do with the combination of firing it up with launchd and the the program being a app... – Bernhard Röttgers May 25 '15 at 11:21
  • Gotcha. Mine was intermittent and sometime accidental - by clicking on another window at the moment the dialog appeared :( . I wonder though if using System Events to display the dialog box makes a difference. In any case I'm happy you found a working solution. – Vic May 27 '15 at 7:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .