42

I want to have my computer set its volume to a specific level every night at 11:45 PM. I'm running OSX 10.11.4. I can set the volume manually through terminal with

osascript -e "set Volume 1.7"

or as a script with

set volume 1.7

I want it to be scheduled nightly though. It's hard to find anything online that isn't super outdated. I don't really want to use iCal. From what I've found online, launchd is the way to go, but as a noob, I don't know where to start.

I see things about using a .plist in /Library/LaunchAgents. So I found a nifty plist generator Launched.zerowidth.com but what kind of code do I put in the plist to get the desired effect? I'm also questioning if this is the correct path for this to execute if any user is logged on.

Am I going down the wrong path here? I'm open to any ideas to make this happen, but I don't want to use a 3rd party app that I have to keep open all the time.

Thanks,

Naboo

Ross Jacobs
  • 2,962
  • 1
  • 17
  • 27
Naboo
  • 421
  • 1
  • 4
  • 4
  • 2
    I'm voting to close this question as off-topic because it is an OS question (scheduling a task) rather than a programming question. Your question may be better suited for [apple.se] or [su] instead. – Ken White Apr 26 '16 at 02:28
  • 1
    Do not use cron, it has been deprecated. [Apple's documentation](https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/ScheduledJobs.html) for scheduling jobs describes how you do this with launchd – TheDarkKnight Apr 26 '16 at 08:30
  • When you say cron has been deprecated, does that mean it no longer works? – Cauder Jul 11 '20 at 02:41
  • @Cauder `cron` still works on macOS (as of macOS 11 Big Sur 2020). It's fine to use to `cron`. [Apple's documentation](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/ScheduledJobs.html) says "In OS X, you can run a background job on a timed schedule in two ways: `launchd` jobs and `cron` jobs. (Older approaches, such as `at` jobs and `periodic` jobs are deprecated and should not be used.)" – wisbucky Jun 14 '21 at 16:37

3 Answers3

41

As @TheDarkKnight points out, cron has been deprecated in favor of launchd.

To use launchd, save the following as com.example.volume.plist in ~/Library/LaunchAgents/:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>com.example.volume</string>
        <key>ProgramArguments</key>
        <array>
                <string>sh</string>
                <string>-c</string>
                <string>set volume 1.7</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
                <key>Hour</key>
                <integer>23</integer>
                <key>Minute</key>
                <integer>45</integer>
        </dict>
</dict>
</plist>

then run launchctl load ~/Library/LaunchAgents/com.example.volume to start. You can force the task to run immediately via launchctl start com.example.volume.

If you prefer to have it run as root, save to /Library/LaunchDaemons/ instead.

Miles Wolbe
  • 627
  • 10
  • 14
  • 3
    Who knows if it'll be up for the long-term, but here's a nice launchd tutorial - http://www.launchd.info/ – Matt Passell Oct 22 '18 at 19:17
  • 41
    Compared to cron this is hilariously tedious seeming ;-) – agathodaimon Jan 10 '19 at 01:28
  • 3
    @agathodaimon, Yes, simple is better. But only until you need the feature enhancements. launchd support for sleep mode and more complex time conditions makes it a better solution for many use cases. – lkanab Dec 17 '19 at 07:46
  • 4
    @ikanab Not quite sure all the great features justifies the big file approach, bit okiedokie :) – agathodaimon Dec 18 '19 at 22:37
  • I called osascript directly (here, I'm muting, but the idea is the same). `` `osascript` `-e` `set volume output muted true` `` – Egwor Jan 09 '21 at 21:31
  • How difficult can Apple possibly make it to run a script periodically? On Windows, there is a task scheduler, no programming experience required. – bjcullinan Sep 10 '22 at 21:20
30

Please consider using the cron daemon. It is present in osx by default.

Create script for volume adjusting

#!/bin/bash -l
/usr/bin/osascript -e "set Volume 1.7"

Then add new line to crontab.

crontab -e

By default, it will open in the vi(m) editor. But you can adjust the default editor with

export EDITOR=/path/to/your/awesome/editor

Then add new string to crontab

0 20 * * * /path/to/volume/script.sh

The given command will run every day at 8 pm.

Please find more crontab examples here: https://en.wikipedia.org/wiki/Cron

Ross Jacobs
  • 2,962
  • 1
  • 17
  • 27
retgoat
  • 2,414
  • 1
  • 16
  • 21
  • 1
    @Naboo, please mark my answer as right if it was useful. Thanks in advance. – retgoat Apr 26 '16 at 02:33
  • 16
    As stated in Apple's [documentation](https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/ScheduledJobs.html) *"Although it is still supported, cron is not a recommended solution. It has been deprecated in favor of launchd."* – TheDarkKnight Apr 26 '16 at 08:29
  • 1
    No need to create a separate bash script to run an applescript command or script. See https://stackoverflow.com/questions/36854193/scheduling-a-terminal-command-or-script-file-to-run-daily-at-a-specific-time-mac/67975800#67975800 – wisbucky Jun 14 '21 at 19:01
  • this is dumb, but what does the `-l` flag do please? – m1nkeh Dec 29 '21 at 07:55
  • Causes bash to run as though it were a login shell. --I (lowercase "I") There is a obviously typo – retgoat Dec 31 '21 at 04:54
6

For a simple one liner, use cron:

# edit your user crontab
crontab -e

# run daily at 20:00 (8:00pm)
0 20 * * * osascript -e "set Volume 1.7"

For a longer script, you can save it as an applescript (.applescript plain text or .scpt binary). Then call it from cron:

# edit your user crontab
crontab -e

# run daily at 20:00 (8:00pm)
0 20 * * * osascript /path/to/setvolume.applescript

Pro Tip:

To avoid the Terminal.app would like to administer your computer prompts every time you edit crontab, you can go to System Preferences > Security & Privacy > Privacy > Full Disk Access and add /Applications/Utilities/Terminal.app.


Note:

While Apple documentation says launchd is preferred over cron, cron is still fully supported thus far even though it's been "deprecated" for years. It's fine to use cron until it's actually removed (which may be never).

wisbucky
  • 33,218
  • 10
  • 150
  • 101