Run script that opens word - if I run another script (10 different) end it

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Run script that opens word - if I run another script (10 different) end it

26 Oct 2017, 11:07

A bit misleading title perhaps but not fully sure what to write.

I have around 20 different scripts that does things. All 20 scripts has the same begging Function(begin) and same ending function(end). The middle part is what differents them aparat. The start of that end function is:

Winwait, WindowTitle

And when Windowtitle opens it runs a different commands. Now for the problem!

The user sees they opened the wrong template of word and closes it. Since they close the window the script is still running and waiting for the "Winwait, windowtitle". They run another script that opens Word and does bunch of stuff. Suddenly there's two active function(end)... That's the prob!

I was thinking about winwaitclose, or ifwinnotexist.. But they only check for it first time an if true/false does stuff. How can I close the script if Word get's closed and/or if another script gets run? Do I need a if script 1-20 is running, exitapp or what's a good solution?
User avatar
king-of-hearts
Posts: 30
Joined: 01 Oct 2017, 06:29

Re: Run script that opens word - if I run another script (10 different) end it

26 Oct 2017, 12:10

About the templates, what about harcoding the word file name inside the 'if', like:

Code: Select all

WinWaitActive, *Specific File Name*
 ; * do some action
https://autohotkey.com/boards/viewtopic.php?f=6&t=38707 - MS Access Manager - SQL Query: Incredible tool for MS Access/SQL Queries on the fly!!
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script that opens word - if I run another script (10 different) end it

26 Oct 2017, 14:34

king-of-hearts wrote:About the templates, what about harcoding the word file name inside the 'if', like:

Code: Select all

WinWaitActive, *Specific File Name*
 ; * do some action

The winwait is for popup that opens. That popup is the same for all 20 scripts..
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Run script that opens word - if I run another script (10 different) end it

26 Oct 2017, 14:59

Why don't you trigger the rest of your code with a hotkey?
When the user knows, that a script is running, it would be not to much required, pressing a predifined key.
WinWait and similar commands can be very unreliable.
Einfach nur ein toller Typ. :mrgreen:
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script that opens word - if I run another script (10 different) end it

26 Oct 2017, 15:06

divanebaba wrote:Why don't you trigger the rest of your code with a hotkey?
When the user knows, that a script is running, it would be not to much required, pressing a predifined key.
WinWait and similar commands can be very unreliable.

We have a script at our rescue hostel that opens a menu for 20 people in a network. From this menu they can click on different scripts that runs different word templates. The codes are a hamburger.

Start (same for all)
middle (unique)
End (Same for all).

The end part has the WinWait (as they have to finish the template, and then press a special button to have it printed, (note, not the normal printing ctrl + p)). So if they opened a template, and noticed it was wrong, they would hotkey the menu, and open another template, this 2 scripts running and suddenly wainting for the endcode where "WinWait" is the first line.. I need to have it shut somehow if you close the word document.

Can one make a loop with winexist or so? But it needs to stop the loop if the endcode is triggered :S Ah, i cant make any sense out of this :(
User avatar
king-of-hearts
Posts: 30
Joined: 01 Oct 2017, 06:29

Re: Run script that opens word - if I run another script (10 different) end it

26 Oct 2017, 18:35

I would join all the ahk scripts file into one simple file.

Then I would refactor the 'hamburguer code' to make ir reliable;

If you need help post your script!
https://autohotkey.com/boards/viewtopic.php?f=6&t=38707 - MS Access Manager - SQL Query: Incredible tool for MS Access/SQL Queries on the fly!!
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script that opens word - if I run another script (10 different) end it

27 Oct 2017, 01:26

king-of-hearts wrote:I would join all the ahk scripts file into one simple file.

Then I would refactor the 'hamburguer code' to make ir reliable;

If you need help post your script!
Each script is around 600 lines and all of them are with GUI's. Would be a hell to manage, update etc. + people are using a menu to call different scripts.. So that's not possible for us :/
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script that opens word - if I run another script (10 different) end it

30 Oct 2017, 02:35

So I am thinking about making a file with the script name in the begging of the function.

Then have a IF with "If file exist, abort else run".

Is there a simplier way?
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Run script that opens word - if I run another script (10 different) end it

31 Oct 2017, 06:12

Patience is a virtue. Also, as king-of-hearts said, if you need help, post code, snippets what ever it might need for a relevant answer. Because we can tell you how something works just by question but where to put it or with what to replace, we first need to know the code.

Its like me asking you, where should i put this picture?
You dont know what picture or its dimensions nor you know my house. Maybe in the kitchen? Maybe in the dining room? Does the picture frame go with the living room decor or the sleeping room instead? Or replace it with a vase? Maybe some other wall decor?
If i showed the picture as well as the house, you could tell in what room the picture would fit in or if a vase should be placed instead.
Hope this gives you an idea. :)
Good luck!
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script that opens word - if I run another script (10 different) end it

