Applescript command encountering error after closing chrome window - if-statement

So I have been working on a project to mess around with some of my coding friends. I am trying to make an AppleScript application that tells Chrome to go back a page when run and I want it to run all the time. I have had to base it off of whether or not chrome has an active window open or not, I managed to get it to work so far but when I put in the repeated "Go back" command it comes up with an error message saying can't get window 1.
This is the code I am using. I am using High Sierra if that makes a difference.
repeat
if application "Google Chrome" is running then
repeat
tell application "Google Chrome"
if exists (window 1 of application "Google Chrome") then
repeat while exists (window 1 of application "Google Chrome")
go back tab of window 1
end repeat
end if
end tell
end repeat
end if
end repeat

No. This is not how to implement a stay-open script. Doing endless repeat loops with no means of exiting them is just going to devour system resources and make Google Chrome unusable until the script is forced to quit.
Also, the code is horrific: lots of redundant statements and confusing syntax.
You would better achieve your aim by creating what is called a stay-open application script, which will use the idle handler to process commands every few seconds or so. As the name implies, the script will stay open and execute the commands until you tell it to quit.
Start by declaring a property that determines how often the idle handler is called. This is defined at the bottom, following a run handler, which executes upon running of the script and calls the idle handler.
property idleTime : 20 --seconds
on run
idle
end run
on idle
tell application "Google Chrome"
if it is not running then return idleTime
tell window 1 to if it exists then ¬
tell its active tab
if its URL contains "disney" then ¬
tell me to quit
set its URL to "chrome://newtab/"
end tell
end tell
return idleTime
end idle
I slightly modified the joke you're pulling on your friends, because go back will only work so many times before you reach the end of the history and there's no further back one can go. Instead, I told the tab to load up the New Tab starting page every 20 seconds, which I'm sure will be very confusing for the user.
In order to get it up and running properly, you need to save the script as an Application, and check the box that marks it as a stay open script. Then simply double click to run it.
There are two ways to terminate this script: ① Open Activity Monitor and terminate its process from the list of running processes; or ② Visit Disney.com (or any Disney website) in Chrome and wait 20 seconds.

Simply save this code as a stay open application.
on idle
tell application "Google Chrome"
if it is not running then return 10 -- seconds to wait before repeating
tell window 1 to if it exists then ¬
try
go back active tab
end try
end tell
return 10 -- seconds to wait before repeating
end idle

Related

OpenSUSE: why does "service ... start" always stop my application?

