DJ Bonus

Q: Increase / Decrease iTunes Volume Using Terminal Command Via Automator

Hi there,

 

I'm new to terminal but I would like to set up two separate Automator actions to control the volume in iTunes (not the system audio): one to increase and one to decrease, possibly over 10 seconds and perhaps with the ability to adjust if necessary.


Note: I'm fine with setting up the Automator actions etc.. and I don't won't to run it from an applescript as there are other automator actions the need to happen after the increase/decrease in volume.  I think if I ran it from an applescript, Automator would set the script running and then continue without waiting for the outcome (I may be wrong on this).. but I also think running it all within Terminal would be tidier.

 

I've done some digging around the net and apple forums.. this is what I've found:

 

I think its a osascript command, like these:

osascript -e 'tell application "iTunes" to play'

osascript -e 'tell application "iTunes" to pause'


This works for the system audio:

 

for((i=0;i<50;i++)); do

osascript -e "set volume output volume $i"

done

 

This is an applescript that works, but I couldn't figure out how get it to run over 10 seconds.. I timed it and it seemed to take about 1min 20ses.. although it says two mins and I don't really want to use an applescript. Using Automator to gradually increase volume

 

Any help or suggestions would be greatly appreciated

Posted on Oct 6, 2015 1:00 PM

Close

Q: Increase / Decrease iTunes Volume Using Terminal Command Via Automator

  • All replies
  • Helpful answers

  • by Camelot,Solvedanswer

    Camelot Camelot Oct 6, 2015 1:34 PM in response to DJ Bonus
    Level 8 (47,233 points)
    Mac OS X
    Oct 6, 2015 1:34 PM in response to DJ Bonus

    I think you're a little misguided on the details and differences between terminal scripting and AppleScript.

     

    Your terminal script:

     

    osascript -e "set volume output volume $i"

     

    IS AppleScript - 'osascript' is the terminal/shell command to invoke AppleScript.

     

    So what you're asking for is an Automator action to fire off a Terminal/shell script to invoke an AppleScript...?

     

    Why not cut out the middleman and go straight to the AppleScript?

     

    My suggestion, create an Automator workflow with a 'Run AppleScript action that contains the following:

     

    tell application "iTunes"

      play

      repeat with i from 1 to 100

      set sound volume to i

      delay 0.2

      end repeat

    end tell

     

    This will start playing your music and increase the sound level from 1 to 100 over 20 seconds (thanks to the 0.2 second delay between increments)

  • by DJ Bonus,

    DJ Bonus DJ Bonus Oct 6, 2015 1:44 PM in response to Camelot
    Level 1 (1 points)
    Oct 6, 2015 1:44 PM in response to Camelot

    Oh ok.. I thought they were two different things.. that aside.. thank you so much, I have spent hours trawling webpages, trying different things and only getting partial results... really thank you!!

  • by DJ Bonus,

    DJ Bonus DJ Bonus Oct 6, 2015 2:44 PM in response to Camelot
    Level 1 (1 points)
    Oct 6, 2015 2:44 PM in response to Camelot

    Hi,

     

    Sorry to ask, but how can I get it to decrease in the same way.. I tried switching the '1' & '100' and 'from' & to'?

     

    Also.. are there any good applescript & Terminal resources you'd recommend?

     

    thanks,

  • by Camelot,Helpful

    Camelot Camelot Oct 7, 2015 9:14 AM in response to DJ Bonus
    Level 8 (47,233 points)
    Mac OS X
    Oct 7, 2015 9:14 AM in response to DJ Bonus

    Sorry to ask, but how can I get it to decrease in the same way.. I tried switching the '1' & '100' and 'from' & to'?

     

    You have a couple of options.

     

    To use decrementing loop counters, you need to use a 'by' parameter in the form:

     

      repeat with i from 100 to 1 by -1

     

    Loop counters, by design, will increment until the counter exceeds the second value. If you use '... from 100 to 1' without the by parameter, it will stop immediately since the counter now exceeds '1'.

     

    The alternative is to use an incrementing loop counter, and just subtract it from 100:

     

      repeat with i from 1 to 100

      set sound volume to 100 - i

      delay 0.2

      end repeat


  • by DJ Bonus,

    DJ Bonus DJ Bonus Oct 7, 2015 9:40 AM in response to Camelot
    Level 1 (1 points)
    Oct 7, 2015 9:40 AM in response to Camelot

    I try to figure things out before resorting to forums.. but I can see I was on the wrong track.  I appreciate you taking the time to respond, thank you so much for your help and information, these scripts will be very useful.

     

    Thanks