Controling volume based on the name of a tab in the taskbar - vbscript

I'm new to programming and as a first exercise I wanted to write a program that would mute the volume when the taskbar displays an element with a certain name.
Full disclosure, this is to mute out the ads on Spotify when they come on. I noticed the name of the tab changes to "Spotify - Spotify". When this happens, I'd like the program to mute the volume until the name changes again. Granted I could simply pay the (quite low) monthly subscription, I really wouldn't learn anything from a programming standpoint.
Can anyone point me in the right direction to get started on this?
I've already found this on SO but not exactly what I'm looking for (I think): How to control Windows system volume using JScript or VBScript?
Thanks all.

If you are ok with muting everything you could try to get all the tasks via the word object (i know...) as described here:
Set objWord = CreateObject("Word.Application")
Set colTasks = objWord.Tasks
For Each objTask in colTasks
If objTask.Visible Then
Wscript.Echo objTask.Name
End If
Next
objWord.Quit
You could put the inner for in a loop and check objTask.Name for "Spotify - Spotify" every second and sleep in between. If you find it you can use the sendkeys solution from the post you already found
var oShell = new ActiveXObject("WScript.Shell");
oShell.SendKeys(String.fromCharCode(0xAD));
to mute the whole system.

Related

Possible for VBS to sendkeys while a full screen application is above it?

Sorry if this seems like a dumb question but im curious to see if this would work?
So my plan was to create a .vbs script to send keys to go to a website, but my plan was to have a blank black screen over top of it so you couldnt see the process. Obviously it would not be hard to make the full black screen (it would be done in a different language, most likely an exe file) but then I completely forgot... would it even be possible? I tested it out with other full screen programs and obviously it sent the "sendkeys" command to the program that was in front.
Still confused? Here is an example
Set WshShell = CreateObject("WScript.shell") for i = 0 to 50 WshShell.SendKeys(chr(175)) next Process.Start("CMD", "/C start chrome.exe http://www.bing.com")
WScript.Sleep 2000
WshShell.SendKeys "this is an example of text being put into the search bar"
So my question is, would it be possible to have a fullscreen application open while this command runs in the background? it could be any fullscreen application, just anything to "hide" or cover this process.
Or would it even be possible to create a fullscreen program specifically to cover this?
Thank you for reading!

Active IE 11 window for SendKeys

Problem : I am trying to automate the saving of a file (manage to save the file when the below IE bar appears at the bottom of the page)
My code is going on my intranet, clicking here and there and then I click on an "Export" button which will trigger this from IE :
I didn't manage to find a way to automate the saving of the file because the only way (I think) to interact with this "window" is to use SendKeys.
In order to do so I have to "activate" this window (Yes, I have it activated for the HTML scraping with this bit of code, but it's not the active window though):
I tried using AppActivate but for some reason it doesn't work.
There are 2 options to pass this obstacle :
Find a way to activate the IE window containing that saving bar so that I can use Application.SendKeys "%{S}" on it
(second option only) : Disable this saving bar or manage to have it open in a new window
So far I have gone through lots of posts talking about that subject but none gave an operational solution for that issue on IE 11.
Let me know if you want to see any bit of code, I have a lot of different attempts gathered from different posts but this would highly increase the length of this post.
Front-end automation is a tricky business. It can be really hard to find a solution that always works (those pesky users are free to move the mouse and click buttons at will, disrupting your code). Because you are taking the data from an intranet site, this suggests the data you need already exists within your organisation. If at all possible (and I know it isn't always) take the data from the servers/source systems, rather than via the UI.
AppActivate and SendKeys can be very fussy. Because the url is always the same a better approach would be to directly download it. Here is a example, based on another answer.
Before you can run code you will need to add two references:
Microsoft XML, v6.0
Microsoft ActiveX Data Objects 2.8 Library
From the VBA IDE click Tools >> References... and select from the list (the original answer does not use references, instead it uses late binding).
' Downloads a file from a given URL.
' URL String URL to get file. Ex: http:\\MySite\MyFile.
' Path String Location to save file. Ex: C:\Myfile.csv.
' Requires: Microsoft XML, v6.0
' Requires: Microsoft ActiveX Data Objects 2.8 Library.
Sub DownloadURL(ByVal URL As String, ByVal Path As String)
Dim httpXML As XMLHTTP60 ' Used to download data.
Dim st As ADODB.Stream ' Used to write downloaded data to file system.
Set httpXML = New XMLHTTP60
' Request file.
httpXML.Open "GET", URL, False
httpXML.send
' Download response.
If httpXML.Status = 200 And httpXML.readyState = 4 Then
Set st = New ADODB.Stream
st.Open
st.Type = adTypeBinary
st.Write httpXML.responseBody
st.SaveToFile Path, adSaveCreateOverWrite
st.Close
Else
MsgBox "Something went wrong :(", vbCritical, "An error occured"
End If
End Sub

How to use VBS to program 1) windows media player? 2) your computer in general?

I would like to open my video using VBS and So far I have this and it works fine
Set wmp = CreateObject("WMPlayer.OCX")
wmp.openPlayer("C:\Users\myvideo.mp4")
I would also like to add one more line to make it full screen. How would one do that?
Also, my second question is probably more useful.
Is there a collective website to describe how to manipulate these objects?
When I played around with Matlab, I can at least press tab and see what can options are available under that object and I learned a lot that way (the website was also very helpful).
Is there a comparable way in VBS or for example, where can I find a extensive lists of command I could send to WM Player?
Thanks!
For switching the window to a full screen window you have to use Alt+Enter. This can be done using sendKeys method of the Shell object. Here's the code:
set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 1000
WshShell.SendKeys "%{ENTER}"
Hope this suits you need. For further help check the documentation for SendKeys.

