2

I want to create a bash script that will execute a one-off volume command at a certain time, however when I follow the instructions given I simply get an immediate print out of:

job 4 at Wed Jun 12 16:55:00 2019

My problem is similar to this question: How to run a script at a certain time on Linux?

However a user on there had commented the same problem I have had and this was not answered in the question, so I was wanted to know if anyone else has a solution to this. From my limited understanding the commands in mac should be similar if not identical to linux so let me know if there's anything I'm missing.

My executable file looks like this:

#!/bin/bash

osascript -e "set Volume 0" | at 16:55

Instead of altering the volume at the specified time, it immediately mutes the computer and prints out the text given above.

Weirdali
  • 413
  • 1
  • 7
  • 16
  • To clarify, do you want to do this every day, or only the day that you're running it? In any case, I think this is what you're looking for: https://superuser.com/a/126928/97415 – ratbum Jun 11 '19 at 16:38
  • Notice that in the question you link, the solution is to `echo` the command to `at`; you're *running* the command instead. Something like `echo 'osascript -e "set Volume"' | at 16:55` might work. – Benjamin W. Jun 11 '19 at 17:47
  • @BenjaminW I tried that and it didn't work. I seem to have tried every variation that others have said to work and it still just immediately prints out the job and does nothing. At this point I'm not convinced that at seems to be working at all. When I try launchctl load -w /System/Library/LaunchDaemons/com.apple.atrun.plist it says it's already loaded so I unload and reload. But still that seems to achieve nothing, and when I go to that directory there is no com.apple.atrun.plist file. Could that have anything to do with it? – Weirdali Jun 12 '19 at 10:41

2 Answers2

1

This will require you to have Python 3 and to install the dependencies at the top of the file, but it does work.

#!/usr/bin/env python3

import datetime as dt
import osascript
import sys
from threading import Timer 


def do_the_biz():
    osascript.osascript("set volume output volume 0")
    print("Happy birthday.")
today = dt.datetime.now()
dateString = today.strftime('%d-%m-%Y') + " " + sys.argv[1]
newDate = today.strptime(dateString,'%d-%m-%Y %H:%M')
delay = (newDate - dt.datetime.now()).total_seconds()
Timer(delay,do_the_biz,()).start()

Once you've done: $ pip install osascript... and all the rest of it, use it like this:

$ ~/filename 16:55

The above assumes it's in your home folder, but it could be anywhere, so long as you put the path in.

ratbum
  • 1,030
  • 12
  • 29
0

Use a heredoc to avoid echo

at 16:55 <<!
    osascript -e "set Volume 0"
!

then, verify you job is schedulled by

at -l

which list all jobs.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134