Log Out or Restart from a script

I want this to give the user an option to log out or shut down there session when finished with there session but I can not get the log out option to work. Here is the code. What am I doing wrong?

tell application “Finder”
display dialog “Are you sure you want to shut down your computer now?” buttons {“Cancel”, “Log Out”, “Shutdown”} default button 3
set userDec to button returned of result
if userDec = “Log Out” then
log out
end if
if userDec = “Restart” then
restart
end if
end tell

  1. You’re in the wrong forum.
  2. It’s “their” not “there”. (Sorry for being persnickety. If you had done this once I would have let it go but twice was too much for me.)
  3. You should use a compound if-then-else statement.
  4. You’re testing for the user returned button with a name of “Restart” when that isn’t a possibility with the buttons you’ve provided in the dialog.
  5. The answer to your question: In OS X, the Finder has been relegated to dealing with files while System Events handles many other system related operations. As such, this should work:
set userDec to button returned of (display dialog "Are you sure you want to shut down your computer now?" buttons {"Cancel", "Log Out", "Shutdown"} default button 3)
tell application "System Events"
	if userDec = "Log Out" then
		log out
	else if userDec = "Shutdown" then
		shut down
	end if
end tell

Jon

Nice corrections on the usage of there vs their. Made me laugh and laughing is always a good thing. The only problem is when i run it in a new script window and I select logout I get an execution error stating: “The variable out is not defined” and i get the only option of stop. I shouldn’t have to define out but that is what it wants. Suggestions?

What version of OS X are you using? Did you use the code I posted above? It works as desired in OS X 10.3.2 and I suspect earlier versions (although you still get a secondary confirmation of a log out and requests to save open, unsaved documents, as necessary).

Jon

I am running 10.2.8 at this moment in time. I did copy and run it in a new script window but when I select logout I get the execution error: “The variable out is not defined” and i get the only option of stop. I shouldn’t have to define out but that is what it wants. Suggestions?

I don’t think this will work in OS X 10.2.8. System Events (even the beta) doesn’t offer a ‘log out’ command.

– Rob

OK, that pretty much blows. What about scripting key strokes. Say I want my script to execute a key stroke command “apple - shift - Q” and then select the log out option? any suggestions on how to do that?

Thank you for everyones input. I really appreciate the help and love I have been recieving from the great BBS!!

Steve W.

Maybe there’s something useful in this thread [Mac OS X Hints].

– Rob