Is it possible to launch and use Itunes with the command line on windows?

Solution 1:

While iTunes might not be controlled directly from within the Windows command line directly, using either VBScripts or PowerShell scripts it might get tamed though.

The following list of PowerShell code was compiled from the above mentioned links to also showcase how playlists can be selected and songs rated.

# Inspired by 
# - https://gist.github.com/rkumar/503162 
# - http://samsoft.org.uk/iTunes/scripts.asp

# Search for iTunes COM object
Get-CimInstance Win32_COMSetting | Select-Object ProgId, Caption | Where-Object Caption -ILike "*itunes*"
# Initializing itunes
$itunes = New-Object -ComObject iTunes.Application

# list methods and properties
$itunes | Get-Member
$itunes.CurrentPlaylist | Get-Member
$itunes.LibraryPlaylist | Get-Member
$itunes.CurrentTrack | Get-Member

# commands
$itunes.Play()
$itunes.NextTrack()
$itunes.PreviousTrack()
$itunes.PlayPause()
$itunes.Resume()
$itunes.Stop()

# changing properties
$itunes.SoundVolume = 50 # sound volume to 50%
$itunes.Mute = 0 # mute
$itunes.Mute = 1 # unmute
$itunes.CurrentPlaylist.Shuffle = 0 # shuffle off
$itunes.CurrentPlaylist.Shuffle = 1 # shuffle on
$itunes.CurrentPlaylist.SongRepeat = 0 # repeat none
$itunes.CurrentPlaylist.SongRepeat = 1 # repeat one
$itunes.CurrentPlaylist.SongRepeat = 2 # repeat all

$itunes.CurrentTrack.Rating = 80 # 100 = 5 stars, 80 = 4 stars, 60 = 3 stars, 40 = 2 stars, 20 = 1 star
$itunes.CurrentTrack.AlbumRating = 80

# Retrieve values
$itunes.CurrentPlaylist.Name
$itunes.CurrentPlaylist.Index

# List available playlists
$itunes.Sources.Item(1).Playlists

# Select playlist and start playing
$playlists = $itunes.Sources.Item(1).Playlists
$selPlaylist = $playlists.ItemByName("Name of your playlist")
If ($selPlaylist -ne $null) { $selPlaylist.Reveal(); $selPlaylist.PlayFirstTrack() }

These options allow to bind certain functions to macros to control iTunes i.e. from within VoiceAttack or mouse/keyboard macros.


How to just add song to library? Or even sync iphone?

In PowerShell if you'd enter the following commands

Get-CimInstance Win32_COMSetting | Select-Object ProgId, Caption | Where-Object Caption -ILike "*itunes*"
$itunes = New-Object -ComObject iTunes.Application
$itunes | Get-Member

you should see an output similar to this:

   TypeName: System.__ComObject#{9dd6680b-3edc-40db-a771-e6fe4832e34a}