31 Oct 2017, 08:12

theimmersion wrote:Patience is a virtue. Also, as king-of-hearts said, if you need help, post code, snippets what ever it might need for a relevant answer. Because we can tell you how something works just by question but where to put it or with what to replace, we first need to know the code.

Its like me asking you, where should i put this picture?
You dont know what picture or its dimensions nor you know my house. Maybe in the kitchen? Maybe in the dining room? Does the picture frame go with the living room decor or the sleeping room instead? Or replace it with a vase? Maybe some other wall decor?
If i showed the picture as well as the house, you could tell in what room the picture would fit in or if a vase should be placed instead.
Hope this gives you an idea. :)
Good luck!
Hi and thanks. I can't show code as my boss has forbidden me as we have strict IT rules.

I Think I have overexplained. The thing is like this:

If I start a script and I have a winwait for a window. The scripts will temporary stop, until the winwait window pops up, then it continues the script.

If I start Another script that was the same winwait window, then two scripts are waiting for the same window to popup, then execute different things.

So: I want the script to Always check a variable (or any other solution). If the variable is true = Exitapp.

So in short: Check if a script is running, if yes, abort. (I have 20-30 scripts, so a 0/1 variable condition if script is running would be the best I Think?)
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Run script that opens word - if I run another script (10 different) end it

31 Oct 2017, 13:28

Than I guess a business and with it your boss should hire a certified programmer/it expert. That's what I would do if I was a strickt boss. xD
I mean... 20 scripts oh boy..
Any way, try ini read ini write? As in, the firt to start writes in ini 1 and others read and exit if 1 encountered?
Every script should have this somewhere at the top,again,guessing since no code to go to.
Only, id place config where its easy accessible (read write permission outside of uac) like documents.

Iniread, check, config.ini,general,check
If (check = 0)
{
iniwrite, 1,config.ini,general,check
Goto, to actual script operation
}
else
exitapp

OnExit, exit
exit:
iniwrite, 0,config.ini,general, check
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script that opens word - if I run another script (10 different) end it

31 Oct 2017, 17:06

theimmersion wrote:Than I guess a business and with it your boss should hire a certified programmer/it expert. That's what I would do if I was a strickt boss. xD
I mean... 20 scripts oh boy..
Any way, try ini read ini write? As in, the firt to start writes in ini 1 and others read and exit if 1 encountered?
Every script should have this somewhere at the top,again,guessing since no code to go to.
Only, id place config where its easy accessible (read write permission outside of uac) like documents.

Iniread, check, config.ini,general,check
If (check = 0)
{
iniwrite, 1,config.ini,general,check
Goto, to actual script operation
}
else
exitapp

OnExit, exit
exit:
iniwrite, 0,config.ini,general, check
Thank you I'll try this. Well I am doing this just so that we have it easier at our work. I volunteer at a hospice for animals mainly rescue dogs. Since the scripts have information about our customers and/or private company information I was not allowed to put it up. I barely got allowed to use autohotkey :)

Thanks again for the help, i'll try this tomorrow :)
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Run script that opens word - if I run another script (10 different) end it

31 Oct 2017, 17:17

Actually this is a very ugly hack. But might be what you are looking for. At least, its a start.
Do keep in mind if for instance power to PC went out, the script exit would not be called and thus, the ini value would remain 1 which would prevent any script from firing. You would need to go into the ini file and set it to 0 manually.

Honestly, id think youd need a main script that controls the rest while being live. This way, no matter what happens, it starts fresh and the main script would control what and where happens real-time. Most of us use that kind of approach. Do keep that in mind. Managing lots of script aint easy but doable. Good luck! :)
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script that opens word - if I run another script (10 different) end it

01 Nov 2017, 03:05

