Discussion:
Script in Windows 7
(too old to reply)
emf
2014-11-20 04:38:19 UTC
Permalink
I my old machine I had a file to toggle between 2 themes, black.thmeme
and white.theme, by clicking at a desktop link to the file. I think I've
made the necessary adjustments, but the script does not work well in
Windows 7. Here is the code:

===================================
Set WshShell = WScript.CreateObject("WScript.shell")

theme1 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
theme2 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"

'Theme names
themename1 = mid(Theme1,instrrev(theme1,"\")+1)
themename2 = mid(Theme2,instrrev(theme2,"\")+1)

'Check current theme
on error resume next
themekey =
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\CurrentTheme"
currenttheme = wshshell.regread(themekey)
currentthemename = mid(currenttheme,instrrev(currenttheme,"\")+1)
on error goto 0

'Choose theme to apply
If trim(lcase(currentthemename)) = trim(lcase(themename1)) then
theme = theme2
thememessage = left(themename2,instrrev(themename2,".")-1)
else
theme = theme1
thememessage = left(themename1,instrrev(themename1,".")-1)
end if

'Apply the theme
runstring = chr(34) & theme & chr(34)
WshShell.Run runstring

'Activate the window
counter = 600
activated = false
do
activated = WshShell.AppActivate("Display Properties")
wscript.sleep 100
counter = counter - 1
if counter = 0 then
wscript.quit
end if
loop until activated = true

wshshell.sendkeys "{ENTER}"
===================================

The major issue is that though when the current theme is not
black.theme, it correctly changes the theme to black.theme; but when the
theme is black.theme, it does not change it to white.theme. I suspect
that the condition

trim(lcase(Currentthemename)) = trim(lcase(Themename1))

in the if-else is always false, so that it always runs the else commands.

How can I correct this?

Thanks,

emf
Dave "Crash" Dummy
2014-11-20 15:25:44 UTC
Permalink
Post by emf
I my old machine I had a file to toggle between 2 themes, black.thmeme
and white.theme, by clicking at a desktop link to the file. I think I've
made the necessary adjustments, but the script does not work well in
===================================
Set WshShell = WScript.CreateObject("WScript.shell")
theme1 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
theme2 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
'Theme names
themename1 = mid(Theme1,instrrev(theme1,"\")+1)
themename2 = mid(Theme2,instrrev(theme2,"\")+1)
'Check current theme
on error resume next
themekey =
"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\CurrentTheme"
currenttheme = wshshell.regread(themekey)
currentthemename = mid(currenttheme,instrrev(currenttheme,"\")+1)
on error goto 0
'Choose theme to apply
If trim(lcase(currentthemename)) = trim(lcase(themename1)) then
theme = theme2
thememessage = left(themename2,instrrev(themename2,".")-1)
else
theme = theme1
thememessage = left(themename1,instrrev(themename1,".")-1)
end if
'Apply the theme
runstring = chr(34) & theme & chr(34)
WshShell.Run runstring
'Activate the window
counter = 600
activated = false
do
activated = WshShell.AppActivate("Display Properties")
wscript.sleep 100
counter = counter - 1
if counter = 0 then
wscript.quit
end if
loop until activated = true
wshshell.sendkeys "{ENTER}"
===================================
The major issue is that though when the current theme is not
black.theme, it correctly changes the theme to black.theme; but when the
theme is black.theme, it does not change it to white.theme. I suspect
that the condition
trim(lcase(Currentthemename)) = trim(lcase(Themename1))
in the if-else is always false, so that it always runs the else commands.
How can I correct this?
Thanks,
emf
As a wild guess, I'd say the problem was in changing registry values. I
always store variables like that in a small text file.
--
Crash

Atheism is a matter of faith, too.
Evertjan.
2014-11-20 16:36:05 UTC
Permalink
Post by Dave "Crash" Dummy
Post by emf
trim(lcase(Currentthemename)) = trim(lcase(Themename1))
in the if-else is always false, so that it always runs the else commands.
How can I correct this?
Thanks,
emf
As a wild guess, I'd say the problem was in changing registry values. I
always store variables like that in a small text file.
Wouldn't it be prudent to do some debugging and show us the values of the
variables that are compared?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dave "Crash" Dummy
2014-11-20 17:56:24 UTC
Permalink
Post by Evertjan.
Post by Dave "Crash" Dummy
Post by emf
trim(lcase(Currentthemename)) = trim(lcase(Themename1))
in the if-else is always false, so that it always runs the else commands.
How can I correct this?
Thanks,
emf
As a wild guess, I'd say the problem was in changing registry values. I
always store variables like that in a small text file.
Wouldn't it be prudent to do some debugging and show us the values of the
variables that are compared?
Probably, but simpler code is easier to debug. Here is a simple script
that will
toggle the OP's theme, as desired.

'------------------------------------------themes.vbs-------------------------------------------------------
theme1 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
theme2 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"

with CreateObject("Scripting.FileSystemObject")
if .fileExists("theme.txt") then
set f=.getFile("theme.txt").OpenAsTextStream(1)
theme=f.readAll
theme=not theme
f.close
set f=.getFile("theme.txt").OpenAsTextStream(2)
f.writeLine theme
else
set f=.CreateTextFile("theme.txt")
theme=0
f.writeLine theme
f.close
end if
end with

if theme then
runstring=theme1
else
runstring=theme2
end if

CreateObject("Wscript.Shell").Run runstring
--
Crash

All this time I thought my analyst was saying I'm psychic...
Evertjan.
2014-11-20 18:49:23 UTC
Permalink
Post by Dave "Crash" Dummy
Post by Evertjan.
Post by Dave "Crash" Dummy
Post by emf
trim(lcase(Currentthemename)) = trim(lcase(Themename1))
in the if-else is always false, so that it always runs the else commands.
How can I correct this?
Thanks,
emf
As a wild guess, I'd say the problem was in changing registry values.
I always store variables like that in a small text file.
Wouldn't it be prudent to do some debugging and show us the values of
the variables that are compared?
Probably, but simpler code is easier to debug. Here is a simple script
that will
toggle the OP's theme, as desired.
'------------------------------------------themes.vbs--------------------
----------------------------------- theme1 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
theme2 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
with CreateObject("Scripting.FileSystemObject")
if .fileExists("theme.txt") then
set f=.getFile("theme.txt").OpenAsTextStream(1)
theme=f.readAll
theme=not theme
f.close
set f=.getFile("theme.txt").OpenAsTextStream(2)
f.writeLine theme
else
set f=.CreateTextFile("theme.txt")
theme=0
f.writeLine theme
f.close
end if
end with
if theme then
runstring=theme1
else
runstring=theme2
end if
CreateObject("Wscript.Shell").Run runstring
Why not just:

themeBlack =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"

themeWhite =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"

switchFile = "themeIsWhite.txt"

with CreateObject("Scripting.FileSystemObject")
if .fileExists(switchFile) then
set f = .DeleteFile(switchFile)
runstring = themeWhite
else
set f = .CreateTextFile(switchFile)
runstring = themeBlack
end if
f.close
end with
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dave "Crash" Dummy
2014-11-20 19:25:54 UTC
Permalink
Post by Evertjan.
Post by Dave "Crash" Dummy
Post by Evertjan.
Post by Dave "Crash" Dummy
Post by emf
trim(lcase(Currentthemename)) = trim(lcase(Themename1))
in the if-else is always false, so that it always runs the else commands.
How can I correct this?
Thanks,
emf
As a wild guess, I'd say the problem was in changing registry values.
I always store variables like that in a small text file.
Wouldn't it be prudent to do some debugging and show us the values of
the variables that are compared?
Probably, but simpler code is easier to debug. Here is a simple script
that will
toggle the OP's theme, as desired.
'------------------------------------------themes.vbs--------------------
----------------------------------- theme1 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
theme2 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
with CreateObject("Scripting.FileSystemObject")
if .fileExists("theme.txt") then
set f=.getFile("theme.txt").OpenAsTextStream(1)
theme=f.readAll
theme=not theme
f.close
set f=.getFile("theme.txt").OpenAsTextStream(2)
f.writeLine theme
else
set f=.CreateTextFile("theme.txt")
theme=0
f.writeLine theme
f.close
end if
end with
if theme then
runstring=theme1
else
runstring=theme2
end if
CreateObject("Wscript.Shell").Run runstring
themeBlack =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
themeWhite =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
switchFile = "themeIsWhite.txt"
with CreateObject("Scripting.FileSystemObject")
if .fileExists(switchFile) then
set f = .DeleteFile(switchFile)
runstring = themeWhite
else
set f = .CreateTextFile(switchFile)
runstring = themeBlack
end if
f.close
end with
Sure. That'd work. Anything to get away from relying on registry reads.
--
Crash

Committed to the search for intraterrestrial intelligence.
emf
2014-11-22 04:56:30 UTC
Permalink
Post by Evertjan.
themeBlack =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
themeWhite =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
switchFile = "themeIsWhite.txt"
with CreateObject("Scripting.FileSystemObject")
if .fileExists(switchFile) then
set f = .DeleteFile(switchFile)
runstring = themeWhite
else
set f = .CreateTextFile(switchFile)
runstring = themeBlack
end if
f.close
end with
Your code is certainly simpler, but it doesn't work: It does not change
the themes, but hen the theme is black, it creates the file
themeIsWhite.txt, and when it runs again it deletes it and gives an
error Object rewquired: 'DeleteFile(...)'

emf
--
Natal Transits Calculator
https://files.nyu.edu/emf202/public/nt/nataltransits.html
Evertjan.
2014-11-22 08:37:54 UTC
Permalink
Post by Evertjan.
themeBlack =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
themeWhite =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
switchFile = "themeIsWhite.txt"
with CreateObject("Scripting.FileSystemObject")
if .fileExists(switchFile) then
set f = .DeleteFile(switchFile)
runstring = themeWhite
else
set f = .CreateTextFile(switchFile)
runstring = themeBlack
end if
f.close
end with
It does not change the themes,
Ofcourse not, as it misses the final line.

This code is just to show you the use of a file existence as memory.
but hen the theme is black, it creates the file
themeIsWhite.txt, and when it runs again it deletes it and gives an
error Object rewquired: 'DeleteFile(...)'
As this usenet newsgroup is no helpdesk,
you should be prepared to do some testing and debugging yourself.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
emf
2014-11-26 11:42:43 UTC
Permalink
Post by Evertjan.
Post by Evertjan.
themeBlack =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
themeWhite =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
switchFile = "themeIsWhite.txt"
with CreateObject("Scripting.FileSystemObject")
if .fileExists(switchFile) then
set f = .DeleteFile(switchFile)
runstring = themeWhite
else
set f = .CreateTextFile(switchFile)
runstring = themeBlack
end if
f.close
end with
It does not change the themes,
Ofcourse not, as it misses the final line.
This code is just to show you the use of a file existence as memory.
but hen the theme is black, it creates the file
themeIsWhite.txt, and when it runs again it deletes it and gives an
error Object rewquired: 'DeleteFile(...)'
As this usenet newsgroup is no helpdesk,
you should be prepared to do some testing and debugging yourself.
It happens that I am not familiar with vbscript, that's why I came
here... Actually I had tried to do some debugging myself, by inserting
an alert that would give me the value of a variable. I do not remember
now the code I found, but anyway unfortunately it did not work. What is
the equivalent to javascript's

alert(variable);

?

emf
--
It ain't THAT, babe! - A radical reinterpretation
https://files.nyu.edu/emf202/public/bd/itaintmebabe.html
Dave "Crash" Dummy
2014-11-26 12:26:50 UTC
Permalink
Post by emf
Post by Evertjan.
Post by Evertjan.
themeBlack =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
themeWhite =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
switchFile = "themeIsWhite.txt"
with CreateObject("Scripting.FileSystemObject")
if .fileExists(switchFile) then
set f = .DeleteFile(switchFile)
runstring = themeWhite
else
set f = .CreateTextFile(switchFile)
runstring = themeBlack
end if
f.close
end with
It does not change the themes,
Ofcourse not, as it misses the final line.
This code is just to show you the use of a file existence as memory.
but hen the theme is black, it creates the file
themeIsWhite.txt, and when it runs again it deletes it and gives an
error Object rewquired: 'DeleteFile(...)'
As this usenet newsgroup is no helpdesk,
you should be prepared to do some testing and debugging yourself.
It happens that I am not familiar with vbscript, that's why I came
here... Actually I had tried to do some debugging myself, by inserting
an alert that would give me the value of a variable. I do not remember
now the code I found, but anyway unfortunately it did not work. What is
the equivalent to javascript's
alert(variable);
There are a couple of popup message boxes available. I usually use

msgbox string

I suggest you download the script56.chm reference. It covers both
VBScript and JScript for the Windows Scripting Host.

http://www.microsoft.com/en-us/download/details.aspx?id=2764
--
Crash

"Facts are stubborn things, but statistics are more pliable."
~ Laurence J. Peter ~
Evertjan.
2014-11-26 20:35:30 UTC
Permalink
Post by Dave "Crash" Dummy
Post by emf
It happens that I am not familiar with vbscript, that's why I came
here...
Wrong argument.

This is not a [paid or free] helpdesk, but a usenet discussion/newsgroup.
You should try to do some research yourself and not have us guess about your
lack of basic knowledge.
Post by Dave "Crash" Dummy
Post by emf
Actually I had tried to do some debugging myself, by inserting
an alert that would give me the value of a variable. I do not remember
now the code I found, but anyway unfortunately it did not work. What is
the equivalent to javascript's
alert(variable);
There are a couple of popup message boxes available. I usually use
msgbox string
On the other hand, if you are using VBS [or even JS] on a server, such popup
would'nt be very usefull, as a server would net have a local display.

In that case, try with VBS:

response.write theString & VbCrLf

or

response.write theString
response.end
Post by Dave "Crash" Dummy
I suggest you download the script56.chm reference. It covers both
VBScript and JScript for the Windows Scripting Host.
http://www.microsoft.com/en-us/download/details.aspx?id=2764
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
emf
2014-11-22 04:48:48 UTC
Permalink
Post by Dave "Crash" Dummy
Probably, but simpler code is easier to debug. Here is a simple script
that will toggle the OP's theme, as desired.
'------------------------------------------themes.vbs-------------------------------------------------------
theme1 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
theme2 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
with CreateObject("Scripting.FileSystemObject")
if .fileExists("theme.txt") then
set f=.getFile("theme.txt").OpenAsTextStream(1)
theme=f.readAll
theme=not theme
f.close
set f=.getFile("theme.txt").OpenAsTextStream(2)
f.writeLine theme
else
set f=.CreateTextFile("theme.txt")
theme=0
f.writeLine theme
f.close
end if
end with
if theme then
runstring=theme1
else
runstring=theme2
end if
CreateObject("Wscript.Shell").Run runstring
Thanks! It works perfectly. One more thing only: How can the
Personalization window be closed at the end of the script?

emf
--
Spherical Triangle Calculator
https://files.nyu.edu/emf202/public/js/spherical.html
Dave "Crash" Dummy
2014-11-22 11:17:53 UTC
Permalink
Post by emf
Post by Dave "Crash" Dummy
Probably, but simpler code is easier to debug. Here is a simple script
that will toggle the OP's theme, as desired.
'------------------------------------------themes.vbs-------------------------------------------------------
theme1 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
theme2 =
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"
with CreateObject("Scripting.FileSystemObject")
if .fileExists("theme.txt") then
set f=.getFile("theme.txt").OpenAsTextStream(1)
theme=f.readAll
theme=not theme
f.close
set f=.getFile("theme.txt").OpenAsTextStream(2)
f.writeLine theme
else
set f=.CreateTextFile("theme.txt")
theme=0
f.writeLine theme
f.close
end if
end with
if theme then
runstring=theme1
else
runstring=theme2
end if
CreateObject("Wscript.Shell").Run runstring
Thanks! It works perfectly. One more thing only: How can the
Personalization window be closed at the end of the script?
That, I don't know. If you figure it out, be sure to share.
--
Crash

Atheism is a matter of faith, too.
emf
2014-11-23 10:20:41 UTC
Permalink
Post by Dave "Crash" Dummy
Post by emf
Thanks! It works perfectly. One more thing only: How can the
Personalization window be closed at the end of the script?
That, I don't know. If you figure it out, be sure to share.
I found this code at

http://stackoverflow.com/questions/24699426/how-to-close-the-personalization-window-after-specified-time-via-command-prompt

+++++++++++++++++++++++++++
Set WshShell = WScript.CreateObject("WScript.Shell")
For i = 1 To 10
WScript.Sleep 1000
If WshShell.AppActivate("Personalization") Then
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"
Exit For
End If
Next
+++++++++++++++++++++++++++

So far it seems to work!

Thanks again,

emf
--
Natal Transits Calculator
https://files.nyu.edu/emf202/public/nt/nataltransits.html
Dave "Crash" Dummy
2014-11-23 14:39:47 UTC
Permalink
Post by emf
Post by Dave "Crash" Dummy
Post by emf
Thanks! It works perfectly. One more thing only: How can the
Personalization window be closed at the end of the script?
That, I don't know. If you figure it out, be sure to share.
I found this code at
http://stackoverflow.com/questions/24699426/how-to-close-the-personalization-window-after-specified-time-via-command-prompt
+++++++++++++++++++++++++++
Set WshShell = WScript.CreateObject("WScript.Shell")
For i = 1 To 10
WScript.Sleep 1000
If WshShell.AppActivate("Personalization") Then
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"
Exit For
End If
Next
+++++++++++++++++++++++++++
So far it seems to work!
Thanks. It seems to work, but it would be nice to have something more
positive, and I don't know what that F4 is for. It works without it.
--
Crash

How ironic that we celebrate Independence Day with Chinese fireworks.
emf
2014-11-26 11:32:53 UTC
Permalink
Post by Dave "Crash" Dummy
Post by emf
Post by Dave "Crash" Dummy
Post by emf
Thanks! It works perfectly. One more thing only: How can the
Personalization window be closed at the end of the script?
That, I don't know. If you figure it out, be sure to share.
I found this code at
http://stackoverflow.com/questions/24699426/how-to-close-the-personalization-window-after-specified-time-via-command-prompt
+++++++++++++++++++++++++++
Set WshShell = WScript.CreateObject("WScript.Shell")
For i = 1 To 10
WScript.Sleep 1000
If WshShell.AppActivate("Personalization") Then
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"
Exit For
End If
Next
+++++++++++++++++++++++++++
So far it seems to work!
Thanks. It seems to work, but it would be nice to have something more
positive, and I don't know what that F4 is for. It works without it.
It usually works without it, sometimes, however, as I found out, it
doesn't...

emf
--
Natal Transits Calculator
https://files.nyu.edu/emf202/public/nt/nataltransits.html
Dave "Crash" Dummy
2014-11-22 16:18:54 UTC
Permalink
<snipped>
Post by emf
Thanks! It works perfectly.
Never content to leave well enough alone, I rewrote the script
completely. I got tired of switching between your themes and my themes
to test the code, so I made it more portable by including all the theme
file info in the separate data file. This means that the "themes.txt"
file must be prepared ahead of time. If it doesn't exist, the script
will fail. Here are the script and data files:

'-------------------------------themes.vbs---------------------------------
with CreateObject("Scripting.FileSystemObject")
data=.OpenTextFile("themes.txt").readAll
themes=split(data,vbCRLF)
set f=.CreateTextFile("themes.txt")
for n=1 to ubound(themes)
if len(themes(n)) then f.writeLine themes(n)
next
f.writeLine themes(0)
end with
CreateObject("Wscript.Shell").Run themes(0)

'-------------------------------themes.txt---------------------------------
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\Black.theme"
"C:\Users\Eustace\AppData\Local\Microsoft\Windows\Themes\White.theme"


Note that you are not limited to two themes. I put four in my themes.txt
file.

There is no internal data checking or error handling. If there is no
themes.txt file, you'll just get the host error popup and the script
will abort. If the theme lines are wrong, they will be run as is and
whatever happens, happens.
Post by emf
One more thing only: How can the
Personalization window be closed at the end of the script?
Still don't know. :-)
--
Crash

"The real question is not whether machines think but whether men do."
~ B. F. Skinner ~
Loading...