Name                               MemberType            Definition
----                               ----------            ----------
Authorize                          Method                void Authorize (int, Variant, string)
BackTrack                          Method                void BackTrack ()
CheckVersion                       Method                bool CheckVersion (int, int)
ConvertFile                        Method                IITOperationStatus ConvertFile (string)
ConvertFile2                       Method                IITConvertOperationStatus ConvertFile2 (string)
ConvertFiles                       Method                IITOperationStatus ConvertFiles (Variant)
ConvertFiles2                      Method                IITConvertOperationStatus ConvertFiles2 (Variant)
ConvertTrack                       Method                IITOperationStatus ConvertTrack (Variant)
ConvertTrack2                      Method                IITConvertOperationStatus ConvertTrack2 (Variant)
ConvertTracks                      Method                IITOperationStatus ConvertTracks (Variant)
ConvertTracks2                     Method                IITConvertOperationStatus ConvertTracks2 (Variant)
CreateEQPreset                     Method                IITEQPreset CreateEQPreset (string)
CreateFolder                       Method                IITPlaylist CreateFolder (string)
CreateFolderInSource               Method                IITPlaylist CreateFolderInSource (string, Variant)
CreatePlaylist                     Method                IITPlaylist CreatePlaylist (string)
CreatePlaylistInSource             Method                IITPlaylist CreatePlaylistInSource (string, Variant)
FastForward                        Method                void FastForward ()
GetITObjectByID                    Method                IITObject GetITObjectByID (int, int, int, int)
GetITObjectPersistentIDs           Method                void GetITObjectPersistentIDs (Variant, int, int)
GetPlayerButtonsState              Method                void GetPlayerButtonsState (bool, ITPlayButtonState, bool)
GotoMusicStoreHomePage             Method                void GotoMusicStoreHomePage ()
NextTrack                          Method                void NextTrack ()
OpenURL                            Method                void OpenURL (string)
Pause                              Method                void Pause ()
Play                               Method                void Play ()
PlayerButtonClicked                Method                void PlayerButtonClicked (ITPlayerButton, int)
PlayFile                           Method                void PlayFile (string)
PlayPause                          Method                void PlayPause ()
PreviousTrack                      Method                void PreviousTrack ()
Quit                               Method                void Quit ()
Resume                             Method                void Resume ()
Rewind                             Method                void Rewind ()
SetOptions                         Method                void SetOptions (int)
Stop                               Method                void Stop ()
SubscribeToPodcast                 Method                void SubscribeToPodcast (string)
UpdateIPod                         Method                void UpdateIPod ()
UpdatePodcastFeeds                 Method                void UpdatePodcastFeeds ()
CanSetShuffle                      ParameterizedProperty bool CanSetShuffle (Variant) {get}
CanSetSongRepeat                   ParameterizedProperty bool CanSetSongRepeat (Variant) {get}
ITObjectPersistentIDHigh           ParameterizedProperty int ITObjectPersistentIDHigh (Variant) {get}
ITObjectPersistentIDLow            ParameterizedProperty int ITObjectPersistentIDLow (Variant) {get}
AppCommandMessageProcessingEnabled Property              bool AppCommandMessageProcessingEnabled () {get} {set}
BrowserWindow                      Property              IITBrowserWindow BrowserWindow () {get}
ConvertOperationStatus             Property              IITConvertOperationStatus ConvertOperationStatus () {get}
CurrentEncoder                     Property              IITEncoder CurrentEncoder () {get} {set}
CurrentEQPreset                    Property              IITEQPreset CurrentEQPreset () {get} {set}
CurrentPlaylist                    Property              IITPlaylist CurrentPlaylist () {get}
CurrentStreamTitle                 Property              string CurrentStreamTitle () {get}
CurrentStreamURL                   Property              string CurrentStreamURL () {get}
CurrentTrack                       Property              IITTrack CurrentTrack () {get}
CurrentVisual                      Property              IITVisual CurrentVisual () {get} {set}
Encoders                           Property              IITEncoderCollection Encoders () {get}
EQEnabled                          Property              bool EQEnabled () {get} {set}
EQPresets                          Property              IITEQPresetCollection EQPresets () {get}
EQWindow                           Property              IITWindow EQWindow () {get}
ForceToForegroundOnDialog          Property              bool ForceToForegroundOnDialog () {get} {set}
FullScreenVisuals                  Property              bool FullScreenVisuals () {get} {set}
LibraryPlaylist                    Property              IITLibraryPlaylist LibraryPlaylist () {get}
LibrarySource                      Property              IITSource LibrarySource () {get}
LibraryXMLPath                     Property              string LibraryXMLPath () {get}
Mute                               Property              bool Mute () {get} {set}
PlayerPosition                     Property              int PlayerPosition () {get} {set}
PlayerPositionMS                   Property              int PlayerPositionMS () {get} {set}
PlayerState                        Property              ITPlayerState PlayerState () {get}
SelectedTracks                     Property              IITTrackCollection SelectedTracks () {get}
SoundVolume                        Property              int SoundVolume () {get} {set}
SoundVolumeControlEnabled          Property              bool SoundVolumeControlEnabled () {get}
Sources                            Property              IITSourceCollection Sources () {get}
Version                            Property              string Version () {get}
Visuals                            Property              IITVisualCollection Visuals () {get}
VisualsEnabled                     Property              bool VisualsEnabled () {get} {set}
VisualSize                         Property              ITVisualSize VisualSize () {get} {set}
Windows                            Property              IITWindowCollection Windows () {get}

Here, you could i.e. retrieve the current playlist like this:

$itunes.CurrentPlaylist()

