AppleScript to log out and in the user

Discussion in 'Mac Programming' started by soer, Feb 12, 2010.

  1. soer macrumors newbie

    Joined:
    Feb 12, 2010
    #1
    I’m trying to make an AppleScript that logs out the current user, then immediately log in the same user again. This would help me solve a problem with a blue screen when my Mac mini and Plex wakes up from sleep mode.

    I’ve tried with the following code, put together from different forums:

    Code:
    set logInUser to ”Media"
    set logInPassword to ”media"
    tell application "System Events" to log out
    delay 1
    tell application "System Events"
        keystroke return
    end tell
    delay 2
    tell application "System Events"
        tell application process "loginwindow"
            key code 53
            delay 1
            key code 125
            delay 1
            key code 36 using option down
            delay 1
            keystroke tab
            delay 1
            key code 117
            delay 0.5
            keystroke tab
            delay 0.5
            key code 117
            keystroke return
            delay 1
            keystroke tab
            delay 0.2
            repeat with aChar in characters of logInUser
                keystroke aChar
                delay 0.2
            end repeat
            keystroke tab
            delay 0.5
            repeat with aChar in characters of logInPassword
                keystroke aChar
                delay 0.2
            end repeat
            key code 98 using control down
            delay 0.2
            keystroke return
        end tell
    end tell ¬
    The problem is that when the script logs out the user, all apps are terminated, also the script itself, making the script stop before it can log in the user again, leaving me a log in window on the screen. How do I solve this?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    You can't.

    If login could be scripted, it would be a huge exploitable security hole.

    And if users could leave scripts running after logout, that would be another security hole.
     
  3. soer thread starter macrumors newbie

    Joined:
    Feb 12, 2010
    #3
    I almost thought so. :(

    Thanks anyway!
     

Share This Page