theimmersion wrote:Actually this is a very ugly hack. But might be what you are looking for. At least, its a start.
Do keep in mind if for instance power to PC went out, the script exit would not be called and thus, the ini value would remain 1 which would prevent any script from firing. You would need to go into the ini file and set it to 0 manually.

Honestly, id think youd need a main script that controls the rest while being live. This way, no matter what happens, it starts fresh and the main script would control what and where happens real-time. Most of us use that kind of approach. Do keep that in mind. Managing lots of script aint easy but doable. Good luck! :)
Thanks,

What about in the ini-file, or any other file, it inserts the file name.

Example all scripts should have at top:

1: IfFileExist: doexist.ahk, read the files first line and exitapp whateveriswritten.ahk

2: Create file called "doexist.ahk" and insert current filename (ex: script1.ahk) into that filename.

3: in the end of the script: Delete the file.

Should this not work? This means it Always checks if a doexist.ahk exist, if it does, it exits the app (name) that is inside that file, then create a new doexist.ahk (or replaces the row) with it's new scriptname.. and then shuts it down if it runs..

Shouldn't this work ?

This code seems to Close the ahk script that is running:

Code: Select all

DetectHiddenWindows, On 
WinClose, %fullScriptPath% ahk_class AutoHotkey
So the fullscriptPath = the full script path of the script inside the doexist.ahk.. Or am I totally wrong overthinking this?
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Run script that opens word - if I run another script (10 different) end it

01 Nov 2017, 07:09

What you described is what i just said about reading the ini. as far as any script is concerned, it doesnt care what is written in it. You can put what ever you want. You can have the first script writes its own path or name so you know in case of failure, which script didnt exit properly.
For instance:
IniWrite, %A_ScriptName%, %A_MyDocuments%\ScriptSets, GENERAL, ActiveScript

Let me give you a ScriptA.ahk and ScriptB.ahk working example:
Have all items on desktop for test.
Config.ini

Code: Select all

[GENERAL]
ActiveScript=
ActiveScriptFullPath=
ScriptA.ahk

Code: Select all


#SingleInstance, Force

IniRead, CurrentlyRunningScript, Config.ini, GENERAL, ActiveScript
if CurrentlyRunningScript=
{
	IniWrite, %A_ScriptName%, Config.ini, GENERAL, ActiveScript
	IniWrite, %A_ScriptFullPath%, Config.ini, GENERAL, ActiveScriptFullPath
}
else
	ExitApp

OnExit, ExitSub
msgbox, 262180,ScriptA, Script is doing its magic and finished.`nIf ScriptB was running, i would never show up.`n`nExit now?
	ifmsgbox,yes
		GoSub,ExitSub

ExitApp

ExitSub:
	IniWrite, % "", Config.ini, GENERAL, ActiveScript
	IniWrite, % "", Config.ini, GENERAL, ActiveScriptFullPath
	ExitApp
ScriptB.ahk

Code: Select all


#SingleInstance, Force

IniRead, CurrentlyRunningScript, Config.ini, GENERAL, ActiveScript
if CurrentlyRunningScript=
{
	IniWrite, %A_ScriptName%, Config.ini, GENERAL, ActiveScript
	IniWrite, %A_ScriptFullPath%, Config.ini, GENERAL, ActiveScriptFullPath
}
else
	ExitApp

OnExit, ExitSub
msgbox, 262180,ScriptB, Script is doing its magic and finished.`nIf ScriptA was running, i would never show up.`n`nExit now?
	ifmsgbox,yes
		GoSub,ExitSub

ExitApp

ExitSub:
	IniWrite, % "", Config.ini, GENERAL, ActiveScript
	IniWrite, % "", Config.ini, GENERAL, ActiveScriptFullPath
	ExitApp
If you active ScriptA and leave the message on and try to start ScriptB, it wont show up and vice versa. Only, imagine instead of msgbox, your scripts code is there. It will do what ever its job is and after exiting, it will clean after itself. But theres the issue of not closing properly and potentially ExitSub never getting called which would prevent any of them to start. A power out, or autohotkey process crash or force close windows shutdown. Any of them could cause any of those scripts to fail to clean close so you would have to edit the config.ini and delete any value after ActiveScript=
Keep in mind, this is a dirty fix. xD
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: Run script that opens word - if I run another script (10 different) end it

01 Nov 2017, 10:25

Hello TheImmersion, thank you so much, I'll try your code too. I didn't see it Before applying my code as well so I made my own code, but I will try yours too as it's nice to have different ways. Here's how I did it.

