Controlling iTunes with AutoHotkey

Skill

Controlling iTunes with AutoHotkey

Posted in:

AutoHotkey is one of those utilities that I am very grateful that I found. It has made my desktop working experience all the better - if you don't know what I am talking about check out my intro to AutoHotkey. Today I am going to go over a set of hot keys that I really like and use daily. The hot keys to control iTunes, the nice thing is that they work no matter where you're at on your computer and no matter what application your are in. I found the basic implementation of these over at the AutoHotkey forums.

The basic idea is that we will be able to do the following: open, minimize, start, stop, go to next, and go to previous. Now, the main idea here is that with AutoHotkey we can send commands to our iTunes application no matter if it is minimized or not. In order to achieve this we are going to use the AutoHotkey command ControlSend. This command simulates sending keystrokes to a control or window.

Looking at the documentation for this there are 6 parameters. Following is taken directly from the documentation page.

Control

Can be either ClassNN (the classname and instance number of the control) or the name/text of the control, both of which can be determined via Window Spy. When using name/text, the matching behavior is determined by SetTitleMatchMode. If this parameter is blank or omitted, the target window's topmost control will be used. If this parameter is ahk_parent, the keystrokes will be sent directly to the control's parent window (see Automating Winamp for an example).

To operate upon a control's HWND (window handle), leave the Control parameter blank and specify ahk_id %ControlHwnd% for the WinTitle parameter (this also works on hidden controls even when DetectHiddenWindows is Off) . The HWND of a control is typically retrieved via ControlGet Hwnd, MouseGetPos, or DllCall.

Keys

