scripted "if" shutdown

Joined
May 17, 2010
Messages
3
Reaction score
0
Points
1
I was told this forum would be appropriate to post a question about applescript. Using automator, I have created a "timeInAndOut.app". Currently it is set as a login item, so every time i log into my work computer, it opens textedit, opens a specific file on my desktop, copies the date and time to clipboard and then pastes it into the text file and closes textedit, saving the file.

Originally i thought about just running the .app as a logouthook, but idk if that is going to work like i want it, or at all. I have decided the easiest way would be a simple script that checks the time and if it is between a certain time, the cpu should shutdown.

normally i just use control-option-command-eject to shutdown at the end of my split shift, but it would be nice to just run that script, have it log the date and time and shutdown the computer.

it should be a simple "if" condition, but idk how to properly word the syntax. Any help would be greatly appreciated.
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
So much work when you can just run a simple bash script to log the time:
date >> myFile.txt
(>> appends to the file, instead of overwriting)

or in applescript:
do shell script "date >> myFile.txt"

Much more effective then opening text editing and copy/pasting :) there will be ways to do this inside of applescipt as well, I just don't know how exactly. I don't want to force this on you, it's just a suggestion.


As for the rest of your post I'm confused as to why would you run a script to check the time? you want to run this manually? For shutdown I would just create an applescript/app that could be called myShutdown which would log in the same manner and then shutdown, but you want to have this running all the time? you mean like a process in the background waiting for a certain time to shutdowm?

Edit:
Oh, I think I get it now, you want to have a background process periodically checking the time and shutting down your computer at a certain time? Why not use the system Scheduler found in System Preferences under Energy Saver?
 
OP
S
Joined
May 17, 2010
Messages
3
Reaction score
0
Points
1
no i don't want it running all the time eating up clock cycles. it's really just for my own education and amusement. i simply want a record of all the times my account is logged into and out of. just for record keeping purposes, so i can compare my actual time sheet to the dates and times listed here.

I like the bash script idea, and i'm not opposed to it, i just have most of the work done already and thought a couple IF statements might no be so difficult. everything i have script-wise i just copied and pasted from other scripts. I can read the scripting, just not write it.
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
well the if syntax is the following:
if condition then
what_you_want_to_do
end if

I would help you out with putting the whole script together, but I don't have my Mac at the moment - only a linux system - and I have no idea when I get my Mac back.

hopefully I will remember to get back to this post in the future :D
 
OP
S
Joined
May 17, 2010
Messages
3
Reaction score
0
Points
1
okay. i got a bit closer. the time was 8:50 PM

Code:
on run {input, parameters}
	
	set CurTim to (time string of (current date))
	
	if the CurTim is greater than 1 then
		tell application "Finder" to shut down
	else
		null
	end if
	
	return input
end run

this will shut down the cpu as was made evident by it asking me if i wanted to save a textedit document. cancel of course canceled the shutdown as well.

Code:
on run {input, parameters}
	
	set CurTim to (time string of (current date))
	
	if the CurTim is greater than 9 then
		tell application "Finder" to shut down
	else
		null
	end if
	
	return input
end run

this did not shut down the cpu because it wasn't greater than 9:00 PM. So I think i can safely assume that a single digit will act as the hour.

now i need to be able to specify a time of 1:30 PM without getting an error on the colon ":".

and i also need to specify a range. Between 1:30 PM and 5:30 PM
 
Joined
Jan 12, 2009
Messages
1,096
Reaction score
19
Points
38
Location
Prague, Czech Republic
Your Mac's Specs
2,4Ghz 15" unibody
use quotes to override the colon: "1:30" - hopefully it will work, you might need to look for string comparing functions. Although I would look for a way to store it as a digit/integer and not a string ... If you can format the time to be in 24h format, then you don't need to worry about the colon, since 1:30 would be 0130

to use a conditional for your range do the following:
if (CurTim>0130) and (CurTime<0900) then
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top