Skip to content

Instantly share code, notes, and snippets.

@phaseOne
Last active February 9, 2016 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phaseOne/d214ee685e6b4e036b8a to your computer and use it in GitHub Desktop.
Save phaseOne/d214ee685e6b4e036b8a to your computer and use it in GitHub Desktop.
AppleScript for controlling Airfoil Volume/Mute from the command line with Karabiner
(* pass parameters to the script from the terminal to determine action *)
-- $ osascript AirfoilVolumeControl.scpt up
-- $ osascript AirfoilVolumeControl.scpt down
-- $ osascript AirfoilVolumeControl.scpt mute
(* muting requires enabling assistive access for the parent process since there's no AppleScript method for muting Airfoil without storing persistent variables *)
(* you can also remove the 'on run action' section of the script. This still allows you to invoke the adjustVolume function to directly manipulate the volume level *)
on run action
set action to (action as string)
if action is "mute" then
tell application "System Events" to tell process "Airfoil"
click menu item "Mute" of menu 1 of menu bar item "Speakers" of menu bar 1
end tell
else if action is "up" then
adjustVolume(0.02)
else if action is equal to "down" then
adjustVolume(-0.02)
end if
end run
on adjustVolume(change)
tell application "Airfoil"
set activeSpeakers to every speaker whose connected is true
repeat with thisSpeaker in activeSpeakers
set currentVolume to volume of thisSpeaker
set (volume of thisSpeaker) to (currentVolume + change)
end repeat
end tell
end adjustVolume
<?xml version="1.0"?>
<root>
<vkopenurldef>
<name>KeyCode::VK_OPEN_URL_Airfoil_Volume_Up</name>
<url type="shell">
<![CDATA[ osascript /AirfoilVolumeControl.scpt up ]]>
</url>
</vkopenurldef>
<vkopenurldef>
<name>KeyCode::VK_OPEN_URL_Airfoil_Volume_Down</name>
<url type="shell">
<![CDATA[ osascript /AirfoilVolumeControl.scpt down ]]>
</url>
</vkopenurldef>
<vkopenurldef>
<name>KeyCode::VK_OPEN_URL_Airfoil_Volume_Mute</name>
<url type="shell">
<![CDATA[ osascript /AirfoilVolumeControl.scpt mute ]]>
</url>
</vkopenurldef>
<item>
<name>Airfoil Volume Controls</name>
<appendix>Left Control + Volume Up turns up active speakers</appendix>
<appendix>Left Control + Volume Down turns down active speakers</appendix>
<appendix>Left Control + Volume Mute mutes/unmutes active speakers</appendix>
<identifier>private.airfoil.volume</identifier>
<autogen>__KeyToKey__ ConsumerKeyCode::VOLUME_UP, ModifierFlag::CONTROL_L,
KeyCode::VK_OPEN_URL_Airfoil_Volume_Up</autogen>
<autogen>__KeyToKey__ ConsumerKeyCode::VOLUME_DOWN, ModifierFlag::CONTROL_L,
KeyCode::VK_OPEN_URL_Airfoil_Volume_Down</autogen>
<autogen>__KeyToKey__ ConsumerKeyCode::VOLUME_MUTE, ModifierFlag::CONTROL_L,
KeyCode::VK_OPEN_URL_Airfoil_Volume_Mute</autogen>
</item>
</root>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment