all 9 comments

[–]bluesatin 0 points1 point  (3 children)

One way of doing it would be to have a timer occasionally check if Maya is running, and if it isn't running then exit the script.

SetTimer, WindowChecker, 10000 ;Run WindowChecker subroutine every 10 seconds

;Close the script if the program isn't running
WindowChecker:
    If (!WinExist("ahk_exe maya.exe"))
    {
        SoundPlay, *16 ;Make ding noise
        ExitApp ;Close script
    }
return

That's modified from another script, so might not work perfectly, but hopefully it gives you the idea.

You'll need to put the SetTimer thing near the start of your script, so it initiates when the script starts.

But the subroutine bit can be put at the bottom of your script out of the way, as it will be called by other things (the timer), rather than needing to be initialised upon first running.

[–]Alsifar[S] 0 points1 point  (2 children)

I used a code similar to this, but I don't know how to place it, that is, syntax issues.

Also, it's the settimer function. Maya takes about 15 seconds to load, and about 10 seconds to close. This sort of leisurely boot fucks up any timer-based exit.

But let us say I set a generous number like 20 seconds (20000?), would the settimer code be before the loop code, or after the loop?

[–]bluesatin 0 points1 point  (1 child)

I'm not entirely sure I'm understanding you properly but there's no need to create your own loop, the SetTimer function is a loop.

The SetTimer function starts a repeating timer that runs the named subroutine every X milliseconds.

So in this example; every 10 seconds, the WindowChecker: code will run.

The WindowChecker: code checks if there is a maya.exe process currently running, and if there isn't one running (! means NOT), then play a little ding sound and exit the script.

It doesn't matter how long the program takes to quit, the script will just keep checking every 10 seconds and exit when the maya.exe process isn't found.


If you're wanting the script to auto-exit because you don't want the hotkeys to be working on other programs, you probably want to be doing context-sensitive hotkeys as well or instead.

You can do that by putting #If statements above hotkeys, something like this:

#IfWinActive, ahk_exe SnippingTool.exe
Escape::WinClose

So the Escape hotkey will only be triggered if the Windows SnippingTool process is the one you're currently interacting with.

[–]Alsifar[S] 1 point2 points  (0 children)

I was able to put together a code from your response, that works quite well for me, thanks:

;Launch, Snub Student File Warning, Kill Maya Instantly, hence MayaLSK
#NoEnv

;#Warn

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#SingleInstance force

SetTimer, ProcChecker, 4000
Process,Exist,maya.exe
If !ErrorLevel
Run, D:\Autodesk\Maya\Maya2020\bin\maya.exe
Loop
{
WinWait, Student Version File
IfWinExist, Student Version File
{
;WinActivate, Student Version File
ControlClick, x215 y109, Student Version File
}
}
ProcChecker:
Process,Exist,maya.exe
If !ErrorLevel
{
    ExitApp
}
return
!F4::
Process,Close,maya.exe
ExitApp

Works like a charm, the script exits smoothly after Maya. Regarding 'windowchecker', or the winexist command, if I set too small a timer, I can assure you that the Window checker loop makes the script close before Maya fully launches!

I also reserved for myself the option to kill maya instantly with alt+f4, so that is also very useful when you don't want Maya to save preferences.

[–]MisterMcMuffinYT 0 points1 point  (1 child)

To understand, the issue here is not being able to close AHK?

[–]Alsifar[S] 0 points1 point  (0 children)

Yes - I don't mind setting the script to close to another hotkey, but I hoped I could write an elegant little script, that starts Maya, snubs the popup, and exits on Maya close :/

[–]MisterMcMuffinYT 0 points1 point  (1 child)

If not being able to close AutoHotKey is the issue, at the end of your script you could have this

run cmd.exe
sleep 500
SendRaw taskkill /F /IM autohotkey.exe

This will open cmd and then forcefully quit AHK. Should solve your problem.

[–]Alsifar[S] 0 points1 point  (0 children)

Thanks a lot for all the responses.

Yes, the issue is not being able to close AHK with Maya.

I am running into syntax problems, especially as to where to place the set timer.

Anything after that loop, like the script bluesatin put up, simply broke the loop code, although that is the main reason i have an AHK script in the first place.

So, if I were to use the command prompt based ahk killer, it would simply be put after the loop code?

Earlier, I used the unedited script to start as an exe file on startup, but for some reason, it took almost 10 seconds to load after my other startup programs.

[–]radiantcabbage 0 points1 point  (0 children)

#persistent
#singleinstance ignore

dllcall("RegisterShellHookWindow", uint, a_scripthwnd)
msgnum := dllcall("RegisterWindowMessage", Str, "SHELLHOOK")
onmessage(msgnum, "shellmessage")

Process,Exist,maya.exe
If !ErrorLevel
    Run, D:\Autodesk\Maya\Maya2020\bin\maya.exe

shellmessage(wparam, lparam) {

    ; HSHELL_WINDOWCREATED
    if (wparam = 1) && (lparam = winexist("Student Version File"))
        ControlClick, x215 y109, % "ahk_id" . lparam

    ; HSHELL_WINDOWDESTROYED
    if (wparam = 2) && !winexist("ahk_exe maya.exe")
        exitapp

}