-
Notifications
You must be signed in to change notification settings - Fork 1
/
Spotify.ps1
134 lines (95 loc) · 3.75 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
$SPOTIFY_PLAYPAUSE =917504
$SPOTIFY_MUTE = 524288
$SPOTIFY_STOP = 851968
$SPOTIFY_PREVIOUS = 786432
$SPOTIFY_NEXT = 720896
function StartSpotifyIfNeeded()
{
$programName = "Spotify"
$spotifyPath = Join-Path -Path $env:USERPROFILE -ChildPath "AppData\Roaming\Spotify\Spotify.exe"
$isRunning = (Get-Process | Where-Object { $_.Name -eq $programName }).Count -gt 0
if (-not $isRunning){
& $spotifyPath
Start-Sleep 2
}
}
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 = Get-Process |where {$_.mainWindowTitle -and $_.name -eq "Spotify"} | select mainwindowtitle
$artist = $songTitle.MainWindowTitle.Split("-")[0]
$song = $songTitle.MainWindowTitle.Split("-")[1]
Write-Host $songTitle.MainWindowTitle
# 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
switch ($option)
{
"Play" {PlayPause}
"Pause" {PlayPause}
"Replay" {
RestartSong
Start-Sleep 1
updateToast
}
"Mute" {Mute}
"Next" {
NextSong
Start-Sleep 1
updateToast
}
"Previous"{
PreviousSong
Start-Sleep 1
updateToast
}
default {"Wrong command"}
}