; @altrun.title Media Keys ; @altrun.keyword media ; @altrun.mode filter ; @altrun.filter altrun ; @altrun.description Live results: play / pause, next and previous track, stop, mute and volume up / down, the commands ALTRun had built in before ; @altrun.description_zh 实时结果: 播放 / 暂停、下一首、上一首、停止、静音和音量增减, 也就是 ALTRun 以前内置的这几个命令 ; @altrun.network none ; ; Type "media" in ALTRun: the seven actions are listed (a few letters narrow them down, "media next" or "media 下一"); Enter runs one. ; The player keys go to the player that Windows' media controls show, as a keyboard's media keys do; the volume changes in steps of 10. ; Hotkeys made for ALTRun's former commands (Mute, VolumeUp, VolumeDown, MediaPlayPause, MediaNext, MediaPrev, MediaStop) run the ; same item here once this script is in the Scripts folder. #Requires AutoHotkey v2.0 #SingleInstance Off ; run again before the last run ended: no AutoHotkey "already running" prompt #NoTrayIcon Chinese := RegExMatch(A_Language, "i)^(0804|0404|0C04|1004|1404)$") > 0 ; [arg, English, Chinese]: the arg is what ALTRun passes back on Enter (and what its former commands map to) Items := [["play", "Play / Pause", "播放 / 暂停"], ["next", "Next Track", "下一首"], ["prev", "Previous Track", "上一首"] , ["stop", "Stop", "停止播放"], ["mute", "Mute / Unmute", "静音 / 取消静音"], ["up", "Volume Up", "增大音量"], ["down", "Volume Down", "减小音量"]] if (EnvGet("ALTRUN_ACTION") = "run") ; Enter on a result Act(A_Args.Length ? A_Args[1] : "") ListItems() ExitApp ListItems() { sound := SoundState() icon := A_WinDir "\System32\SndVol.exe" out := "" for item in Items { title := Chinese ? item[3] : item[2] subtitle := (item[1] = "mute" || item[1] = "up" || item[1] = "down") ? sound : (Chinese ? "发给 Windows 媒体控制里的播放器" : "Sent to the player in Windows' media controls") out .= (out = "" ? "" : ",`n") '{"title": "' Json(title) '", "subtitle": "' Json(subtitle) '", "arg": "' item[1] . '", "type": "script", "uid": "' item[1] '", "match": "' Json(Chinese ? title " " item[2] : title) '", "icon": "' Json(icon) '"}' } FileAppend('{"items": [' out ']}', "*", "UTF-8-RAW") } ; The volume now, for the subtitles of the sound items: "Volume 40%", "Muted (volume 40%)", or why it can't be read SoundState() { try { volume := Round(SoundGetVolume()), muted := SoundGetMute() } catch return Chinese ? "读不到音量 (没有音频设备?)" : "Can't read the volume (no sound device?)" if muted return Chinese ? "已静音 (音量 " volume "%)" : "Muted (volume " volume "%)" return Chinese ? "音量 " volume "%" : "Volume " volume "%" } ; One action; the sound ones say the new state (ALTRun shows the last line printed as a notification), the player keys say nothing Act(arg) { switch arg { case "play": Send("{Media_Play_Pause}") case "next": Send("{Media_Next}") case "prev": Send("{Media_Prev}") case "stop": Send("{Media_Stop}") case "mute", "up", "down": try { if (arg = "mute") SoundSetMute(-1) else SoundSetVolume(arg = "up" ? "+10" : "-10") } catch as e Fail((Chinese ? "不能调节音量: " : "Can't change the volume: ") e.Message) FileAppend(SoundState(), "*", "UTF-8-RAW") default: Fail((Chinese ? "不认识的操作: " : "Unknown action: ") arg) } ExitApp } Json(s) { ; text -> inside a JSON string s := StrReplace(StrReplace(s, "\", "\\"), '"', '\"') s := StrReplace(StrReplace(StrReplace(s, "`r", "\r"), "`n", "\n"), "`t", "\t") while RegExMatch(s, "[\x01-\x1F]", &c) s := StrReplace(s, c[0], Format("\u{:04X}", Ord(c[0]))) return s } Fail(message) { ; ALTRun shows the first line of standard error as the reason FileAppend(message "`n", "**", "UTF-8-RAW") ExitApp(1) }