Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Commnd C shell script?

The command "caffeinate" will indefinitely keep your mac from going to sleep, to get out of this you must press Control+C. How would I make a shell script for AppleScript to imitate the keyboard action control+c to quit the "caffeination" at a desired time?


do shell script "caffeinate"

delay 10 --not actual script, just an example

--end caffeination

MacBook Pro, OS X Mountain Lion (10.8.2), 2.3 GHz i5, 4GB RAM, OCZ 120GB SSD

Posted on Feb 2, 2013 7:13 PM

Reply
8 replies

Feb 2, 2013 7:46 PM in response to MacMan240

The script will not be able to do anything after issuing the command, since it will be waiting for the shell script to complete - if you are wanting a timeout, then use that option. You can also attach it to the utility you are running, or run the shell script in the background and then terminate it later with a killall caffeinate command - see the man page.

Feb 2, 2013 8:09 PM in response to red_menace

Red is right - at least by default AppleScript will wait for the shell command to finish before continuing the rest of your script. There are workarounds, though:


set thePID to do shell script "caffeinate &> /dev/null &; echo $!"

delay 10 --not actual script, just an example

do shell script "kill -INT " & thePID


In this case, the text added at the end of the shell script forces the process to run in the background (which allows your AppleScript to continue), and returns the process ID of the caffeinate process.

Then, after your intermediate actions, you use another shell command to send an INT (interrupt) signal to the process ID captured in the first line. Ctrl-C is the same as an Interrupt signal, so should have the desired effect.

Feb 2, 2013 8:53 PM in response to MacMan240

The image you included suggests you used single quotes instead of the double quotes Camelot used at the line "caffeinate &> /dev/null &; echo $!" in his AppleScript fragment. It also appears that as a result the first single quote was interpretted as a backtick (`), which has a completely different meaning in the context of shell scripts. Hence the error near `;'.

Feb 3, 2013 6:53 AM in response to MacMan240

Why do it the hard way caffeinate has a flag -t which sets a timeout after which caffeinate terminates.


see man caffeinate


-t Specifies the timeout value in seconds for which this assertion has to be valid. The assertion is dropped after

the specified timeout. Timeout value is not used when an utility is invoked with this command.


I guess its true that when all you have is a hammer all problems look like nails 😉

Commnd C shell script?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.