Excel VBA - Is there a TextChanging / TextChanged or a similar event? Or how to move a selection without using Enter?

I have recently been asked if I could make a macro in Excel VBA that will allow a user to type in two numbers and have it automatically drop to the next row. The purpose of this is so they can type in grades for a test two numbers at a time without pressing enter since they aren't great at typing.
When I first heard this he mentioned it was Visual Basic, so I figured I'd just use a TextChanging or TextChanged event in the cell range and have it work off that. However, I haven't been able to find any such event or anything resembling it in the documentation thus far. The first thing that I came across was Workbook_Change, but that only changes after you press enter which is useless to me. Someone else mentioned there is such an event, but couldn't name it directly and I haven't been able to find what they were talking about.
If anyone has any information on if such an event exists or is possible I'd love to know.
The Excel version is 2007 as far as I'm aware.
This, in my opinion, requires a non-programming solution. I absolutely sympathize - it is tough to watch people get old - but you have to draw the line somewhere - for their sake and yours. The enter key is the most basic part of a computer. You could probably write a macro that would automatically hit enter on every even(or odd depending) keystroke in excel - but you're going to run into other problems like not being able to use delete normally. And what if you do want to put a string of text in a cell(like the student's name)? Perhaps it is time to find a non-programming solution. By that I mean someone should have a candid conversation with him about how he wants to solve the problem. Personally, I would offer to type the numbers in for him, as I am accustomed to the number pad - but it is probably better to be more direct and start to discuss retirement.
See this discussion about the limitations of cell edit mode in excel:
http://www.mrexcel.com/forum/excel-questions/524860-call-macro-every-keystroke.html
If you're really heart-set on a programming solution, I would recommend some kind of keystroke logging add-in.
Good Luck.
You could use the Worksheet_SelectionChange event. It is triggered without enter, but it would be triggered a lot.
You could however also create a special user-form for typing in the data, but this might be more work than necessary.
The main problem with using my suggested event is, you will need it as trigger and trigger it yourself, when selecting the next row, so disable event handling before changing the selection.
Edit:
This is a quick solution (paste this into the vba-code of the desired worksheet):
Private Const clngColumnRightToLastGrade = 5
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column = clngColumnRightToLastGrade Then
Application.EnableEvents = False
'offset selection, one row down, two cols to left
Target.Offset(1, -2).Select
Application.EnableEvents = True
End If
End Sub
This will set you one row down and to column C, everytime your selection changes to column E (=5).
You don't have to use a constant of course, you could specify the column to sense in the workbook, so your user might modify it easier by himself.
To make this as an optional feature, you could extend it to autogenerated code. What I have in mind is like a Ribbon-Button, which opens a setupForm to configure, and a Ribbon-Button to activate the configuration, which would place this code in the configured sheet. But this might be a bit over the top.
In Excel 2003, (may be different in Excel2007 ?!) the WorkSheet_Change event is triggered every time the value of a cell is changed wether it is by pressing enter, delete, selecting an other cell after modifying a cell or even when a vba script changes the value of a cell.
I would do something like that:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RefRange As Range
Set RefRange = Intersect(ActiveSheet.Columns("??????????"), ActiveSheet.UsedRange)
If Not Intersect(Target, RefRange) Is Nothing Then
Target.Offset(0, 1).EntireColumn.Range("A1").Select
'Target.Offset(0, 1).EntireColumn.Range("A65536").End(xlUp).Offset(1,0).Select
End If
End Sub

vbscript delete folder containing multiple files with progress bar

was hoping their might be a way to visualize progress of the deletion of several files, I have an application that runs a cleanup when it's done, the directory it deletes is almost 3GB, so it would be nice to have a progress bar popup similar to the one that shows if you use the
Const FOF_CREATEPROGRESSDLG = &H0&
strTargetFolder = "C:\OfficeTemp"
Set oShell = CreateObject("Shell.Application")
Set objFolder = oShell.NameSpace(strTargetFolder)
objFolder.CopyHere "OfficeTemp\*.*", FOF_CREATEPROGRESSDLG
supposedly you can implement this with SHFileOperation, but I only see examples for using this in C++, anyone ever done this with VBScript?
C++ Win32 API Delete file with progress bar
My advise is don't do it if you want to keep yout script agile.
It's not the size in GB that takes long to delete, so for a couple of large files it's not suitable since before you get up your gui up and running and display some progress your filedelition allready could be done. You culd just show the filename as it is being deleted.
If it are many many small files that take longer the progress bar would be more suitable, only you need to do it in IE or another browser that you can script to and the result will never be very reliable nor beautifull. I've seen ActiveX objects that give such progress bar but even when you could use these you have problems. You need to know in advance how many files there are to be deleted and the divide the process in small steps and show the progress as a percentage of the total.
This alone could take as long as the deletion itself, vbscript is very slow in handling files.
Showing the files here would certainly slow down the process, you could show something like
1000 files deleted..
2000 files deleted..
so that the user knows there is something happening.
The fastest way do delete the map is by shelling out en let de OS take care of it, then wait for the process to end and resume the script from there.
For some of these aproaches i have samples, sorry that i can't give you and easy allproblemssolved answer.
For people on the look for a progressbar while copying, i found this in meanwhile, tested and working on Win7
Const FOF_CREATEPROGRESSDLG = &H0&
strTargetFolder = "D:\Scripts"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(strTargetFolder)
objFolder.CopyHere "C:\Scripts\*.*", FOF_CREATEPROGRESSDLG

Resources