AutoHotkey Community

It is currently May 27th, 2012, 12:00 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: November 24th, 2010, 10:48 am 
Offline

Joined: July 18th, 2009, 1:37 am
Posts: 194
Location: Earth
I understand how to stop a script from running a second instance, using #SingleInstance

What i don't understand, is how i can get a "Second instance" of a script to detect it's "First instance", Kill it, then Kill Itself too!

Can some one show me various ways to accomplish this task please? thank you.

For those of you who want to understand method not code, what i'm trying to do is create a single key run-process toggle-feature from a logitech g15 "G-Key" (G-keys are able to run applications).

Any ideas?

_________________
Code I write is free to use under 2 Conditions.
1: Do Not indicate me as responsible for the code, in any way!
2: Do Not willingly inconvenience anyone using code originated by me, period!
Violation of the above voids your eligibility to use my code.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 24th, 2010, 10:53 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Use winget to get a list of windows, each AutoHotkey script has a window (you may have to set detect hidden windows) if there is more then one window kill them all :?: Or simply use an INI to set running state on/off if running kill all running copies of script...

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 24th, 2010, 11:11 am 
Offline

Joined: July 18th, 2009, 1:37 am
Posts: 194
Location: Earth
hugov:
Quote:
Or simply use an INI to set running state on/off if running kill all running copies of script...


Again the intention of my script is to not get along well with duplicates of itself running about.

I want the script to it's brother AND itself, If it detects one.

like the doppelganger-touch, or matter//antimatter theory heh.

This way, when run once, it stays on and active, till it is run again. Then both will blink out of existence together in a fit of process resource usage and liberation!

There is a reason i need to do it this way. Suspend and Pause are not working for my needs, as the script uses both hotkeys, -and- a looped input. Which, when both in the same script seem to behave erratically if one tires to suspend, pause, or both.

If you could provide a rough example to get me started i'd appreciate it a great deal.

_________________
Code I write is free to use under 2 Conditions.
1: Do Not indicate me as responsible for the code, in any way!
2: Do Not willingly inconvenience anyone using code originated by me, period!
Violation of the above voids your eligibility to use my code.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 24th, 2010, 11:38 am 
Offline

Joined: August 25th, 2009, 4:52 pm
Posts: 321
Save this as firstscript.ahk
Code:
#singleinstance, Force
#persistent
SetWorkingDir %A_ScriptDir%
OnExit, ExitLog

IniRead, ExitReason, exitlog.ini, Log, ExitReason
if ExitReason = Single
   ExitApp
   
loop {
   sleep 100
   count++
   tooltip % count
}
Return

~Esc::
ExitApp

ExitLog:
   IniWrite, %A_ExitReason%, exitlog.ini, Log, ExitReason
ExitApp


Press Control-1 to toggle the first script from the other script.
Code:
^1::
   Run firstscript.ahk
Return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 24th, 2010, 11:41 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Save as thump.ahk, don't keep it open in your editor as it will close that window as well. Of course change thump to full autohotkey.exe path and script name (builtin variable) to be 100% sure
Quote:
#SingleInstance Off
DetectHiddenWindows, On
SetTitleMatchMode, 2

WinGet, Windows, List, thump
If (Windows > 1)
{
Process, Exist ; code from hotkeyit http://www.autohotkey.com/forum/viewtop ... 170#272170
OWNPID:=ErrorLevel
WinGet, ID, List, thump
Loop %ID%
{
WinGet, PID,PID,% "ahk_id " . ID%A_Index%
If (PID!=OWNPID)
WinClose, % "ahk_id " . ID%A_Index%
}
ExitApp
}

#z::
MsgBox Hi
Return

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 24th, 2010, 12:11 pm 
Offline

Joined: July 18th, 2009, 1:37 am
Posts: 194
Location: Earth
Yay! \o/
Not only did your code work, (especially well with use of the %A_ScriptFullPath% Var!)

But i also fixed my Suspend+Pause problems!

So with your powers combined! I am going to Bed! Finally! Yay! ~because it WORKS! Squeee! *^-^* So here you go, here is the self terminating on a double run script, with pause-toggle option :D

Designed to type off-screen into an untitled notepad, useful for transcribing data while in full screen games! *^-^*

Hope you and others find it useful! *^-^*

Code:
;csinu.ahk
;Control Send to Inactive Notepad Untitled

#SingleInstance Off
DetectHiddenWindows, On
SetTitleMatchMode, 2

WinGet, Windows, List, %A_ScriptFullPath%
If (Windows > 1)
{
Process, Exist
OWNPID:=ErrorLevel
WinGet, ID, List, %A_ScriptFullPath%
Loop %ID%
{
WinGet, PID,PID,% "ahk_id " . ID%A_Index%
If (PID!=OWNPID)
WinClose, % "ahk_id " . ID%A_Index%
}
ExitApp
}

SendMode Input

Loop
{
IfWinNotExist, Untitled - Notepad
   {
   exit
   }

Input, OutputVar, I L1 M,{pause}
IfNotInString, ErrorLevel, Pause
   {
   ControlSend, Edit1,{blind}%OutputVar%, Untitled
   OutputVar:=
   }
   else
   {
   OutputVar:=
   Suspend, Toggle
   Pause, Toggle
   }
}
Return

