Sometimes when programs install they say you have to reboot after installation. And sometimes they do that automatically. But occasionally a program logs out and automatically logs in again. Even if you have a password for your account.
One example is when you install google importer ( http://www.apple.com/downloads/macosx/spotlight/googleimporter.html )
Is it posible to recreate this effect with an applescript or in some other way? because i really like it, and it’s realy usefull after you’ve done some (automatic) maintenance on your mac…
I’m still thinking about a good algorithm for this, but here’s a quick script.
-- Add this app to login items
-- on login, auto login is turned off
-- if login_state is true, then logging in
-- otherwise, logging out
property login_state : false
--
set c1 to "defaults read /Library/Preferences/com.apple.loginwindow autoLoginUser"
set c2 to "defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser 'kel'"
set c3 to "defaults delete /Library/Preferences/com.apple.loginwindow autoLoginUser"
if login_state then -- logging in
-- check if autoLoginUser is set in error handler
-- otherwise, error and nothing happens
try
do shell script c1 -- errors if key does not exist
-- set auto login to false
do shell script c3
end try
set login_state to false
else -- logging out
do shell script c2
set login_state to true
tell application "Finder" to restart
end if
Maybe, just get rid of the login part. That would eliminate a lot, but auto login would be set. I don’t know how to determine by script if you’re logging in or out (was script run by user or login items?). Have to think more about these things and error checking.
EDITED: Note you have to change the auto login user name in the script.
Just got up, looked at the script, and saw what was bothering me. If user logs out manually, at login, the script will restart once. Still needs to determine if the user ran the script or login items ran the script. Or how to determine if user logged out or script logged out.
I don’t see how you could run this from the user’s account. It strikes me that you’d need to have root daemon poised that, when activated, would log out and then log in immediately if it had been started by your script and not otherwise. The “logger-outer” script that started the daemon would first leave a “fixer” in login items. You’d set autologin from the user’s domain in the “logger-outer” script, start the daemon and quit. The daemon would log out and back in, the system would run the fixer script that was in the login items, set the autologin back the way is was, and remove the second script from the login items. Strikes me as not much AppleScript there, and a lot of shell scripting hidden away somewhere.
Adam
BTW: svdf said “And sometimes they do that automatically. But occasionally a program logs out and automatically logs in again. Even if you have a password for your account.” My comment is that I don’t know of any that haven’t in some part of the process asked for your password and stored it for the login instead of fiddling with autologin settings.
I don’t see what you’re saying. Doesn’t it auto login for you when run from an admin account? If it’s not run from admin account, then I think you would probably need a password to write to the com.apple.loginwindow.plist, so I wouldn’t know how to run it on a non-admin account without password. This sounds like a typo:
“And sometimes they do that automatically. But occasionally a program logs out and automatically logs in again. Even if you have a password for your account.”
and I’m not sure what it means. It probably means as you say "Even if you don’t have a password.
Anyway, I’m through experimenting with login hooks and such, because the last time I did that my machine locked up because of a bad unix script. Wish I had a testing computer that I could just reinstall the os at anytime.
I have an old B&W G3 hanging around and I’ve often thought that if I was ever going to learn to shell script successfully, I’d have to resurrect it, install Tiger on it, and try everything there.
My point, not very well stated, I guess, was that once you’ve logged out of any account, you can’t run a script until you’ve logged it again, and you can’t distinguish the cause of the logout either. When you log out, only root is running.
What I’m trying to do is set flags from a script to itself or another script. I’ve been working on a two script way. The first script is the helper app I named “AutoLoginHelper”.
property login_user : missing value
property change_autologin : false
--
if change_autologin then
set change_autologin to false
if login_user is "" then
set c to "defaults delete /Library/Preferences/com.apple.loginwindow autoLoginUser"
else
set c to "defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser '" & login_user & "'"
end if
do shell script c
end if
quit
It’s saved as a stay open app and added to the login items. It’s a helper for this app"
-- get current user
set current_user to system attribute "USER"
-- check auto login state and store auto login user if any
try
set login_user to do shell script ¬
"defaults read /Library/Preferences/com.apple.loginwindow autoLoginUser"
on error
set login_user to ""
end try
-- set auto login user to current user
do shell script ¬
"defaults write /Library/Preferences/com.apple.loginwindow autoLoginUser '" & current_user & "'"
-- launch helper app and set login user
tell application "AutoLoginHelper"
launch
set login_user of it to my login_user
set change_autologin of it to true
quit
end tell
-- restart
tell application "Finder" to restart
This is just saved as an regular applet. When the user runs this app, it gets the current user and the auto login user if any. It then launches the helper app and sets its properties for the auto login user and a flag to change to the auto login user on next login. If auto login was not set login user is “” and the autoLoginUser key is deleted. Otherwise it’s set to what it was before. It works alright exept when the auto login is set to another user than the current user. In this case, there is no auto login and stops at the login window. In other words, the script works when auto login is off or the auto login user is the current user. It doesn’t work otherwise. I better recheck that.
I tried using password, but it still doesn’t work when current user is not the auto login user. I guess I’ll just limit it to it only works if the auto login user if any is the current user.
Still needs more testing.
BTW, I had an old G3 imac also, but the video went out.
This is what I found out. There is an encrypted password file at /etc/kcpassword.
This is what we would need to do. Suppose there are two users Ann and Joe. Their files might look like this:
Ann:
mc8tyn-8yfh
Joe:
p82ffrn8ytauiln
Now if Ann was the last auto login user and say Joe is now logged in. We change the autoLoginUser to Joe, but the kcpassword file still has Ann’s encrypted password. We can’t overwrite this file just by knowing Joe’s password, but need to know his encrypted password. I haven’t found how it is encrypted. Short of storing every encrypted password, the best thing to do is tell joe to set the auto login user in System preferences to himself. This would change the kcpassword file. From then on we could switch between auto login Joe and no auto login.
For personal use, you’d probably have just one main user doing all the maintenance. For many users you could store all encrypted passwords. Here you would ask for a password to overwrite the kcpassword file. I think I could use the personal version of the script for rebooting after fixing permissions and running the daily, weekly, and monthly scripts, and after downloads and installs. Think I’ll add a reminder in the last posted scripts on how to reset the encrypted password by switching auto login user in System preferences.
Edited: just thought of something. How can I find who was the last auto login user? I think it’s stored in com.apple.loginwindow.plist.