Jump to content

Synergy Launcher


willichan
 Share

Recommended Posts

I use Synergy-Plus in multiple locations/networks, and was getting tired of manually changing which configuration I used for each location. I now use the following script to automatically detect which network I am on, load the correct configuration file, and launch either the client or the server accordingly. This allows me to take my laptop to various client sites and use my setup for that site, while still keeping my home office setup as well.

--- updated Oct. 15, 2010 to fix a bug in the 'client' launcher ---

SynergyStarter.au3

Opt("MustDeclareVars", 1)
Opt("TrayAutoPause", 0)
Opt("TrayMenuMode", 0)
Opt("TrayIconHide", 0)

Global Const $MyName = StringLeft(@ScriptName, StringInStr(@ScriptName, ".", 0, -1) - 1)
Global Const $MyMutex = $MyName & "-77EC0AB9AA3BBB7B"
If _MutexExists($MyMutex) Then Exit

Global Const $versions[7][3] = [[6, "", ""], _
        ["HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Synergy", "", ""], _
        ["HKEY_LOCAL_MACHINE\SOFTWARE\The Synergy+ Project\synergy-plus 1.4.1", "", "bin"], _
        ["HKEY_LOCAL_MACHINE\SOFTWARE\The Synergy+ Project\synergy-plus 1.4.0", "", "bin"], _
        ["HKEY_LOCAL_MACHINE\SOFTWARE\The Synergy+ Project\synergy-plus 1.3.5", "", "bin"], _
        ["HKEY_LOCAL_MACHINE\SOFTWARE\The Synergy+ Project\synergy-plus 1.3.4", "", "bin"], _
        ["HKEY_LOCAL_MACHINE\SOFTWARE\Synergy", "Install_Dir", ""]]
Global $AppPath = _GetAppPath()
Global $ConfName = "synergy.conf"
Global $IniFile = $MyName & ".ini"
Global $ConfigFile = $AppPath & $ConfName

Main()
Exit

Func Main()
    Local $i, $inf, $j
    Local $configs = IniReadSection($IniFile, "configs")
    For $i = 1 To $configs[0][0]
        $inf = StringSplit($configs[$i][1], "|")
        If $inf[0] <> 2 Then ContinueLoop
        If $inf[2] = $ConfName Then ContinueLoop
        $j = Ping($configs[$i][0], 5000)
        If $j > 0 Then
            Switch StringLower($inf[1])
                Case "server"
                    FileCopy(@ScriptDir & "\" & $inf[2], $ConfigFile, 1)
                    Sleep(500)
                    Run('"' & $AppPath & 'synergys.exe" --config "' & $ConfigFile & '" --no-daemon --debug FATAL', $AppPath, @SW_HIDE)
                    ExitLoop
                Case "client"
                    Run('"' & $AppPath & 'synergyc.exe" --no-daemon --debug FATAL ' & $configs[$i][0], $AppPath, @SW_HIDE)
                    ExitLoop
                Case Else
                    ContinueLoop
            EndSwitch
        EndIf
    Next
EndFunc   ;==>Main

Func _GetAppPath()
    Local $res, $ret, $i
    $ret = @ScriptDir
    For $i = 1 To $versions[0][0]
        $res = RegRead($versions[$i][0], $versions[$i][1])
        If Not @error Then
            $ret = $res
            If StringRight($ret, 1) <> "\" Then $ret &= "\"
            $ret &= $versions[$i][2]
            If StringRight($ret, 1) <> "\" Then $ret &= "\"
            ExitLoop
        EndIf
    Next
    Return $ret
EndFunc   ;==>_GetAppPath

Func _MutexExists($sOccurenceName)
    Local $ERROR_ALREADY_EXISTS = 183, $handle, $lastError
    $sOccurenceName = StringReplace($sOccurenceName, "\", "")
    $handle = DllCall("kernel32.dll", "int", "CreateMutex", "int", 0, "long", 1, "str", $sOccurenceName)
    $lastError = DllCall("kernel32.dll", "int", "GetLastError")
    Return $lastError[0] = $ERROR_ALREADY_EXISTS
EndFunc   ;==>_MutexExists

Credit to martin for the _MutexExists function. (Thanks JRowe for the correction)

Here is a sample INI file:

SynergyStarter.ini

[configs]
;machine=type|config
server1=server|srv1.sgc
server2=server|srv2.sgc
machine3=client|cli3.sgc

'machine' is the name of a system to look for that is unique to that network. The first match found will be used. The machine name provided must be able to answer a PING.

'type' is the type of Synergy session to launch. This can only be 'server' or 'client'.

'config' is the name of the config file to use for this session. It will be copied to 'synergy.conf' for server sessions, or passed on the command line for client sessions. The config file must be in the same folder as the script.

This was an effective, yet quick and dirty solution. Any suggestions for improvement will be appreciated.

Edited by willichan
Link to comment
Share on other sites

Link to comment
Share on other sites

This is awesome. I actually just started using Synergy+ and that is one of things that bothered me about it. Nice work.

Edited by FuryCell
HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
Link to comment
Share on other sites

  • 5 weeks later...

I have updated and cleaned up the Synergy Launcher a bit. The top post has been updated to contain the most up-to-date code.

Link to comment
Share on other sites

  • 2 months later...

I have updated and cleaned up the Synergy Launcher a bit. The top post has been updated to contain the most up-to-date code.

This is exactly what I was looking for. Thanks a million. It was getting especially annoying when I would get home and start VPN and my laptop and desktop and work would link up. Now hopefully with your script my home pc's and my work laptop will connect instead.

Anyways thanks for the work.

Link to comment
Share on other sites

  • 4 weeks later...

Latest update (see post #1) fixes a bug in the way that synergyc.exe was being launched. It was not getting the correct parameter.

Link to comment
Share on other sites

  • 3 months later...
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...