iTunes Fadeout Script

by David Battino

Ah, interruptions. These days, when the phone rings or someone butts in, I hit the F13 key on my Mac. If music is playing, iTunes smoothly fades it down to silence and pauses itself. After the coast is clear, I hit F13 again and the Mac asks what volume I'd like playback to resume at. Hit Return and the music fades back up. The effect is more pleasing than simply stabbing the Mute key, and it means I don't lose my place. Further, being able to enter a volume directly is often nicer than messing with the volume up/down keys.

itunes-stop-shadow.jpg

Launch the script when iTunes is playing, and you're prompted to enter a new volume level. The default is 0, creating a fadeout.

It's all done through this AppleScript I assembled from snippets at OSX Hints and Doug's AppleScripts, plus some experimentation. I mapped it to the F13 key using the no-longer-available LumaCode Key Xing, but it should work fine from other AppleScript launchers.


34 Comments

Gavin Dold
2007-03-05 08:00:32
Nice, but perhaps it would be nice if it quickly faded back the volume as well... Also, I prefer just running the script. I think it would be better to just fade to zero, pause, and when you run it again it quickly fades back to the original volume...
Gavin Dold
2007-03-05 08:42:10
By "just running the script" I mean no dialogues,,.
Bryan Liles
2007-03-05 09:01:03
This seems complex. Why can't you use just Quicksilver and some iTunes triggers?
David Battino
2007-03-05 09:04:28
@Gavin: You're right. It doesn't fade in from pause. I'll see if I can add that. The reason I offered the level-setting dialog was because sometimes I just want to change the level rather than fade out and this way one button is all I need.
Gavin Dold
2007-03-05 09:39:58
Fair enough, I just prefer pressing a button to do it all. I tried editing the script to fit my purposes, but could only get it to work properly in Script Editor... I guess I'll have to assume volume to be set is some value or have a dialogue...
David Battino
2007-03-05 09:47:58
@Brian:
This seems complex. Why can't you use just Quicksilver and some iTunes triggers?
Does QuickSilver offer fading? The real reason to use AppleScript, of course, is to get more control and learn how the Mac works. I'm sure there's a more efficient way to code this. Perhaps someone will show us.
Gavin Dold
2007-03-05 10:06:23
Well, I've edited the script to have no dialogues, just fade in and fade out... Do you want me to post it?


One question: is there any way to just end the script if iTunes isn't running? Currently, it just opens iTunes instead. I guess you might want that, but what if I accidentally press the wrong key or key combination?

David Battino
2007-03-05 10:17:45
@Gavin:
Well, I've edited the script to have no dialogues, just fade in and fade out... Do you want me to post it?
Sure! I added one as well.
The okflag bit is supposed to end the script if iTunes isn't running. It doesn't seem to work consistently. Anyone have a better approach?
Gavin Dold
2007-03-05 10:45:56
I've found the problem, why it always launches iTunes...


On my computer, it keeps setting "okflag" to true, whether iTunes is running or not. Thus it always runs the full script. Annoying. So we'd better find some other way of testing...

Lee
2007-03-05 13:40:56
I want to thank you folks for working on this. I'm not a developer, but this script will be very handy for me. If possible, I'd be most grateful for a link to the completed script that accomplishes the simple single-command fade in or fade out with no dialogs. Thanks again for your efforts. Sincerely appreciated.
Jacob Rus
2007-03-05 14:43:36
I cleaned up the code a bit, and flattened it out (I can't stand this absurd deep nesting). Take a look: http://pastie.textmate.org/44906
Jacob Rus
2007-03-05 14:44:29
Also note that with that version, making it just fade in/out with no dialogs is very simple to accomplish.
Jacob Rus
2007-03-05 14:46:00
Gavin said:
I've found the problem, why it always launches iTunes...


On my computer, it keeps setting "okflag" to true, whether iTunes is running or not. Thus it always runs the full script. Annoying. So we'd better find some other way of testing...


Yes, the problem here is that a "property" of a script will get saved after the script is run, and so the next time you run the script it will still be true. See my pasted version above. That should fix the problem.
Jacob Rus
2007-03-05 14:49:28
Also, I made a nice set of iTunes controller scripts a while ago that will show growl notifications. You can find them here. I use them with quicksilver triggers set to ⌘ + number pad keys (on laptops that is ⌘ + Fn + right half of the keyboard), and they work wonderfully. No fading in or out though.
David Battino
2007-03-05 16:36:23
Jacob: Thanks for sharing the property insight and cleaning up the code. You seem to have left out a play command (or playpause), though, so the script doesn't start iTunes when it's paused.
David Battino
2007-03-05 17:00:17
Jacob's tip to replace the property with a global variable worked.


In any of the scripts above, change


property okflag : false


to


global okflag
set okflag to false


...and the script will exit if iTunes isn't running. Here's a downloadable version of the simple Fade-in/Fade-out script Gavin and Lee wanted.

Lee
2007-03-05 19:44:36
Thanks ever so much, folks.
Jacob Rus
2007-03-06 00:21:53
Just for clarity (to help everyone improve), here are some things I changed from the original script:


1. When there are multiple uses of the same type of functionality, a handler is called for, so I defined a "fade" handler. This cuts out at least one, if not two, levels of nesting. In general, if a program has more than 2-3 levels of nesting, I don't like it, as it starts to scroll off the right edge of my screen.


2. The word "the" is unnecessary in applescript, though I suppose it doesn't hurt anything.


3. when testing booleans, there's no need to specify "if «bool» is true". "if «bool»" is just fine, as "«bool» is true" should evaluate to the same thing as "«bool»".


4. uses "System Events" to get GUI events, rather than Finder or iTunes, to avoid bringing those applications to the front, and then pushing them back again.


There might be some other things too. Anyway, this version should just do the simple fade in/out, without any GUI or fuss:


http://pastie.textmate.org/44996

Richard Penny
2007-03-06 05:03:36
We do something very similar here, but actually call the script via our phones.
We use Snom 360 VOIP phones which have the options to send a web request on certain actions - such as phone pick up/phone down. This way, when ever someone picks up a phone in the office, the music fades down, until the phone is put down at which point the music returns to it's previous volume.
Hank
2007-03-06 06:46:12
This is a nice thought but I find most people always fight the tools apple provides for accomplishing things. A simple right-click on the dock icon and selecting "pause" is simple enough without all the key mapping, volume changing and having an applescript running all the time. Or if itunes is the frontmost application a simple push of the space bar also pauses it.
David Battino
2007-03-10 22:42:56
Hank:
I find most people always fight the tools apple provides for accomplishing things. A simple right-click on the dock icon and selecting "pause" is simple enough
Ah, but the journey is the reward. Look how much smarter we all are now! ;-) Also, because I keep my dock hiden, finding and Control-clicking the iTunes icon takes some concentration at a moment when I’ve just been interrupted. That’s why I prefer a key command.
Andres
2007-03-18 21:43:41
Very useful code, thank you for sharing!


