Script to check timesheets

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

END MAIN SCRIPT

Hi, spinach187!

Too much lines for me (nine o’clock here!), but if it does work, it’s a cool script. You’ll learn debugging and optimizing code if you keep scripting… You’ll learn, for example, to be more analytic:

if button returned of result is "OK" then 
     open location "https://home. companyname.com/" 
else 
     open location "https://home. companyname.com/" 
end if

For example, here, when the user chooses a button, it will be redirected to the “open location” command, in despite of its selection, huh?

In the first lines of code… You are running an app (the “starter”), which runs in the background the main app, ¿no? Then, you could better create a background-app and place it in the login items, and avoid the step to force-main-app-to-be-background. You’ll find some examples here around.

Also, you use several “delay” commands with a great amount of time. This could be ineffective if the computer is sleep past 4 hours, since it won’t auto-wake-up it to follow the process. Also, some folk suggested a time using “sleep” instead of “delay”, because it was less cpu-intensive:

do shell script "sleep 5000"

But, as I said, you’ll learn learning! :wink:

thanks for the feedback! BTW, I can’t make the computer go to sleep, it needs to be on if the user is using it.

-ds

ds, the shell “sleep” command is not the same thing as putting your computer to sleep (I know it’s confusing). It is essentially the same as the AppleScript “delay” command (as jj says) but without the CPU crushing overhead.

Jon

Aha! Yes, that would be useful! I’ll put that in. Thanks for clearing that up!

-ds