Is this possible?

I am new to Apple Script and I was just trying to see if anyone has any idea if this is possible. A long time ago an apple script application was made that Logged-Out our public machines. All students did was just double click then bam it logged them out. It also does some other stuff cause if you have external drives plugged into the machine it reminds you that you should eject them before you log out, but I did not write the script So I was wondering is there A way to deconstruct the script application to see what exactly it is doing. Or capture the commands that its sending to bash? I have been tasked with updating the script for Snow Leopard but the is no original script file to work from and the person that created it is long gone. I am sure that I could write a script that logged the computers off but now I am curious is this script is doing anything else and would just like to see if I could figure it out. Thank you for your help.

Scott

I don’t know how to get human-readable data out of run-only scripts (that’s what you want, isn’t it?). But I made this script for you and it does something similar.

It first ejects all (ejectable) disks using the Finder.
Then it looks for applications that are still open (except for the Finder and the application itself).
If there are open applications it asks the user what to do.
If the user wishes to continue, it forces all applications to quit.
Last bot not least it logs out using System Events.

If any error occurs, the script either stops or asks the user what to do.

Script:

-- EJECT ALL DISKS
try
	tell application "Finder" to get every disk whose ejectable is true
on error
	display alert "Failed to eject all disks. Do you want to continue?" buttons {"Yes", "No"} default button 2 cancel button 2
end try

-- CHECKING FOR RUNNING APPS
tell application "System Events" to set runningApps to name of every process whose background only is false and name is not "Finder" and name is not (name of me as string)

-- if apps are open
if (count runningApps) is not 0 then
	set appString to listToText(runningApps, return)
	display alert "One or more applications are still running:" & return & appString & return & return & "Do you wish to continue and force quit all applications?" buttons {"Yes", "No"} default button 2 cancel button 2
	
	-- killall
	repeat with i in runningApps
		try
			do shell script "killall " & quoted form of (i as string)
		on error
			display alert "The process \"" & (i as string) & "\" could not be stopped. Please stop it mannually and try again."
			return
		end try
	end repeat
end if

-- LOGOUT
tell application "System Events" to log out

(* ===== HANDLERS ===== *)
on listToText(l, delim)
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delim
	set t to l as text
	set AppleScript's text item delimiters to tid
	
	return t
end listToText

Hope it helps,
ief2

good old shift-apple-Q will do this right out of the box, with the exception of ejecting removables - but there ought to be a way of forcing that to happen on logout.

Thanks ief2 that script will work just fine I do believe. And yes human readable was what I was going for. Mostly to just see what all was going on. But the script is much appreciated this gives me a lot to work with.

And yes Mr. Friendly shift+command+Q will do that just fine. But not everyone that comes into the lab necessarily knows were everything is on a mac and a nice big red “Button” that says log out on the desktop is welcome site to some I would assume.

If anyone else has suggestions for how to get human readable output from a run only script I would love to know.

Thanks again guys.

Is there any way to get this script to run when attached as a folder action? I want my computer to shut down when opening the folder.

I edited the script posted a little:

try
tell application “Finder” to get every disk whose ejectable is true
end try

– CHECKING FOR RUNNING APPS
tell application “System Events” to set runningApps to name of every process whose background only is false and name is not “Finder” and name is not (name of me as string)

– if apps are open
if (count runningApps) is not 0 then
try
do shell script "killall "
return
end try
end if

– LOGOUT
tell application “System Events” to shut down

do shell script "killall "

doesn’t make sense.

You have to have a name of a process for “killall” to do anything.

Hello

Its a cool script.

However I would change the log out part with this:

tell application “System Events”

key down command
key down option
key down shift
keystroke "Q"

end tell

It just me thinking the last question is unecceccary, as I would if I used that script every time I logged out.

Pretending I use this script:
I want to log out and have executed the script.
I’ve been asked once already, and if my disks and all my procesess are closed (by me), I’d accept that I had to log in again at that moment should the logout have been a bummer.

Best Regards

McUser

Hi,

it’s pretty dangerous to perform key down commands without a balancing key up sequence.

Anyway, the weird GUI scriptiong is not needed, System Events has a log out command


tell application "System Events" to log out

Hey ief2, for some reason Xcode doesn’t like that code in my ASOC project…