I am running OpenSUSE LEAP 15. I am trying to define an application as service such that I can start/stop it using service <servicename> start or service <servicename> stop, resp. and also to restart it automatically on a system restart.
But for some odd reason this doesn't work! My application never runs after a service <servicename> start :-(
The <servicename>.service file that I defined in /usr/lib/systemd/system contains the two commands:
...
ExecStart=/opt/zhquest/current/ctlscript.sh start
ExecStop=/opt/zhquest/current/ctlscript.sh stop
...
When I call the script manually, i.e. when I enter ctlscript.sh start or ctlscript.sh stop then the application starts and stops fine as it should.
But - as I found out by adding echo statements to the script - when I issue a service <servicename> start then the script is actually called twice, first with the argument start and then again with the argument stop. Naturally the application then does not run, since it gets stopped again immediately after it got started.
Why is that so??? Why does the service command call my ctlscript.sh twice, first with argument start and then again with argument stop when I enter service <servicename> start? That seems completely absurd!
Just for the benefit of anyone stumbling over this thread:
I found the answer for my problem here: https://superuser.com/questions/1022142/why-is-systemd-stopping-service-immediately-after-it-is-started
In short: If the application being called to start the application is actually a control script which itself terminates after having done its job of starting (i.e. forking) the application (as is the case here - ctlscript.sh is exactly such a script) then the service type needs to be of Type=forking (and not Type=simple as I had specified since I had simply copied the service-file from another .service-file and only replaced the ExecStart=... and ExecStop=...-values.
Since the started script ends systemd "thinks" that the started application has crashed and immediately executes a subsequent stop-command with the intention to clean up after a crashed applicaiton start.
Here, however, it's normal that the script terminates after having done its job and that is signaled with the ...=forking-option.

What's the time-out value for the start-up of a Windows service

In order to debug a Windows service, written here in my company, the main function starts with:
Sleep(3000); // three seconds
This should allow me to do an "Attach to Process" while the service is starting up, but I seem to be too slow, so I decided to modify this value to 30,000 (thirty seconds).
However now, when starting up the service, I get following error message:
Prompt>net start <Service_Application>
The service is not responding to the control function.
What is the maximum value I can put there in order not to fall into such time-out?
You can get the value by accessing the registry key at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control key ServicesPipeTimeout .
If this value is not set, its default value is set to 125 s ( see https://msdn.microsoft.com/en-us/library/windows/desktop/ms685149(v=vs.85).aspx to learn more about it ).

how to quit currently running Trace32 from command line

I am doing the automate regression with Trace32. Before the regression starts, if any Trace32 process is in use, I want to kill the process. The problem is, if I kill it with system OS, when regression starts, the GUI will pop up a dialog saying "TRACE32 device already in use. Reset device and connect?" I have to manually click yes to continue to regression. Is there any way to quit the currently running Trace32 properly from command line, such that the reset dialog will not show when Trace32 starts next time. Or any command I can add to the .cmm file in my regression to skip this question dialog. I have tried to put RESet initially in .cmm, which does not help.
First of all try to end all your automated test with TRACE32 command QUIT. This will close TRACE32. However something might go wrong in your tests and thus, the QUIT command might not get reached and TRACE32 is still running.
So secondly start TRACE32 with an open Remote-API port. Add to your TRACE32 config-file (by default this is c:\T32\config.t32) the following lines
RCL=NETASSIST
PORT=20000
Before and after the block there must be an empty line. You can also choose any other number for PORT, which specifies a UDP/IP port, which gets opened by TRACE32. (If more than one TRACE32 instance is active at the same time, use different a port number for each instance.)
If TRACE32 was started with open Remote-API port you can send a QUIT command to the still running application instead of terminating it via a kill-command. To send the QUIT command used command line tool t32rem.exe as follow:
t32rem localhost port=20000 QUIT
Finally we need a way to handle the (hopefully rarely happening) situation that TRACE32 somehow crashed and is not longer responsive. Then you have to kill it of course. For a proper reconnect use the following setting CONNECTIONMODE=AUTOCONNECT in the PBI= section of you TRACE32 config-file (by default this is c:\T32\config.t32). This setting does the "Reset device and connect" without asking you.
Putting all together you config-file should look somehow like that:
OS=
ID=myT32
SYS=C:\T32
PBI=
USB
CONNECTIONMODE=AUTOABORT
RCL=NETASSIST
PORT=20000

How to use a windows service as a crash restarter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I recently built an .exe that needs to stay open. (Or re-open after crashed) I have decided to make a watchdog program to restart it when it is closed/crashes. If I can create the watchdog program to work, how would I use a function like this to make my watchdog (which would be a windows service) able to start an executable in the session of the user logged on?
Edit:
Actually, if they don't do the same thing as a fork bomb, would having two vbs scripts doing something like this work?
Set WshShell = WScript.CreateObject("WScript.Shell")
Do
WshShell.Run "c:\somefolder\script1.vbs", 1, true
Loop
along with this script
Set WshShell = WScript.CreateObject("WScript.Shell")
Do
WshShell.Run "c:\somefolder\script2.vbs", 1, true
WshShell.Run "c:\somefolder\program.exe", 1, true
Loop
Okay. The two VBS scripts work really well. I have a question about VBS, but I'll ask that as its own question.
Also the reason people ask questions like yours is often because they don't like the console window.
This vbs script starts the console window hidden.
Set WshShell = WScript.CreateObject("WScript.Shell")
msgbox FormatNumber(WshShell.Run("cmd /k dir c:\windows\*.*", 0, false))
Also your batch file consumes much system resources incl battery life.
Try
:Start
c:\somefolder\program.exe
Goto Start
This starts your program, does nothing till it exits, then starts it.
That can be done in vbscript or vb6 (or vb.net by adding a couple of brackets).
Set WshShell = WScript.CreateObject("WScript.Shell")
Do
WshShell.Run "c:\somefolder\program.exe", 1, true
Loop
OR this which waits for an event.
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set objEvents = objWMIService.ExecNotificationQuery ("SELECT * FROM Win32_ProcessStopTrace")
Do
Set objReceivedEvent = objEvents.NextEvent
If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then
Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
WshShell.Run "c:\Windows\notepad.exe", 1, false
End If
Loop
In the run method. 0 is hidden and true/false is wait for program to exit.
EDIT
The WMI one may look like it's event driven, which it is. But WMI uses polling (appears to be 1 sec). So it has the same resource problems as the first batch file. All you do is move the blame from your program to WMI.
Edit 1
All my scripts I posted do what you want. You only need 1 script not 2. In the looping ones it waits for program to exit then continues. None of the scripts exit. They just keep waiting until your program stops running, then restarts it and waits till it stops again.
I would go the route of having a service process monitor for your primary application. And if it isn't running, then starts it in the desktop session of the current user.
See this msdn blog for some details about doing that.
You will also need to think about what the behavior should be when there isn't somebody logged on, or when multiple people are logged on.
Application Recovery and Restart.
Application Recovery and Restart
Applications have the opportunity to control what actions are taken on their behalf by the system when they fail.
If the application calls the RegisterApplicationRecoveryCallback function, the system executes the recovery callback on application failure. The application can attempt to recover documents. If the application has not registered for recovery, the system still attempts to diagnose and report the problem.
If the application calls the RegisterApplicationRestart function, the system restarts the application after the recovery or diagnosis is completed. If the application has not registered for restart, the system closes the application.
From Help.

How to track code causing application to hang

I'm maintaining an MFC application in some points it stop responding for 30 seconds , some times 1 minute or more. I'm supposed to fix that issue, I tried tracking the code[all methods in this class] and also the issue is still, I tried to pause debugging during this time and I got nothing as in this image
I want to know how to track the code that cause the application to stop responding
add watch variable for any separate thread event
increment each watch variable inside its thread
also very useful can be flag if the thread/event is executing (especially for events)
you must set this flag on enter
end clear before exit
visualize watch variables somehow.
either use some debug print inside your App.
or use separate Window
or even better separate App connected with any IPC method. (this will work even if your App UI hangs)
When the app hangs just see which variables are incrementing and which not
with flags you can determine exact what is hanging up on you ...
Good luck with debuging.