Hi all. I know this i a long post, but it’s interesting… please read on.
I have no prior coding experience and this is my first attempt at an applescript. What this script does is check to see if a user has completed their timesheet. If they haven’t the script will warn them that they have not and bring them to the intranet site to do so. If they have it will wait 4 hours and check again. If after 1 warning and 2 hours later they haven’t completed their timesheet, the script will attempt to log the user out (it’s kind rough I know, but I work for a corporation).
When the user logs out the script places a file that is checked when the user logs back in. This way it knows that the user has already been told they need to logout. If after one hour the user hasn’t completed thier timesheet, the script tries to log them out agian. This continues to happen until the timesheet is completed. No open work should be left unsaved because i’m not sending any kill commands.
As an avid mac user, I am very excited about my new knowledge of applescript and also that this script works (a lot to do w/ info I found from this site!) But I am looking for suggestions on ways to make it more efficient and functional. I know my coding is not all that.
Thanks for any/all suggestions!!
Anywho, on with the scripts…
heres the script that calls the main one (don’t know why I had to do this, the main script just wouldn’t quit at logout):
STARTUP SCRIPT (startTSE.app) This is executed from /Library/Preferences/loginitems.plist
set s to "/usr/bin/osascript /private/tse/TSE.app > /dev/null 2>&1 &"
try
do shell script s
end try
quit
END STARTUP SCRIPT
and here’s the main script:
MAIN SCRIPT (TSE.app)
set username to do shell script "whoami"
set s to "curl --connect-timeout 10 --max-time 5 http://tsenforcer/cgi-bin/query.pl?username=" & username
set theAxe to false
set warn1 to false
set r to do shell script s
if r is "" then
repeat until r is not ""
set r to do shell script s
end repeat
end if
if r is "NO" then
set delayWindow to 14400 --4 hours
delay delayWindow
else if r is "YES" then
beep
set warn1 to true
set delayWindow to 7200 --2 hours
end if
tell application "Finder"
if document file "MacintoshHD:private:tse:enforcer.logout" exists then
set theAxe to true
set delayWindow to 3600 -- 1 hour
end if
end tell
if theAxe is true then
open location "https://home.companyname.com/"
end if
repeat
tell application "Finder"
if document file "MacintoshHD:private:tse:enforcer.logout" exists then
set warn1 to false
set theAxe to true
end if
end tell
if warn1 is true then
tell application "Finder"
with timeout of 100000 seconds
activate
set visible of (every process whose visible is true and name is not "Finder") to false
display dialog "? Please Complete Your Timesheet:
Our records indicate you have one or more outstanding timesheets. Click OK to be taken to the Companyname Portal to submit your timesheet. Note: the system will log you off the network in two hours if your timesheet remains outstanding. Thank you." buttons ["OK"] with icon caution
end timeout
end tell
if button returned of result is "OK" then
open location "https://home. companyname.com/"
else
open location "https://home. companyname.com/"
end if
delay delayWindow
set r to do shell script s
if r is "" then
repeat until r is not ""
set r to do shell script s
end repeat
end if
if r is "YES" then
set theAxe to true
end if
set warn1 to false
end if
delay delayWindow
if theAxe is true then
tell application "Finder"
activate
set visible of (every process whose visible is true and name is not "Finder") to false
display dialog "You Are About to be Logged Off:
Our records indicate you have one or more outstanding timesheets.? Please click OK and save your work before the system logs you off in two minutes. ?Then proceed to the Companyname Portal to submit your timesheet. ?Note: the system will continue to log you off the network once every hour if your timesheet remains outstanding. Thank you." buttons ["OK"] with icon stop
if button returned of result is "OK" then
try
do shell script "touch /private/tse/enforcer.logout"
end try
delay 10 -- 2 minutes
with timeout of 100000 seconds
ignoring application responses
tell application "loginwindow" to «event aevtrlgo»
quit
end ignoring
end timeout
end if
end tell
end if
set r to do shell script s
if r is "" then
repeat until r is not ""
set r to do shell script s
end repeat
else if r is "YES" then
set warn1 to true
set delayWindow to 7200
set r to do shell script s
else
try
do shell script "rm /private/tse/enforcer.logout"
end try
end if
if r is "NO" then
set delayWindow to 14400 -- 4 hours
end if
end repeat