*!::ControlSend, Edit1,{blind}{!}, Untitled
*#::ControlSend, Edit1,{blind}{#}, Untitled
*^::ControlSend, Edit1,{blind}{^}, Untitled
*+::ControlSend, Edit1,{blind}{+}, Untitled
;Space::ControlSend, Edit1,{blind}%A_ThisHotkey%, Untitled
;*Tab::ControlSend, Edit1,{blind}{tab}, Untitled
;*Enter::ControlSend, Edit1,{blind}{enter}, Untitled

;*Pause::ControlSend, Edit1,{blind}{pause}, Untitled
Pause::
Suspend, Toggle
Pause, Toggle
return

;*Esc::ControlSend, Edit1,{blind}{esc}, Untitled
*BS::ControlSend, Edit1,{blind}{bs}, Untitled
*Del::ControlSend, Edit1,{blind}{del}, Untitled
*Ins::ControlSend, Edit1,{blind}{ins}, Untitled
*Home::ControlSend, Edit1,{blind}{home}, Untitled
*End::ControlSend, Edit1,{blind}{end}, Untitled
*PgUp::ControlSend, Edit1,{blind}{pgup}, Untitled
*PgDn::ControlSend, Edit1,{blind}{pgdn}, Untitled
*Up::ControlSend, Edit1,{blind}{up}, Untitled
*Down::ControlSend, Edit1,{blind}{down}, Untitled
*Left::ControlSend, Edit1,{blind}{left}, Untitled
*Right::ControlSend, Edit1,{blind}{right}, Untitled

;ScrollLock::ControlSend, Edit1,{blind}{}, Untitled
;CapsLock::ControlSend, Edit1,{blind}{}, Untitled
;NumLock::ControlSend, Edit1,{blind}{}, Untitled

*f1::ControlSend, Edit1,{blind}{f1}, Untitled
*f2::ControlSend, Edit1,{blind}{f2}, Untitled
*f3::ControlSend, Edit1,{blind}{f3}, Untitled
*f4::ControlSend, Edit1,{blind}{f4}, Untitled
*f5::ControlSend, Edit1,{blind}{f5}, Untitled
*f6::ControlSend, Edit1,{blind}{f6}, Untitled
*f7::ControlSend, Edit1,{blind}{f7}, Untitled
*f8::ControlSend, Edit1,{blind}{f8}, Untitled
*f9::ControlSend, Edit1,{blind}{f9}, Untitled
*f10::ControlSend, Edit1,{blind}{f10}, Untitled
*f11::ControlSend, Edit1,{blind}{f11}, Untitled
*f12::ControlSend, Edit1,{blind}{f12}, Untitled
;*f13::ControlSend, Edit1,{blind}{f13}, Untitled
;*f14::ControlSend, Edit1,{blind}{f14}, Untitled
;*f15::ControlSend, Edit1,{blind}{f15}, Untitled
;*f16::ControlSend, Edit1,{blind}{f16}, Untitled
;*f17::ControlSend, Edit1,{blind}{}f17, Untitled
;*f18::ControlSend, Edit1,{blind}{f18}, Untitled
;*f19::ControlSend, Edit1,{blind}{f19}, Untitled
;*f20::ControlSend, Edit1,{blind}{f20}, Untitled
;*f21::ControlSend, Edit1,{blind}{f21}, Untitled
;*f22::ControlSend, Edit1,{blind}{f22}, Untitled
;*f23::ControlSend, Edit1,{blind}{f23}, Untitled
;*f24::ControlSend, Edit1,{blind}{f24}, Untitled

;*AppsKey::ControlSend, Edit1,{blind}{appskey}, Untitled

_________________
Code I write is free to use under 2 Conditions.
1: Do Not indicate me as responsible for the code, in any way!
2: Do Not willingly inconvenience anyone using code originated by me, period!
Violation of the above voids your eligibility to use my code.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 24th, 2012, 8:03 pm 
Offline

Joined: January 29th, 2009, 9:50 pm
Posts: 483
Location: Belgium
if you compile the script, you can use this:
Code:
#SingleInstance off                           ; We handle instances ourselves
DetectHiddenWindows, On                     ; needed to detect the first instance (WinClose)
Process, Exist,%A_ScriptName%               ; this allways gets the pid from the 1 instance in the ErrorLevel
FirstInstancePID:=ErrorLevel               ; store the pid of the instance
Process, Exist                           ; this gets the pid of this instance
ThisInstancePID:=ErrorLevel                  ; store the pid of this instance
if (FirstInstancePID<>ThisInstancePID) {       ; if this is the second instance
   WinClose,ahk_pid %FirstInstancePID%         ; close the first instance
   exitapp                              ; close this instance
   }
return
esc::run,%A_scriptfullpath%                  ; pres escape to run itself and terminate itself(for testing)

_________________
Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Google Feedfetcher, Yahoo [Bot] and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group