0

I work at a store, which sells Apple computers.

I’m trying to find a way to close all open programs after a few minutes. It’s because of our Shop Demo Account for customers. After opening all windows, it’s quite messy and I want to get a clean user experience.

All I would need is to close all windows. It should work like the command:

killall -u admin

BUT without admin permission

AND after 1 min,

WITHOUT user interaction.

Maybe, there is a way to: close all windows and programs, without showing something like "do you really want to", log out the demo account, and reload.

I found this, after searching some time, but I don't know if that's what I was looking for and how to make it work:


Apple Script Log Out:

on run tell application "System Events" to log out end run


You can wrap this up in a bash alias using the osascript command: alias maclogout="osascript -e 'tell application \"System Events\" to log out'"

It is the same as clicking " > Log out [username]...", and will logout after a 2 minute wait

This is easily combined with the sleep command: alias delayedlogout="sleep 3600; maclogout"

..or could be combined into a single alias:

alias delayedlogout="sleep 3600; osascript -e 'tell application \"System Events\" to log out'"

Could someone please give some advice? Thanks!

2 Answers 2

1

Here you go. A simple script you can setup to run automatically in the background using launchd or other means of your liking...

#
# From: http://www.dssw.co.uk/blog/2015-01-21-inactivity-and-idle-time
# Returns seconds system is idle, ie no user input...
#
set cmd to "echo $((`ioreg -w 0 -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))"

#
# How many seconds user can be idle before we log them out
#
set maxIdleAllowed to 60

#
# How frequently we check the idle time, in seconds...
#
set checkInterval to 30

#
# We loop forever...
#
repeat
    set secsIdle to 0

    try
        set answer to (do shell script cmd)
        set secsIdle to (answer as number)
    on error
        # May want to do something fancy here...
        exit repeat
    end try

    log secsIdle

    if secsIdle > maxIdleAllowed then
        logUserOut()
    end if

    delay checkInterval
end repeat

on logUserOut()

# Exclude Finder at minimum because bad things happen, so I am told...
set excludedApps to {"Finder"}

tell application "System Events"
    #set oAppList to get id of every application process whose background only is false
    set {processList, idList, pidList, bidList} to the {name, id, unix id, bundle identifier} of (every application process whose background only is false)
end tell

set i to 0
repeat with p in processList
    set i to i + 1

    log "ID: " & item i of idList
    log "PID:" & item i of pidList
    log "Name: " & p
    log "Bundle: " & item i of bidList

    if p is not in excludedApps then
        try
            log "Quit with out saving app with id: " & item i of idList

            # timeout to prevent blocking by certain apps...
            with timeout of 1 second

                # Use bundle id for some odd apps... soffice i'm talking to you!?!
                tell application id (item i of bidList) to quit saving no

            end timeout

        on error
            try
                log "Giving up, killing pid: " & (item i of pidList)
                do shell script "kill " & (item i of pidList)
            end try
        end try
    end if

    delay 1

    log "" & return
end repeat

# Finally, with all apps closed, kill our session...
tell application "loginwindow" to «event aevtrlgo»

end logUserOut

EDIT: Your question has already been answered really, it's just that your action, after idle time, is different. Take a look here at,

How can I automatically launch an application whenever the Mac goes idle?

EDIT2: There you go. Added routine to close apps nicely if possible, and if not, kill them and logs out.

6
  • first of all, I appreciate for your advice!! THANK YOU! It works well, but I would like to LOG OF immediately, even when a iMovie edit, or a unsaved document are open.
    – Lars
    Sep 19, 2015 at 22:08
  • @Lars Sorry, I missed that part in your question. Added the code to quit/kill apps and logout. Sep 20, 2015 at 21:46
  • Thank you, thank you so much! Unfortunately not quite working. So, if Numbers oder Safari is running, the one in the background closes, the program in front stays open. And it doesn't log out. It's an Demo/Kiosk (no produktive) mac - I think, we could use more dark and bad force ;-)
    – Lars
    Sep 21, 2015 at 5:20
  • @Lars Sorry I don't have Numbers, running 10.6.8 so I can't test to see the problem. On my Snow Leopard macbook it runs quite nicely. Probably needs to be tweaked for new versions of OSX... Sep 21, 2015 at 6:16
  • 1
    @Lars Do you have access to the admin/root account? Because then we could just KILL everything with a simple shell script .... Sep 21, 2015 at 6:28
0

I think you'll want to keep Vic's script whether or not this works because it offers some options that this suggestion does not, but if you have a recent system version, try the setting accessible from the Security prefpane (click lock) > Advanced (button in lower right) > Log out after _ minutes of inactivity. I think 5 may be the lowest number you can keep there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .