Using iTunes with AutoHotkey_L
So I recently noticed that my code to combine AutoHotkey with iTunes wasn’t working, so tonight’s project was to get it working again. Luckily, AutoHotkey has dramatically developed since I wrote the last post, now bundling native COM support in a version that is generally known as AHK_L. For a full list of things added to AutoHotkey, consult the AHK_L Changelog.
Using COM
So the best news here is that we don’t have to mess with low-level COM coding at all; anyone who has done so will know the pain of which I speak. Using COM is one of the most degrading, mind-wrangling and time-consuming tasks in Computer Science. Thankfully, the new syntax is simple.
Playback Controls
Media_Next::
iTunes := ComObjCreate("iTunes.Application")
iTunes.NextTrack()
Return
Media_Play_Pause::
iTunes := ComObjCreate("iTunes.Application")
iTunes.PlayPause()
Return
Media_Prev::
iTunes := ComObjCreate("iTunes.Application")
iTunes.PreviousTrack()
Return
^Media_Mute::
iTunes := ComObjCreate("iTunes.Application")
iTunes.Mute := !iTunes.Mute
Return
More after the jump.
Rating Controls
^#Up::
iTunes := ComObjCreate("iTunes.Application")
iTunes.CurrentTrack.Rating += 20
Return
^#Down::
iTunes := ComObjCreate("iTunes.Application")
iTunes.CurrentTrack.Rating -= 20
Return
Copy song to Dropbox
I’ve changed the function to copy a specific file location to the Dropbox, taking the COM reference out of dropbox.ahk.
Media_Stop::
iTunes := ComObjCreate("iTunes.Application")
CopyFileToDropbox(iTunes.CurrentTrack.Location)
Return
Other useful fields/methods
Many of the following were gleaned from this file.
General
- Version:
iTunes.Version
- Go to iTunes Store:
iTunes.GotoMusicStoreHomePage()
- Enable visualization:
iTunes.VisualsEnabled := 1
- Toggle visualization:
iTunes.VisualsEnabled := !iTunes.VisualsEnabled
- Update iPod/iPhone:
iTunes.UpdateIPod()
- Path of library file:
iTunes.LibraryXMLPath
Playback
- Seek to beginning of song:
iTunes.BackTrack()
- Rewind:
iTunes.Rewind()
- Fast forward:
iTunes.FastForward()
- Volume:
iTunes.SoundVolume
- Play file:
iTunes.PlayFile(full file path)
- Open URL:
iTunes.OpenURL(url path)
- Quit:
iTunes.Quit()
Current Track
- If iTunes is currently playing:
iTunes.PlayerState
- Seconds elapsed in file:
iTunes.PlayerPosition
- Artist:
iTunes.CurrentTrack.Artist
- Name:
iTunes.CurrentTrack.Name
- Album:
iTunes.CurrentTrack.Album
- Time (total length):
iTunes.CurrentTrack.Time
- File size (bytes):
iTunes.CurrentTrack.Size
- Play count:
iTunes.CurrentTrack.PlayedCount
- Bit rate:
iTunes.CurrentTrack.BitRate
- File location:
iTunes.CurrentTrack.Location
Playlist
- Current playlist:
iTunes.CurrentPlaylist
- Library:
iTunes.LibraryPlaylist
- Selected tracks:
iTunes.SelectedTracks
- Name of a playlist:
iTunes.CurrentPlaylist.Name
- Number of tracks in a playlist:
iTunes.CurrentPlaylist.Tracks.Count
- Length of a playlist (hh:mm:ss):
iTunes.CurrentPlaylist.Time
- Size of a playlist (bytes):
iTunes.CurrentPlaylist.Size
- Name of the first track in a playlist:
iTunes.CurrentPlaylist.Tracks[1].Name
- Play first track in the current playlist:
iTunes.CurrentPlaylist.PlayFirstTrack()
Search
- Number of search results for a phrase:
iTunes.LibraryPlaylist.Search("wild things", 1).Count
Hopefully this is of use to someone. If it is, I’d like you to tell me what you used it for!
4 Comments