forked from edgiardina/Spotify-Powershell-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spotify.ps1
169 lines (126 loc) · 4.35 KB
/
Spotify.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
param([switch] $DontStartSpotify)
$SPOTIFY_PLAYPAUSE =917504
$SPOTIFY_MUTE = 524288
$SPOTIFY_STOP = 851968
$SPOTIFY_PREVIOUS = 786432
$SPOTIFY_NEXT = 720896
$SPOTIFY_PROCESS_NAME = "Spotify"
$SPOTIFY_NOTHING_PLAYING_WINDOW_TITLE = "Spotify"
function IsSpotifyRunning()
{
(Get-Process | Where-Object { $_.Name -eq $SPOTIFY_PROCESS_NAME }).Count -gt 0
}
function StartSpotifyIfNeeded()
{
if($DontStartSpotify){
return
}
$spotifyPath = Join-Path -Path $env:USERPROFILE -ChildPath "AppData\Roaming\Spotify\Spotify.exe"
if(-not $(IsSpotifyRunning)){
& $spotifyPath
Start-Sleep 2
}
}
function GetSpotifyWindowTitle()
{
$(Get-Process |where {$_.mainWindowTitle -and $_.name -eq $SPOTIFY_PROCESS_NAME} | select mainwindowtitle).MainWindowTitle
}
function IsPlaying()
{
$(GetSpotifyWindowTitle) -and $(GetSpotifyWindowTitle) -ne $SPOTIFY_NOTHING_PLAYING_WINDOW_TITLE
}
function NextSong(){
SendSpotifyCommand($SPOTIFY_NEXT);
}
function RestartSong(){
SendSpotifyCommand($SPOTIFY_PREVIOUS);
}
function PreviousSong(){
SendSpotifyCommand($SPOTIFY_PREVIOUS);
SendSpotifyCommand($SPOTIFY_PREVIOUS);
}
function Stop(){
SendSpotifyCommand($SPOTIFY_STOP);
}
function Mute(){
SendSpotifyCommand($SPOTIFY_MUTE);
}
function PlayPause(){
SendSpotifyCommand($SPOTIFY_PLAYPAUSE);
}
function SendSpotifyCommand($command){
$spotifyProcess = Get-Process | Where-Object {$_.MainWindowTitle -and $_.Name -eq "Spotify"} | Select-Object Id,Name,MainWindowHandle,MainWindowTitle
#Store the C# signature of the SendMessage function.
$signature = @"
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
"@
#Add the SendMessage function as a static method of a class
$SendMessage = Add-Type -MemberDefinition $signature -Name "Win32SendMessage" -Namespace Win32Functions -PassThru
#Invoke the SendMessage Function
$result = $SendMessage::SendMessage($spotifyProcess.MainWindowHandle, 0x0319, 0, $command)
}
function updateToast(){
# create toast template TO xml
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
[xml]$toastXml = ([Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText04)).GetXml()
$songTitle = GetSpotifyWindowTitle
$artist = $songTitle.Split("-")[0]
$song = $songTitle.Split("-")[1]
Write-Host $songTitle
# message to show on toast
$stringElements = $toastXml.GetElementsByTagName("text")
$stringElements[0].AppendChild($toastXml.CreateTextNode("Spotify")) > $null
$stringElements[1].AppendChild($toastXml.CreateTextNode($artist)) > $null
$stringElements[2].AppendChild($toastXml.CreateTextNode($song)) > $null
# no image
$imageElements = $toastXml.GetElementsByTagName("image")
$imageElements[0].src = "file:///you_can_personalize the toast icon if you want" + ""
# convert from System.Xml.XmlDocument to Windows.Data.Xml.Dom.XmlDocument
$windowsXml = New-Object Windows.Data.Xml.Dom.XmlDocument
$windowsXml.LoadXml($toastXml.OuterXml)
$APP_ID = "hoge"
$shortcutPath = [Environment]::GetFolderPath([Environment+SpecialFolder]::ApplicationData) + "\Microsoft\Windows\Start Menu\Programs\hoge.lnk";
# send toast notification
$toast = New-Object Windows.UI.Notifications.ToastNotification ($windowsXml)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($toast)
}
$option=$args[0]
StartSpotifyIfNeeded
if(-not $(IsSpotifyRunning))
{
echo "Spotify is not running. Skipping command."
return
}
switch ($option)
{
"Play" {
if(-not $(IsPlaying))
{
PlayPause
}
}
"Pause" {
if($(IsPlaying))
{
PlayPause
}
}
"Replay" {
RestartSong
Start-Sleep 1
updateToast
}
"Mute" {Mute}
"Next" {
NextSong
Start-Sleep 1
updateToast
}
"Previous"{
PreviousSong
Start-Sleep 1
updateToast
}
default {"Wrong command"}
}