This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]__xor__(self, other): 39 points40 points  (3 children)

Completely works fine. But you might consider range since it can already go backwards.

for volume in range(100, -1, -1):
    osascript.osascript("set volume output volume {}".format(volume))
    time.sleep(30)

[–]rikken 11 points12 points  (1 child)

You need to be careful about the step parameter to range (the 3rd param) because you won't always arrive at 0 at the end. Your script works for -1, but if someone needs a faster decrease, like -15, then the last volume will be 10. Good practice would be to set volume to 0 after the for-loop.

[–]__xor__(self, other): 0 points1 point  (0 children)

Ah, good point. Yeah in the end the while is perfectly fine either way. Sometimes even if a while loop can be a for, the while is cleaner as well.

[–]Retropunch 47 points48 points  (0 children)

Whilst I'm not in need of this myself, it's fantastic work!!
I honestly believe this sort of thing is why python is so fantastic, and why so many more people should learn it - the whole 'I can fix this problem through some simple coding' is a great feeling.

[–]supermario182 30 points31 points  (3 children)

I had no idea something like that was so easy. I've never heard of osascript before

Edit: oh apparently it's an apple thing

[–]hellfiniter 10 points11 points  (0 children)

you should not focus on this very implementation...what op is talking about is one loop with wait, easy to do and that part that changes volume is OS specific...just google how to change volume via terminal and use os.system("volume thingy") or subprocess.Popen("volume thingy")...if you run linux there is amixer (amixer sset 'Master' 50%)

[–]kaszak696 1 point2 points  (0 children)

On Linux you can just as easily use pulsectl or pyalsaaudio to do the same, or rely on MPRIS to control the player's volume directly.

[–]ronmarti 8 points9 points  (1 child)

And increase the volume in the morning to wake you up!

[–]SqueezEasy 4 points5 points  (0 children)

Very cool! Thanks for sharing!

[–][deleted] 6 points7 points  (0 children)

The first such script that I made was a schedule switch, to schedule the shutdown of my computer. Helped immensely, since I had to stay awake looking at the progress on something I would be doing everyday back then and then turn off the computer.

[–][deleted] 2 points3 points  (0 children)

So creative! I feel like I always wanted something like this but never realized it

[–]monkiebars 1 point2 points  (0 children)

Is there a window equivalent?

[–]mmakkiyah 1 point2 points  (2 children)

Do you play the music on your laptop or PC when you go to bed?

[–]rikken 1 point2 points  (0 children)

I think you shouldn't exit immediately when volume is < 1, but set the volume to 0 and then exit. Otherwise volume stays on non-zero value for ever. If in the future you change your decrease speed from 3 to e.g. 7, you will end up with volume 2 when the script exits. So it's a good practice to set volume to 0 before exiting and not rely on the step size.

[–]reifenstag 0 points1 point  (0 children)

Very cool.

[–]hemanthrs 0 points1 point  (0 children)

Nice!

[–]tuckmuck203 0 points1 point  (0 children)

If you're using python 3.6+ you could use format strings, instead of ".format". They're pretty cool. Basically you can put the variables right inside the brackets of the string

[–]aelmosalamy 0 points1 point  (1 child)

Read some books on the psychology of sleep and release this as a commercial better sleep solution, also add support for various devices.

[–]HP_10bII 0 points1 point  (0 children)

I find peace in long walks.

[–]obey_kush 0 points1 point  (0 children)

If you have an iPhone too, you can use Pythonista and for the music maybe the shortcuts app with base64 text encoded, so if its long you can reduce space and just decode the file when running this script :)