Skip to main content
Question

Problems Running a GUI Script at Login


Forum|alt.badge.img+4

Hello folks,

I'm running into some issues. This came about because I wanted to disable Mail.app's Junk Mail filtering. We filter at the mail server, so we don't need it, and had issues with some users' mail being deleted after they started tweaking with Mail.app's Junk Mail settings.

I have not been able to determine method to disable this feature via 'defaults write com.apple.mail' so I resorted to scripting the GUI in AppleScript.

I've got a script that does the job, but:

When I set it to run at login, I can't get the script to wait long enough for the user to be fully logged in before it executes. If I add delays in the script, the entire login process seems to wait until the delay elapses before continuing, so that doesn't work.

When I set it to run at logout, I can't get the script to execute quickly enough to do any good. The logout process is so far along that, although the script can launch Mail.app and change the preference via the GUI, the change doesn't actually stick.

I thought, perhaps I could save the script as an applet and deploy it to a known location at login, then have a shell script launch it. But I seem to be running into the same problem. I can understand that you wouldn't want the login process to continue unabated while doing a package deployment, so I get why it's holding things up.

Is there a way to get a GUI script to run at login, and have it behave as it would if it had been added to the Login Items for that user? In other words, prevent its execution until the user was fully logged in.

Thanks for any suggestions!
--
Bryan Vines
bkvines at wgclawfirm.com

2 replies

Forum|alt.badge.img+31
  • Honored Contributor
  • 2721 replies
  • April 24, 2009

Bryan

Have you looked into launchd and tossing your script in /Library/LaunchDaemons and then creating a launchd item that runs the script and using the KeepAlive option it could run any time anyone modifies that preference file.

Another thing you could do is have a log in hook via Casper and use the self healing option.



Thomas Larkin
TIS Department
KCKPS USD500
tlarki at kckps.org
blackberry: 913-449-7589
office: 913-627-0351


Forum|alt.badge.img+4
  • Author
  • Contributor
  • 24 replies
  • April 27, 2009

Thomas,

I did further research and discovered I could use Global Login Items, a feature of Leopard.

source: <http://mattdanger.net/2009/01/scripting-login-tasks-in-mac-os-x/ >

GLOBAL LOGIN ITEMS

GlobalLoginItems has superseded SystemLoginItems, and in some cases LoginHooks, as the best method for launching a login script. Unlike LoginHooks, a Global Login Item executes after all the login processes have completed and is meant for GUI applications. You can even configure whether you would like the application’s GUI to appear or not.

To configure a Global Login Item run the following command as root:
defaults write /Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -array-add '{ "Path" = "/path/to/ script"; "Hide" = "0"; }'

To remove the global login item either delete /Library/Preferences/ loginwindow or run the following command as root:
defaults delete /Library/Preferences/loginwindow AutoLaunchedApplicationDictionary

Note that we are modifying the loginwindow.plist file and not the more popular com.apple.loginwindow.plist file. A reboot may also be required after deleting a global login item

More information about Login Items can be found in this Apple Technical Note:
http://developer.apple.com/technotes/tn2005/tn2083.html#SECGLOBALLOGINITEM

Taking that information, I created a shell script to issue the defaults write command, and packaged up my GUI Script.
Then I set a policy to run once per user, at login, to do the following: - Execute the shell script, which sets the Global Login Item. - Deploy the GUI script.

As the user logs in, the Global Login Item gets set and the GUI script is deposited to a known location. The login process is held at bay until these tasks complete. When the login process resumes, the Global Login Item launches the GUI script after the GUI has loaded; the GUI script is then able to launch Mail, open Mail's prefs, switch to the Junk Mail panel, uncheck the "Enable junk mail filtering" checkbox if it's checked, close the Preferences window, remove the Global Login Item, and finally remove itself.

Mission accomplished. :)

Thanks for your help!
--
Bryan Vines
bkvines at wgclawfirm.com


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings