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!

Andrew Guyton
http://www.disavian.net/

4 Comments

Kamy

Yeah thanks a lot !

I’ve been searching for 2 days why my former script wasn’t working anymore, and I found the solution thanks to you =)

Alex

Message box to display current track’s rating.

^!?::
iTunes := ComObjCreate(“iTunes.Application”)
Rating := iTunes.CurrentTrack.Rating
Rating /= 20
Stars := “Rating: ”
Loop, %Rating%
{
Stars = %Stars%*
}
MsgBox, %Stars%
Return

pitzy

thx, i had a similar idea, but i’m lazy and i came across this post 😀

i’ve mapped a key, so that i can send to someone (using dropbox) the song i’m currently listening, so i don’t have to drag’n’drop every time the song into the dropbox folder

Drako

HI!
Thank you very much!
Do you have a way to somehow control as well the seachbox?
In Winamp I have an option that set as last the song that is being playing. Meaning that even if the playlist has more songs after the one playing, the player will finish to play the one its playing and not play anything else automatically. I’m trying to have a shortcut that does the same for iTunes and I figured that having a shortcut that sends some random text to the searchbox will make the playlist blank so itunes will simply finish to play the current song and dont play anything else.

Leave a Reply