Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have the following code in a text file (myfile.applescript):

#! /usr/bin/osascript
tell application "Google Chrome" to quit

I would like Chrome to shut down each night at 9:15PM, so I added the following cron task to crontab:

15 21 * * * ~/Desktop/myfile.applescript

Unfortunately, the script is not triggering. Any thoughts?

NOTE: I am aware I can use iCal to resolve this problem. However, I'd prefer to not clutter my calendar with a daily script-triggering event.

share|improve this question
add comment

4 Answers 4

Cron has be deprecated on modern versions of OS X. Apple's suggestion is to use launchd instead. You will find Lingon to be a useful tool for creating launchd daemons.

If you would prefer to use iCal to launch the script, you can create a separate calendar for scripted event(s), and hide it from view. It will function, but your calendar view will not be cluttered.

enter image description here

share|improve this answer
    
Thanks a lot for the hint at Lingon! –  fanaugen Mar 8 '12 at 7:23
add comment

Make sure the ~/Desktop/myfile.applescript file is executable. You can only call a script by filename if it has the relevant execute bit.

Consider the following Terminal session:

$ ls -laFh sh.sh
-rw-rw-r--  1 jason  staff   278B Mar  2 10:18 sh.sh

$ ./sh.sh
-bash: ./sh.sh: Permission denied

$ chmod 764 sh.sh

$ ls -laFh sh.sh
-rwxrw-r--  1 jason  staff    32B Mar  7 09:40 sh.sh*

$ ./sh.sh 
Hello World!

Until I had execute permissions (denoted with an 'x' in either the 4th, 7th, or 10th slot of the permissions string), I could not run it.

You can override it by calling the file with it's particular interpreter:

$ chmod 664 sh.sh

$ ls -laFh sh.sh
-rw-rw-r--  1 jason  staff    32B Mar  7 09:40 sh.sh

$ ./sh.sh
-bash: ./sh.sh: Permission denied

$ sh sh.sh
Hello World!
share|improve this answer
    
I'm sorry about the terrible filename. It was a temporary file so I gave it a stupid filename. But "sh sh sh" all in a row is very absurd. And for the record: I highly suggest using launchd, cron is easier but launchd is more complete. Apps like Lingon that jaberg referenced really do help to make a functional launchctl plist in a hurry. –  Jason Salaz Mar 7 '12 at 16:46
add comment

I discovered my original script worked. The problem had to do with my cron entry: it wasn't terminated with a carriage return. Once I did that, the applescript triggered properly.

share|improve this answer
add comment

Try the following in your script.

#!/usr/bin/bash
/usr/bin/osascript -e 'tell application "Google Chrome" to quit'

Make sure it's chmod +x and test from CLI.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.