The sequence of keys to send (see the Send command for details). To send a literal comma, escape it (`,). The rate at which characters are sent is determined by SetKeyDelay.

Unlike the Send command, mouse clicks cannot be sent by ControlSend. Use ControlClick for that.

WinTitle The title or partial title of the target window (the matching behavior is determined by SetTitleMatchMode). If this and the next 3 parameters are omitted, the Last Found Window will be used. If this is the letter A and the next 3 parameters are omitted, the active window will be used. To use a window class, specify ahk_class ExactClassName (shown by Window Spy). To use a process identifier (PID), specify ahk_pid %VarContainingPID%. To use a window group, specify ahk_group GroupName. To use a window's unique ID number, specify ahk_id %VarContainingID%. The search can be narrowed by specifying multiple criteria. For example: My File.txt ahk_class Notepad
WinText If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if DetectHiddenText is ON.
ExcludeTitle Windows whose titles include this value will not be considered.
ExcludeText Windows whose text include this value will not be considered.

The only ones we care about are the first three though. These define the control to send the keystokes to, the keystrokes themselves, and finally the window of the control. For the first we pass ahk_parent (telling it to use the direct parent control of our window). The second we are going to pass the keystroke which varies for the different commands. For the third parameter we pass iTunes ahk_class iTunes which is the title iTunes and then the class name. The title and class can be gotten by using the window spy application (AU3_Spy.exe) that comes with AutoHotkey, just look in your AutoHotkey installation folder (C:\Program Files\AutoHotkey). Using this application you can click on windows and get their class and title, not to mention lots of other useful information.

So, without anymore delay here is the entire script piece that can be added to your current AutoHotkey script.

; ############## iTunes ##############

; win+right -> next song
; win+left  -> previous song
; win+space -> play/pause

#right::
DetectHiddenWindows , On
ControlSend , ahk_parent, ^{right}, iTunes ahk_class iTunes
DetectHiddenWindows , Off
return

#left::
DetectHiddenWindows , On
ControlSend , ahk_parent, ^{left}, iTunes ahk_class iTunes
DetectHiddenWindows , Off
return

#space::
DetectHiddenWindows , On
ControlSend , ahk_parent, {space}, iTunes ahk_class iTunes
DetectHiddenWindows , Off
return

#z::
DetectHiddenWindows , On
IfWinActive , iTunes ahk_class iTunes
   WinMinimize
else
   Run %ProgramFiles%\iTunes\iTunes.exe
DetectHiddenWindows , Off
return

Now above you can see that in the first three hotkeys we are just running the ControlSend command. Wrapped around the command is enabling and then disabling detection of hidden windows. This makes sure that if iTunes is minimized it will send the keystrokes to it. The last hotkey acts a little different. It will open or minimize/restore iTunes, checking first if it is running. You will also notice that all the hotkeys are bound to #key, which is the Windows key plus any other key - for pause/play it is windows + space.

That pretty much wraps it up. I hope someone out there found something useful and learned something. If anyone has any questions about AutoHotkey or anything else feel free to leave a comment. You can also download the script we created today. Also I ran across this small little application that does basically what we did today if anyone is simply looking for the end solution.

Scott Trosien
01/04/2009 - 21:53

I was inspired by your use of AutoHotKey to solve another minor problem I have:

Implementing multiple iTunes libraries to keep each of my family members libraries seperate using one login account. The idea is to keep the master song lists seperate so we don’t have to weed through 100’s of songs that are not of interest by each other.

This can easily be done by holding down the shift key when starting iTunes, but that is a hassle to remember when docking the iPod or starting iTunes so I have autohotkey detect the startup of itunes.exe and hold the shift key for 5 seconds for us! See below for sample script:

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
#InstallKeybdHook
SetKeyDelay, 20
SetTimer, KeepRunning
return

;waits for iTunes to run, holds down the shift key for 5 seconds then waits for itunes to close to resume waiting for iTunes to launch again.
;This will cause iTunes to ask for a library on startup so we can keep our master song lists separate.

KeepRunning:
Process, wait, itunes.exe
{
send {shift down}
sleep 5000
send {shift up}
Process, waitclose, itunes.exe
}
return

Enjoy! (just wrote it tonight so minimal testing has been done with it).

reply

mp3dave77
01/21/2009 - 02:37

Thanks for this guys.. been looking for something similar!

reply

Anonymous
04/07/2009 - 05:33

I found a program that has a really nice hot key function, they're user selectable and it offers a lot of features. I've tried playing around with making my own hot key program and i've even tried a few, but for the price, this program appears to be solid. Check it out, www.skinitunes.com

reply

MLReeves
08/21/2009 - 09:49

Fantastic! Thanks for posting a great solution that was simple to implement.

reply

Anonymous
11/19/2009 - 22:15

this is just wat i wanted!!! thanks so much!

reply

Anonymous
09/12/2011 - 05:54

Play/Pause option is not working. Though Next and Previous works with any possible combination..

reply

Anonymous
10/14/2011 - 00:52

For the Play/Pause, try using this code instead. Just add the carrot symbol (^) before the {space}, like this:

#space::
DetectHiddenWindows , On
ControlSend , ahk_parent, ^{space}, iTunes ahk_class iTunes
DetectHiddenWindows , Off
return

iTunes 10.5 may need a little adjustment too, so the final code should be

#space::
DetectHiddenWindows , On
ControlSend , ahk_parent, ^{space}, iTunes ahk_class ITWindow
DetectHiddenWindows , Off
return

reply

Anonymous
10/14/2011 - 01:14

neither of this works for me... im using windows 7 ultimate and the newest version of autohotkey

reply

Anonymous
10/17/2011 - 13:35

The following is my entire file. I had to tweak it slightly for iTunes 10.5.:

; iTunes Anywhere
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:        Polyphenol <myemail@nowhere.com>
;
; Script Function:
;   Control iTunes from anywhere with hotkeys
;

#NoTrayIcon
#SingleInstance force
DetectHiddenWindows, on

#\::
IfWinNotExist, ahk_class iTunes
{
Run %ProgramFiles%\iTunes\iTunes.exe  ;launch program
return
}

IfWinExist, ahk_class iTunes ; toggle minimize/restore
{
IfWinNotActive ; restores window
WinActivate
Else
WinMinimize ; minimizes windows
return
}

#NumPad6::
#3::
#Right::
Ctrl & Right::
#F12::
if WinExist("ahk_Class iTunes") or WinExist("ahk_Class ITWindow")
{
ControlSend, ahk_parent, ^{RIGHT}  ; > next
}
return

#NumPad4::
#1::
#Left::
Ctrl & Left::
#F10::
if WinExist("ahk_Class iTunes") or WinExist("ahk_Class ITWindow")
{
ControlSend, ahk_parent, ^{LEFT}  ; < previous
}
return

#NumPad5::
#2::
#F11::
#Space::
Ctrl & Space::
if WinExist("ahk_Class iTunes") or WinExist("ahk_Class ITWindow")
{
ControlSend, ahk_parent, ^{SPACE}  ; play/pause toggle
}
return

#NumPad8::
#4::
#Up::
Ctrl & Up::
if WinExist("ahk_Class iTunes") or WinExist("ahk_Class ITWindow")
{
ControlSend, ahk_parent, ^{UP}  ; volume up
}
return

#NumPad2::
#5::
#Down::
Ctrl & Down::
if WinExist("ahk_Class iTunes") or WinExist("ahk_Class ITWindow")
{
ControlSend, ahk_parent, ^{DOWN}  ; volume down
}
return

#=::
SetTimer, songpreview, 10000 ; change song every 10 sec
songpreview:
if WinExist("ahk_Class iTunes") or WinExist("ahk_Class ITWindow")
{
ControlSend, ahk_parent, ^{RIGHT}
}
return

#-::
SetTimer, songpreview, Off
return


;endofscript

reply

Anonymous
10/17/2011 - 13:36

I am on Windows 7 as well.

Thanks.

reply

yaung27
11/17/2011 - 23:08

Thanks dude! You saved me a lot of trouble.
I thought there wasn't going to be a script for the current version of iTunes.
Again, Thanks!

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.