Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Applescript - Force Timed Logout?

Applescript - Force Timed Logout?
Thread Tools
Dedicated MacNNer
Join Date: Mar 2000
Status: Offline
Reply With Quote
Jun 8, 2001, 02:07 PM
 
Hey everyone, I'm wondering if there is an applescript available or a way to program one that will log out a user in OS 9 after a certain period of time (not an idle timer). Thanks!

S
     
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status: Offline
Reply With Quote
Jul 27, 2001, 05:11 PM
 
Hi SpiffyGuyC,

If you haven't yet found or written a suitable script, you could try searching the usual suspects such as MacScripter's ScriptBuilders (Script Library) section.

[Side note:- If you're running under Mac OS Classic, it would probably be better to find or write a suitable system extension (INIT), as that would provide truly system-wide functionality.]

With the AppleScript approach, one would need to explicitly make sure that the applet is started up at Login (e.g., by placing a copy in the each User's personal Startup Items subfolder). For any Limited or Panels Users, one would also need to make sure that they are granted the privilege of running the applet. There are also tricks that one could use to ensure that the applet runs "invisibly" as a background-only app (BOA), e.g., by fiddling with a Classic applet's SIZE resource ('Only Background' flag) via a resource editor such as Apple's free ResEdit.

The actual code for the script/applet might not be be too difficult. It would need to be saved as a 'Stay Open' applet, so that it could have 'on idle' handler. One would declare a global "timer-start" variable, and a global "timer-interval" value, at the top level of the script, e.g.:-<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>global theLoginTime
property theUsageLimitInSeconds <font color = blue>15</font> * minutes)</font>[/code]The 'on run' handler would initialise that timer-start variable, e.g.:-<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>on run
--<font color = red>"Remember"</font> the login time
set theLoginTime to (current date)
end</font>[/code]The 'on idle' handler would be set to wake up periodically (or, for greater efficiency, it could be set to wake up only at/near the designated time) to check whether the usage interval had elapsed, e.g.:- <BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>on idle
-- Double-check whether it's time to log out
<font color = green>if</font> (((current date) - theLoginTime) &gt;= theUsageLimitInSeconds) then
-- Call a routine to force a Logout (see below)
DoLogout()
end <font color = green>if</font>

-- Signal to the OS that we want to wake up only at/near the designated time
<font color = green>return</font> theUsageLimitInSeconds
end</font>[/code]
The 'DoLogout()' routine is probably the most troublesome piece of the puzzle, because it would require the installation of a third-party scripting addition or OSAX (I don't think the Finder or the Multi-User Startup extension provides a "logout" Apple Event). One could use any of the OSAXen available at MacScripter's Scripting Additions library that can emulate the typing of a keystroke with the Command-key down (to simulate choosing 'Cmd-Q', or whatever keystroke is custom-defined for the Finder's Logout menu-item shortcut), plus pressing the &lt;Return&gt; key (to simulate accepting the subsequent "Are you sure you want to quit and logout?" prompt).

[Update 2001/09/16: Well, mea culpa. :-( I hadn't realised that versions of MU later than 1.1 seem to flush the event queue when the logout prompt is displayed. So, under MU 1.2.x+, any extra &lt;Return&gt; keystroke would almost certainly be lost. The good news is that there are ways around this; the bad news is that it requires an extra step and the use of a scriptable extension (such as the KeyQuencer Engine) which is capable of generating a time-delayed keystroke or button-press and which is also controllable from AppleScript via some interface (such as the 'KeyQuencer Links' OSAX). I've added one such KQ-inspired step to the script below.]

For instance, with the "Sandi's Additions" OSAX (Update: and the commercial KeyQuencer 2.5.x package) installed, one would say something like:-<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>on DoLogout()
tell (application <font color = red>"Finder"</font>)
-- It's necessary to bring an app frontmost to send it keystrokes
activate

-- Set up a <font color = red>"delayed"</font> command to confirm & dismiss the forthcoming
-- Logout prompt
-- [Required <font color = green>for</font> MU <font color = blue>1.3</font>.x+, which seems to flush the event queue, thus
-- discarding an <font color = red>"inline"</font> followup keystroke]
-- ...First, set up the initial <font color = red>"delay"</font> command (waiting <font color = green>for</font> the Logout
-- prompt to appear, up to <font color = blue>1.5</font> seconds)
set theFollowupKQMacro to (<font color = red>"WaitWindow dialog <font color = blue>75</font> ticks quiet"</font>)
-- ...Then, append the actual followup command
-- ...[NOTE: We could use either a button-presser or keystroke-typer command]
(*
set theFollowupKQMacro to ¬
(theFollowupKQMacro & <font color = green>return</font> & <font color = red>"Button \"</font>Logout\<font color = red>""</font>)
*)
set theFollowupKQMacro to ¬
(theFollowupKQMacro & <font color = green>return</font> & <font color = red>"Type <font color = green>return</font>"</font>)

-- Initiate the <font color = red>"delayed"</font> command to confirm & dismiss the Logout prompt
-- NOTE: Requires the 'KeyQuencer Links' OSAX
KeyQuencer Do Macro theFollowupKQMacro

-- Emulate typing the Finder's Logout shortcut
-- NOTE: Requires the <font color = red>"Sandi's Additions"</font> OSAX
TypeText <font color = red>"q"</font> with Command
end tell

-- The applet needs to explicitly quit <font color = green>if</font> it's running as a BOA
quit
end</font>[/code]

In any event, good luck with finding or writing your auto-Logout script or extension!

Regards,

--Paul

[ 09-16-2001: Message edited by: Paul Crawford ]
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:07 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2