All the scripts are Calling "function.ahk". So inside of it, I have:

Code: Select all

ScriptPath := A_ScriptFullPath

IfNotExist, %A_MyDocuments%\AutoHotKey\ ; If the folder AutoHotKey does not exist
{
FileCreateDir, %A_MyDocuments%\AutoHotKey\ ; Create a folder called "AutoHotKey"
}

IfNotExist, %A_MyDocuments%\AutoHotKey\scriptrunning.donttouch ; if file exist
{
FileAppend, %ScriptPath%, %A_MyDocuments%\AutoHotKey\scriptrunning.donttouch, UTF-8 ;  Add a file called "scriptrunning.donttouch" with the scripts full scripthpath.
}

else
{
DetectHiddenWindows, On
	FileReadLine, RunningScript, %A_MyDocuments%\AutoHotKey\scriptrunning.donttouch, 1 ; Read the first line inside the file, add it to variable "RunningScript"
WinClose, %RunningScript% ahk_exe AutoHotKey.exe ; Close the ahk-file with the name from the variable RunningScript
}
And in the end of each file theres a end() function, also called from functions.ahk so i added filedelete to the file.
theimmersion wrote:What you described is what i just said about reading the ini. as far as any script is concerned, it doesnt care what is written in it. You can put what ever you want. You can have the first script writes its own path or name so you know in case of failure, which script didnt exit properly.
For instance:
IniWrite, %A_ScriptName%, %A_MyDocuments%\ScriptSets, GENERAL, ActiveScript

Let me give you a ScriptA.ahk and ScriptB.ahk working example:
Have all items on desktop for test.
Config.ini

Code: Select all

[GENERAL]
ActiveScript=
ActiveScriptFullPath=
ScriptA.ahk

Code: Select all


#SingleInstance, Force

IniRead, CurrentlyRunningScript, Config.ini, GENERAL, ActiveScript
if CurrentlyRunningScript=
{
	IniWrite, %A_ScriptName%, Config.ini, GENERAL, ActiveScript
	IniWrite, %A_ScriptFullPath%, Config.ini, GENERAL, ActiveScriptFullPath
}
else
	ExitApp

OnExit, ExitSub
msgbox, 262180,ScriptA, Script is doing its magic and finished.`nIf ScriptB was running, i would never show up.`n`nExit now?
	ifmsgbox,yes
		GoSub,ExitSub

ExitApp

ExitSub:
	IniWrite, % "", Config.ini, GENERAL, ActiveScript
	IniWrite, % "", Config.ini, GENERAL, ActiveScriptFullPath
	ExitApp
ScriptB.ahk

Code: Select all


#SingleInstance, Force

IniRead, CurrentlyRunningScript, Config.ini, GENERAL, ActiveScript
if CurrentlyRunningScript=
{
	IniWrite, %A_ScriptName%, Config.ini, GENERAL, ActiveScript
	IniWrite, %A_ScriptFullPath%, Config.ini, GENERAL, ActiveScriptFullPath
}
else
	ExitApp

OnExit, ExitSub
msgbox, 262180,ScriptB, Script is doing its magic and finished.`nIf ScriptA was running, i would never show up.`n`nExit now?
	ifmsgbox,yes
		GoSub,ExitSub

ExitApp

ExitSub:
	IniWrite, % "", Config.ini, GENERAL, ActiveScript
	IniWrite, % "", Config.ini, GENERAL, ActiveScriptFullPath
	ExitApp
If you active ScriptA and leave the message on and try to start ScriptB, it wont show up and vice versa. Only, imagine instead of msgbox, your scripts code is there. It will do what ever its job is and after exiting, it will clean after itself. But theres the issue of not closing properly and potentially ExitSub never getting called which would prevent any of them to start. A power out, or autohotkey process crash or force close windows shutdown. Any of them could cause any of those scripts to fail to clean close so you would have to edit the config.ini and delete any value after ActiveScript=
Keep in mind, this is a dirty fix. xD
User avatar
theimmersion
Posts: 181
Joined: 09 Jul 2016, 08:34
Location: Serbia

Re: Run script that opens word - if I run another script (10 different) end it

01 Nov 2017, 12:36

Which ever works best for you. Glad you figured it out. :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: DaveF, rubeusmalfoy, Spawnova and 227 guests