Skip navigation

Commnd C shell script?

607 Views 8 Replies Latest reply: Feb 3, 2013 6:53 AM by Frank Caggiano RSS
MacMan240 Level 1 Level 1 (80 points)
Currently Being Moderated
Feb 2, 2013 7:13 PM

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
  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    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.

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    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.

  • g_wolfman Level 4 Level 4 (1,110 points)
    Currently Being Moderated
    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 `;'.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Feb 2, 2013 9:15 PM (in response to MacMan240)

    Remove the semi-colon:

     

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

    delay 10

    do shell script "kill -INT " & thePID

  • Frank Caggiano Level 7 Level 7 (22,805 points)
    Currently Being Moderated
    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

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.