Is it possible for the song to fade back in with a small delay, say 1 or 2 seconds before it was faded out?


Can you please do it?


again, thank you or sharing very useful to me

Andres
2007-03-18 21:53:36
I should add that when restoring sound it starts up at the highest level for a fraction of a second and then it starts fading in. It is quite an abrupt change if using headphones.


Does that happens to you as well?

David Battino
2007-03-18 22:41:46
@Andres: I’d be happy to refine it. But which of the three scripts are you talking about?
Andres
2007-03-19 03:22:16
David, I got the script from the above comment,


"Here's a downloadable version of the simple Fade-in/Fade-out script Gavin and Lee wanted."
David Battino | March 5, 2007 05:00 PM


thank you for your time :D

David Battino
2007-03-20 00:55:10
@Andres: You're right — there is a small glitch when iTunes starts up again. That's because I had restored the volume to the previous value after pausing playback. Adding set the sound volume to 0 right before the play line fixes that. I'll update the code examples above.


I'm not clear what you mean by "Is it possible for the song to fade back in with a small delay, say 1 or 2 seconds before it was faded out?" Can you explain?


Andres
2007-03-20 10:02:42
David, I appreciate your time, thank you.


Let's suppose you pause a song at 1:40, then the volume fades out smoothly and takes 2 seconds until volume is zero. Then when unpaused begins at 1:40 from zero volume and reaches the original volume level in 1 second. I think your code makes it begin from 1:42 and fades in until is ~1:43. What should I do to make it begin from 1:40 when fading back in?

Andres
2007-03-20 15:08:12
David,


thanks for the sound volume fix, it works but only when itunes windows is not hidden. if it is hidden it brings up the sound loud. I am running the script from quick silver.

David Battino
2007-03-20 22:40:39
@Andres: To make iTunes back up one second before fading in, add these lines before the play command:



set pausepoint to player position
if pausepoint > 1 then
set player position to (pausepoint - 1)
end if


I had to dial the stepback number down to 0.1 before I liked the effect.


I'm not sure why QuickSilver makes the music blast back in.

Andres
2007-03-21 01:40:21
Wow it works like a charm :D


Thank you for your time, Im now studying your code. I will attempt to code a script that lowers the level to certain good level immediately and then goes back smoothly to the previous volume setting. I;ll post it here to see if is useful to somebody.


again thank you

Eric Blossom
2007-03-21 19:09:13
Thanks, this has been helpful. I wrote a similar thing that when used in conjunction with crontab let's my son leave his music on when he goes to bed. This turns it down and then off at night and wakes him up in the morning. Is it possible to get iTunes to stop after the current tune completes?
David Battino
2007-03-22 02:00:04
Eric: I saw two scripts at Doug's AppleScripts for iTunes that might be able to stop playback at the end of a song the way you want. They seem to work by copying the song to a single-song playlist.
Ben
2007-06-23 07:34:17
I've been searching round for a script that can end a song on demand so that itunes will then smoothy crossfade into the next song on the playlist. Can anyone help me please? thanx!
David Battino
2007-06-24 13:11:36
@Ben:


I've been searching for a script that can end a song on demand so that iTunes will then smoothly crossfade into the next song on the playlist.


I don't think there's a way to trigger a crossfade in iTunes, but there are DJ programs that can do that. I did experiment with making iTunes jump to the last few seconds of the song, but that was jarring. (The Doug's AppleScripts for iTunes site has a script that jumps to X seconds before the end in a song.)


Probably the closest you could currently get in iTunes would be to fade the current song and then immediately start the next. To do that, substitute next track for pause in the third script above and adjust the fadeout time to taste. I found -10 often worked, but results vary if tracks have fades already.