Name            : Lukas Graham
Index           : 25
sourceID        : 71
playlistID      : 13135
trackID         : 0
TrackDatabaseID : 0
Kind            : 2
Source          : System.__ComObject
Duration        : 7050
Shuffle         : True
Size            : 253446516
SongRepeat      : 0
Time            : 1:57:30
Visible         : True
Tracks          : System.__ComObject
Shared          : False
Smart           : False
SpecialKind     : 0
Parent          :

You can further check what members this object supports on appending | Get-Member:

$itunes.CurrentPlaylist() | Get-Member


   TypeName: System.__ComObject#{0a504ded-a0b5-465a-8a94-50e20d7df692}

Name            MemberType Definition
----            ---------- ----------
AddFile         Method     IITOperationStatus AddFile (string)
AddFiles        Method     IITOperationStatus AddFiles (Variant)
AddTrack        Method     IITTrack AddTrack (Variant)
AddURL          Method     IITURLTrack AddURL (string)
CreateFolder    Method     IITPlaylist CreateFolder (string)
CreatePlaylist  Method     IITPlaylist CreatePlaylist (string)
Delete          Method     void Delete ()
GetITObjectIDs  Method     void GetITObjectIDs (int, int, int, int)
PlayFirstTrack  Method     void PlayFirstTrack ()
Print           Method     void Print (bool, ITPlaylistPrintKind, string)
Reveal          Method     void Reveal ()
Search          Method     IITTrackCollection Search (string, ITPlaylistSearchField)
Duration        Property   int Duration () {get}
Index           Property   int Index () {get}
Kind            Property   ITPlaylistKind Kind () {get}
Name            Property   string Name () {get} {set}
Parent          Property   IITUserPlaylist Parent () {get} {set}
playlistID      Property   int playlistID () {get}
Shared          Property   bool Shared () {get} {set}
Shuffle         Property   bool Shuffle () {get} {set}
Size            Property   double Size () {get}
Smart           Property   bool Smart () {get}
SongRepeat      Property   ITPlaylistRepeatMode SongRepeat () {get} {set}
Source          Property   IITSource Source () {get}
sourceID        Property   int sourceID () {get}
SpecialKind     Property   ITUserPlaylistSpecialKind SpecialKind () {get}
Time            Property   string Time () {get}
TrackDatabaseID Property   int TrackDatabaseID () {get}
trackID         Property   int trackID () {get}
Tracks          Property   IITTrackCollection Tracks () {get}
Visible         Property   bool Visible () {get}

So adding a file to the playlist can be achived by simply invoking the AddFile(string) operation on the CurrentPlaylist() oject, i.e. like this:

$itunes.CurrentPlaylist().AddFile("D:\Music\iTunes\iTunes Media\Music\Jason Mraz\Mr. A–Z\03 Geek in the pink.mp3")

InProgress Tracks
---------- ------
     False System.__ComObject

In regards to synchronization, I haven't used my IPod in a while so haven't used synchronization this way myself yet. I've now dig up my IPod and checked the API really quickly for it and have found my IPod this way:

$itunes.Sources.Item(4)

Name            : Roman's IPod Shuffle
Index           : 4
sourceID        : 14036
playlistID      : 0
trackID         : 0
TrackDatabaseID : 0
Kind            : 2
Capacity        : 2017361920
FreeSpace       : 1128808448
Playlists       : System.__ComObject
SoftwareVersion : 1.0.1

A lookup of the respective members lists the following methods and properties for my IPod Shuffle:

$itunes.Sources.Item(4) | Get-Member


   TypeName: System.__ComObject#{cf4d8ace-1720-4fb9-b0ae-9877249e89b0}

Name            MemberType Definition
----            ---------- ----------
EjectIPod       Method     void EjectIPod ()
GetITObjectIDs  Method     void GetITObjectIDs (int, int, int, int)
UpdateIPod      Method     void UpdateIPod ()
Capacity        Property   double Capacity () {get}
FreeSpace       Property   double FreeSpace () {get}
Index           Property   int Index () {get}
Kind            Property   ITSourceKind Kind () {get}
Name            Property   string Name () {get} {set}
playlistID      Property   int playlistID () {get}
Playlists       Property   IITPlaylistCollection Playlists () {get}
SoftwareVersion Property   string SoftwareVersion () {get}
sourceID        Property   int sourceID () {get}
TrackDatabaseID Property   int TrackDatabaseID () {get}
trackID         Property   int trackID () {get}

The UpdateIPod() method here might be what you are looking for, though I haven't tested is yet.