Showing posts with label AutoHotKey. Show all posts
Showing posts with label AutoHotKey. Show all posts

Saturday, March 9, 2013

AutoHotKey Scripts

So after my last post about AHK script to allow an application running only when connected to a VPN i thought i should make another post with moar scriptz.

I found about AHK Command Picker @ http://ahkcommandpicker.codeplex.com/ and i thought damn this thing is a great script and i Shall build all my scripts, shortcuts, functions and what not in AHK Cmd picker.

I want to share my complete setup with you and i'll try to make it as general as possible so you can adopt it pretty easily for your own needs.

So GitHub, i know, my first experience with it just now for this purpose to share AHK scripts. One of the other reasons why GitHub is collaboration and improvements...

https://github.com/ilirb/ahk-scripts

I'll intruduce you briefly here what will you find at my GitHub repository and what "extra" functions i have included in AHK Command Picker. However, i will not keep this post up-to-date since it'll defeat the purpose having it in the GitHub, instead, i'll update my GitHub repository.

Please read more about AHK Command Picker itself at http://ahkcommandpicker.codeplex.com/documentation so you can get a sense what it is about, how to use it and how to include your own scripts.

Besides AHK cmd picker you will get at least this from my repository (as of  2013 March 09):
  • OpenConsole - Will open CMD in the current folder opened in Windows Explorer
  • NewTextFile - You like build-in Ctrl+Shift+N to create a new folder in Windows 7? Use Ctrl+Shift+T to create a new text file in current folder.
  • uTorrentWebUI - Remote control uTorrent running in another machine. Stop, start, pause, unpause, etc. Send a torrent url or magnet link to another computer with a single shortcut. And more...
  • ConnectVPNRemote - Start VPN connection on the remote machine
  • PSExecWArgs - Execute commands in remote computer using PSExec using dialog input.
  • GoogleMusicControl - Simple Google Play Music control start, pause, next, previous. (Opened in Chrome only, for now)
  • LoopChromeTabs - Go through all opened tabs and find a certain tab in Chrome
  • Some really basic shortcuts...
Go ahead and try it, i myself have become obsessed with automating things and trying to do normal day to day stuff with less effort.

Tuesday, January 8, 2013

Allow application to run only when you are connected to VPN with AutoHotKey

I recently started using +AutoHotkey  to automate things and i really like it, and I'm going to post a series of my scripts that i use.

This AHK script is about checking if I'm connected to certain VPN (or one of them if there are many) and start a backup, perform a set of actions,start an application, run uTorrent :) basically anything you can think of.
It will do a check every 10 sec to see if you are connected, if the VPN connection is dropped it will close the application and start it if connection is back.

To get started download and install AutoHotKey from http://www.autohotkey.com/

I also recommend SciTE4AutoHotkey editor http://www.autohotkey.net/~fincs/SciTE4AutoHotkey_3/web/ to create, edit, debug scripts.

Copy the entire code (in blue) and paste it in a new file and save it with extension .ahk.
Tip: If you are using Notepad and want to have different file extension than .txt include the whole filename in quotas like "checkVpn.ahk" when you save the file.

To run it just double click on the file and it will run in background which you can find it in system tray.

To have this run when Windows starts make a shortcut of this file in Startup folder in Startmenu or just move the script there.

For this example i have used uTorrent.exe \m/
And only changes you have to make is your VPN IP list column delimited, e.g.:
I have three different VPN connections whose IPs start with
10.0.1.3
10.0.2.23
10.0.3.132
Since i know the first 10.0.1 won't change i use only those and make it
10.0.1:10.0.2:10.0.3
Applications filename and
Applications full path

Enjoy
VPNIP = 10.0.1:10.0.2:10.0.3 ;Write your VPN IP or part of it, if you have many separate them using column :
app = uTorrent.exe ;Write the file name of the app
appFullPath = "C:\Program Files (x86)\uTorrent\uTorrent.exe" ;Write the full path including filename of the app

Loop ;perform the main action
{
 global Connected, app, appFullPath
 Process, Exist, %app%
 AppRunning = %ErrorLevel%
 Sleep, 10000
 CheckIP()
 If (Connected = 1)
  {
  If (AppRunning = 0) ; If it is not running
   {
   Run, %appFullPath%
   }
  else
  continue
  } 
 else
 {
  If (AppRunning > 0)
  Process, Close, %app%
 }
}

CheckIP() ;checks if we are connected to VPN and set variable Connected to 0|1
{
 global VPNIP
 StringSplit, IPArray, VPNIP, :
 LocalIP = %A_IPAddress1%,%A_IPAddress2%,%A_IPAddress3%,%A_IPAddress4%
 Matches = 0
 Loop, %IPArray0%
 {
  Each := IPArray%A_Index%
  IfInString, LocalIP, %Each%
   Matches++
 }
 If Matches > 0
  Connected = 1
 else
  Connected = 0
}