I don't have expiriance in batch scripting bat I need help.
i need a script that will do the following:
After every windows restart, this action must be writen in one .txt file (eg. 'log_restart.txt').
My company want to know number of restarts per every computer, and they want to have recorded it in one file.
Which is the best way to do this?
Thanks
Use WMI to query the system log for the shutdown and restart event. There could be other parts of WMI thats has this info too. Use the WMI Code Creator to generate your code.
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=8572
Class: Win32_NTLogEvent
Logfile: System
You can look over the event log with event viewer to figure out what events you want to record. Set the script to run on startup and it will write them to the file for you, recording the events. Personally, I think you should just setup a real monitoring system that will record these events to a central log. Look at syslog.
UPDATE: There is a setting that hold the last boot time...
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_OperatingSystem",,48)
For Each objItem in colItems
Wscript.Echo "-----------------------------------"
Wscript.Echo "Win32_OperatingSystem instance"
Wscript.Echo "-----------------------------------"
Wscript.Echo "LastBootUpTime: " & objItem.LastBootUpTime
Next
Related
If I open multiple windows/tabs of a browser, and I want to distinguish between them so that I can control those windows individually from my application, what is the approach? If I send command programmatically to a process with PID id it will send that command to the most recently active window but I want to send command to all the window of that process. If I query for PID of running process with CGWindowListOption I get owner PIDs of running processes. let windowsListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
I need to know and use something that will trigger multiple window/process at the same time. Is there a different PID of different window of the same process? For example: Chrome tab 1 has a pid, tab 2 will have another pid. How to find those PIDs instead of the owner PID only?
So how can I find out different process id or similar attribute of a process with multiple window with same owner PID?
This activates each window in TextEdit and sends a space keystroke.
activate application "TextEdit"
tell application "System Events"
tell application process "TextEdit"
repeat with theWindow in windows
perform action "AXRaise" of theWindow
keystroke " "
end repeat
end tell
end tell
But to note how this would work without pressing keys, the following appends "A" to all the currently open documents without bringing anything to the foreground or interfering with the user's input.
tell application "TextEdit"
repeat with theDocument in documents
set text of theDocument to (text of theDocument) & "A"
end repeat
end tell
Whenever possible, you want things like this rather than sending keystrokes.
I'm writing a script to batch convert videos into h.265 format which uses repeat with to go through all videos. It works great for 3 or 4 files at the same time, but my old mac restarts when the number of videos reaches to ~50.
repeat with videofile in video_list
set filePath to POSIX path of videofile
set filePath to esc_space(filePath)
set [folderPath, filename] to split_path_name(filePath)
tell application "Terminal"
activate
do script with command "ffmpeg -hide_banner -i " & filePath & " -vcodec libx265 -tag:v hvc1 " & folderPath & filename & "_hevc.mp4; mv " & filePath & " ~/.Trash"
end tell
end repeat
Therefore, I want to use applescript to achieve the "Queue" functionality: converting limited number of videos in terminal windows (let's say 10) and monitoring if any windows finished executing, activate some remaining tasks if number of active windows is less than 10.
I did some searches and found system event can tell if apps are running, but I'm not sure how to monitoring multiple windows, especially new windows would be activated when some tasks finished.
Any suggestions is appreciated.
(shell script is also welcome if it's convenient)
After some attempts I successfully did it on my own, hope my answer would help someone who happens to have similar questions.
Since the list in AppleScript cannot be modified (correct me if I'm wrong), I have to use index to loop through my videos instead of getting the 1st item and then remove it from list:
set next_video_index to 1
The following infinite repeat loop is for monitoring the number of active terminal windows, which is counted by System Events. It is not the best solution, because System Events counts all windows including those manually opened by the user.
repeat while true
tell application "System Events"
tell application "Terminal"
set window_count to (count of windows)
end tell
end tell
if statement helps to start new converting task when the number of terminal windows doesn't reach the maximum (set to 5 in my code) and not all videos are converted.
It should be noted that ; exit at the end of terminal script, it ensures the finished task window is not messing up the window counts, but you need to change terminal preference first, see this link:
OSX - How to auto Close Terminal window after the "exit" command executed.
set task_not_finished to (next_video_index ≤ length of video_list)
if (window_count < 5) and task_not_finished then
set filePath to POSIX path of item next_video_index in video_list
set filePath to esc_space(filePath)
set [folderPath, filename] to split_path_name(filePath)
set next_video_index to next_video_index + 1
tell application "Terminal"
activate
do script with command "ffmpeg -hide_banner -i " & filePath & " -vcodec libx265 -tag:v hvc1 " & folderPath & filename & "_hevc.mp4; mv " & filePath & " ~/.Trash; exit"
end tell
end if
When the last video is converting, it's time to end the repeat loop.
if not task_not_finished then exit repeat
delay 1
end repeat
I have a scheduled task that wakes up the computer to run a batch file. However the computer turns back off after some time. I have my computer set to (never sleep) so after waking up from the task it should stay on.
However after doing some reading I found out this was because the computer did not wake up from user input.
What I am looking for is a simple script (batch or vb file maybe) I can run via task scheduler that will simulate user input. Maybe hitting the space bar once or moving the mouse.
Running windows 8.1
I tried the following .vbs script without success
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.SendKeys("+{F10}")
You can try this application:
http://mousejiggler.codeplex.com/
It'll simulate mouse movement to keep your computer awake.
If you really want a script try this:
https://gallery.technet.microsoft.com/scriptcenter/Keep-Alive-Simulates-a-key-9b05f980
You can simulate the [F5] key to refresh windows every 2 minutes like this :
Option Explicit
Dim Ws
set Ws = createobject("Wscript.Shell")
Do
Ws.Sendkeys "{F5}"
Call Pause(2)'To sleep for 2 minutes
Loop
'***************************************
Sub Pause(min)
Wscript.Sleep(min*1000*60)
End sub
'***************************************
I'm trying to set it so that there's no user interaction when I open up my illustrator file using applescript, but the standard:
tell application id "com.adobe.Illustrator"
activate
set user interaction level to never interact
open theFile without dialogs
doesn't work for this plugin I have installed that checks for white overprints.
If it were up to me I'd just uninstall the plugin but it's for a work pc.
I also tried clicking the button automatically (with help from Tim Joe) by using:
try
tell application "System Events"
tell process "Finder"
click button "OK" of window "Adobe Illustrator"
end tell
end tell
end try
and I've tried
tell application "System Events"
tell process "Adobe Illustrator"
keystroke return
end tell
end tell
Does anyone know a way of solving this?
below is the full code as it currently stands:
set saveLocation to ((path to desktop) as string) --place to save the files
set theFile to choose file with prompt "Choose the Illustrator file to get outlines on"
set outputFolder to choose folder with prompt "Select the output folder"
tell application "Finder" to set fileName to name of theFile
set fullPath to (saveLocation & fileName) --file path of new .ai
set fileName to (text 1 thru ((length of fileName) - 3) of fileName) --remove .ai from fileName
set olPath to text 1 thru ((length of fullPath) - 3) of fullPath & "_OL.ai" --path of outlined file
tell application id "com.adobe.Illustrator"
activate
ignoring application responses
open theFile without dialogs
end ignoring
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
save current document in file fullPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save file to desktop
convert to paths (every text frame of current document) --convert text to paths
save current document in file olPath as Illustrator with options {class:Illustrator save options, compatibility:Illustrator 15, font subset threshold:0.0, embed linked files:true, save multiple artboards:false} --save another copy to desktop with name + _OL.ai
end tell
tell application "Finder"
set newFolder to make new folder at saveLocation with properties {name:fileName}
move fullPath to newFolder --create new folder and move both new files into it
move olPath to newFolder
set newFolderPath to (newFolder) as string
set newFolderPath to text 1 thru -2 of newFolderPath --remove the trailing ":"
tell current application --zip up the new folder
set qpp to quoted form of POSIX path of newFolderPath
do shell script "cd $(dirname " & qpp & ")
zip -r \"$(basename " & qpp & ").zip\" \"$(basename " & qpp & ")\""
end tell
set zipFile to newFolderPath & ".zip"
move zipFile to outputFolder --move .zip to output
delete newFolder --delete folder on desktop left from zipping
end tell
--prepare a notification email
set presetText to "Hello,
Files Uploaded:
" & fileName & ".zip
To access our FTP Server:
http://217.207.130.162:8080/WebInterface/login.html
To access our FTP server, log onto our website below:
Username:
Password:
Thanks,
Joe"
tell application "Mail" --open up prepared email
activate
set theMEssage to make new outgoing message with properties {visible:true, subject:fileName, content:presetText}
end tell
--open file containing usernames and passwords for the FTP
do shell script "open /Users/produser/Desktop/FTP_Users"
I tracked down and installed White Overprint Detector I could see what you mean. I had to use an older version as I only have CS3, and I saw the dialog it produces when you open a document. The following worked for me to get it to dismiss:
tell application "Adobe Illustrator" to activate
tell application "System Events"
tell process "Adobe Illustrator"
repeat 60 times -- wait up to 60 seconds for WOPD window to appear
try
tell window "White Overprint Detector"
keystroke return
exit repeat
end tell
on error
delay 1
end try
end repeat
end tell
end tell
Since my original post seemed too objective to understand I will revise.
With in the tell block for illustrator look for your line that opens the file. Some commands allow with and without properties. Try applying the "without dialogs" property to look something like this.
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
Update:
Two work arounds I can think of. 1) Try telling system events to tell AI to open without dialogs
tell application "system events"
tell application id "com.adobe.Illustrator"
open file (VariableOfFilePath) without dialogs
end tell
end tell
Other is just add in a bit that will just okay the prompt.
try
tell application "System Events"
tell process "Finder"
click button "Continue" of window "Adobe Illustrator"
end tell
end tell
end try
Can try just having it accept the default button.
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
I'm writing a JScript program which is run in cscript.exe. Is it possible to run a commnad line command from within the script. It would really make the job easy, as I can run certain commands instead of writing more code in jscript to do the same thing.
For example:
In order to wait for a keypress for 10 seconds, I could straight away use the timeout command
timeout /t 10
Implementing this in jscript means more work.
btw, I'm on Vista and WSH v5.7
any ideas? thanx!
You can execute DOS commands using the WshShell.Run method:
var oShell = WScript.CreateObject("WScript.Shell");
oShell.Run("timeout /t 10", 1 /* SW_SHOWNORMAL */, true /* bWaitOnReturn */);
If you specifically need to pause the script execution until a key is pressed or a timeout elapsed, you could accomplish this using the WshShell.Popup method (a dialog box with a timeout option):
var oShell = WScript.CreateObject("WScript.Shell");
oShell.Popup("Click OK to continue.", 10);
However, this method displays a message box when running under cscript as well.
Another possible approach is described in this article: How Can I Pause a Script and Then Resume It When a User Presses a Key on the Keyboard? In short, you can use the WScript.StdIn property to read directly from input stream and this way wait for input. However, reading from the input stream doesn't support timeout and only returns upon the ENTER key press (not any key). Anyway, here's an example, just in case:
WScript.Echo("Press the ENTER key to continue...");
while (! WScript.StdIn.AtEndOfLine) {
WScript.StdIn.Read(1);
}
thanx for the help ppl, this was my first post and stackoverflow is awesome!
Also, I figured out another way to do this thing, using the oShell.SendKeys() method.
Here's How:
var oShell = WScript.CreateObject("WScript.Shell");
oShell.SendKeys("cls{enter}timeout /t 10{enter}");
This way you can run almost every dos command without spawning a new process or window
EDIT: Although it seems to solve the problem, this code is not very reliable. See the comments below
Yes, with the WScript.Shell object.
See the docs and samples