Tutorial Update – Enhanced AutoHotKey Scripts when Remotely Syncing HanDBase Database for Android with Desktop PC via Dropbox or Google Drive

Set-up the AutoHotKey Macro App for Remote, Automatic Syncing
Before you proceed, make sure you have installed the AutoHotKey Macro App on your computer.

After some additional testing I figured out a way in AutoHotKey to automatically select the HanDBase Conduit Partnership, rather than requiring it to be the first Partnership in the list. I have also added instructions for syncing these files between your Android device and Dropbox or Google Drive to make initiating the sync easier, with fewer steps.

This updated version of the AutoHotKey Syncing will require you to create a folder on your Android device and in your Dropbox or Google Drive folder to make initiating the sync easier. This will also hold the file that contains your Partnership name so that the Script selects it in the HanDBase Conduit.

First create a folder in the User folder you previously created in your Dropbox of Google Drive folder called RemoteActions Then create another folder inside that one called remoteAction_ran

You will then need to create these same folders on your Android Device using a File Manager like ASTRO File Manager or Root Explorer. Create the RemoteActions folder in the same place you find the HanDBase folder, either the root of your device or the root of your SDCard, depending on your device. Create the same remoteAction_ran folder inside that one. Later you’ll be setting up other FolderSync actions to sync these folders as well.

You will then need to create or modify two scripts for the Remote and Automatic syncing to work properly. At the end of this section you will find a link to download a .ZIP file that includes the two files needed. You can modify those as needed.

The first script is RemoteAction.ahk (.ahk is the file extension for AutoHotKey scripts). This script will always be active on your computer, so that it can run the second script when needed. The commands in the RemoteAction.ahk script are as follows.


;Format the Date and Time for the Backup file.
FormatTime, CurrentDateTime,, yyyy.MM.dd.HH.mm.ss

; Check if the RemoteHDBSync.ahk script exists in the same folder.
IfExist, RemoteHDBSync.ahk

; If the RemoteHDBSync.ahk script exits, run it to launch the HanDBase Conduit.
; Then wait for about 2 minutes for the Conduit sync to finish before continuing.
{
Run, RemoteHDBSync.ahk
Sleep, 120000

; Move the RemoteHDBSync.ahk file to the remoteAction_ran folder and rename it with the Date and Time ran.
FileMove, RemoteHDBSync.ahk, remoteAction_ran/%CurrentDateTime%_RemoteHDBSync.ahk
}

; Wait for 5 minutes and then restart this script to search for the file again.
Sleep, 300000

Reload

The comments in the code give you a basic idea of what is happening. You should not need to change any of the code in this script. The only thing you may need to change is the “Sleep, 120000” after the RemoteHDBSync script is launched. If you have a lot of databases and it takes longer than 2 minute to synchronize with the HanDBase Conduit you can increase this so the script waits longer. During your testing you can guage how long the sync is taking. The last step is to move the RemoteHDBSync.ahk script file to the remoteAction_ran folder you created before and then re-name it with the Date and Time that the script was run. This will enable you to open Dropbox or Google Drive on your Android device and see exactly when the Sync was executed on your computer, like a log of your sync.

Save a copy of this script file somewhere local, I’d recommend in your HanDBase “sync folder”, eg C:\Users\WindowsUser\Documents\HanDBase\. That way you have a backup of it in case something happens to it. You will then save a copy of it to the RemoteActions folder in your Dropbox or Google Drive folder.

  • Dropbox – C:\Users\WindowsUser\Dropbox\RemoteActions\
  • Google Drive – C:\Users\WindowsUser\Google Drive\RemoteActions\

Next create or modify the RemoteHDBSync.ahk script file. The settings in this script assume you have not made any changes to the Install location of the HanDBase Conduit when you installed it. It also assumes that you only have ONE Partnership set up in the HanDBase Conduit, or that the Partnership that will sync with your device is the first one in the list of Partnerships.


; Check to see if the HanDBase Conduit is already open.
IfWinExist Manage Android Users
{
; If the HanDBase Conduit is already open, activate it and bring it to the front of other windows.
WinActivate
}
else
{
; If the HanDBase Conduit is not open, launch it and wait for it to open and come to the front of other windows.
Run %A_ProgramFiles%\HanDBase4\Android\AndroidSync.exe
WinWait Manage Android Users
WinActivate
}

; Wait for 5 seconds to make sure the Conduit window is loaded.
Sleep, 5000

; Set focus to the Users List Box
ControlClick, TListBox1, ahk_Class TEditUsers ;select the drop down menu box

; Read the SyncUserName.txt file to see which user needs to be synched.
Loop
{
FileReadLine, line, SyncUserName.txt, %A_Index%
if ErrorLevel
break
MsgBox, 4, , Line #%A_Index% is "%line%". Continue?
IfMsgBox, No
return
}

; Set the variable to search for in the User list to the Username found in the SyncUserName.txt file.
target = %line%

msgbox, %target%

; Read the Users List and store all Partnership names in an array variable.
Controlget,ItemList,list,,TListBox1, ahk_class TEditUsers ;store the list into ItemList

;you should see your target on this list
Msgbox, %ItemList%

/* I don't think this loop is needed, the list should already be populated.
loop
{
Control, Choose, %a_index%, TListBox1, ahk_class TEditUsers
sleep, 500 ;one can see the control going down the list
}
*/

loop,parse,ItemList,`n, `r ;parses each line of the list, and gets rid of `r just in case
{
If InStr(a_loopfield, target) ;InStr is more forgiving, and == is case sensitive and requires an EXACT match
{
Msgbox, Found %target% on line %A_Index%
LineToSelect:=a_index ;If the Username is found, assign it to a variable to be used later.
break ; Exit the loop
}
}

; Check if a line was found for the user, if so select that user in the list.
if %LineToSelect%
{
Control, Choose, %LineToSelect%, TListBox1, ahk_class TEditUsers
}
else ; If the user was not found then just exit the Conduit and close the script.
{
Sleep, 1500

Send !{F4} ; Send the keyboard command to exit the app.

Sleep, 1500

ExitApp ; Exit the script.
}

; A user was found and selected in the list, wait to make sure the script had time to select the user.
Sleep, 2500

; Click the Synchronize button to begin the sync.
ControlClick, TButton4, ahk_Class TEditUsers

; Wait One Minute for the Synchronization to complete
; Increase this number if in your tests the Sync takes longer
; 60000 = 1 Minute - 120000 = 2 Minutes - 180000 = 3 Minutes - 300000 = 5 Minutes - ETC.
Sleep, 60000

; Exit the HanDBase Conduit
Send !{F4}

; Wait for the HanDBase Conduit to exit.
Sleep, 5000

; Exit the RemoteHDBSync.ahk script so it's no longer running.
ExitApp

Save a copy of this script file somewhere local, I’d recommend in your HanDBase “sync folder” again, eg C:\Users\WindowsUser\Documents\HanDBase\. That way you have a backup of it in case something happens to it. You will then save a copy of it to your Dropbox or Google Drive. Save it to the same RemoteActions folder you saved the RemoteAction.ahk script file to above.

You will then need to create a simple Text file called SyncUserName.txt. In that file you will just have one line. That line will have the exact same name as the Partnership you want to sync with in the HanDBase Conduit. So, if you open the HanDBase Conduit and you see the Partnership name there as MyAndroidDevice you will type MyAndroidDevice on the first line of the SyncUserName.txt file. Do not press Enter/Return. This script assumes there is just one line in that file.

Theoretically you can use this feature to sync multiple users to the same computer/server. Each device would have the SyncUserName.txt file on their device, and subsequently in the Dropbox or Google Drive folder for that user. The Partnership/User name in each file would match the user’s Partnership in the HanDBase Conduit so the script could select the correct one to sync when each users syncs using this tutorial. I have only done some basic testing with this and it would require some additional setup not discussed in the tutorial for this to work at a basic level. It would also require more in depth testing to make sure there are no errors. But it is theoretically possible.

NOTE: In my tests, all of my Android Partnerships in the HanDBase Conduit did NOT have spaces in the Partnership name. This should still work if the Partnership names does contain spaces, eg My Android Device, however this has not been tested. You will want to make sure to test this throughly while at your computer to make sure the correct partnership is created and the sync happens.

Again, save a copy of this text file somewhere local, I’d recommend in your HanDBase “sync folder” again, eg C:\Users\WindowsUser\Documents\HanDBase\. That way you have a backup of it in case something happens to it. You will then save a copy of it to your Dropbox or Google Drive. Save it to the same RemoteActions folder you saved the RemoteAction.ahk and RemoteHDBSync.ahk script files to above.

You will then need to copy these files to your Android device in some way and put them in the RemoteActions folder there. You can do this by connecting your device to your computer or emailing them and downloading them. If you email them you’ll need to more them to the RemoteActions folder.

This updated version of the AutoHotKey syncing will now have you create additional folderpairs in FolderSync to sync these folders. If you want you can let Dropbox or Google Drive copy these files over to your device.

You can now test the scripts while you are at your computer, to make sure they are functioning as designed. To see that the sync is working I’d recommend adding a test record to one of your databases on your Android device. Then Synchronize your Android using FolderSync. When you see confirmation that the files have been Synchronized you can proceed. While testing do not use any other programs or click anywhere on the screen or the sync may fail. Open your Dropbox or Google Drive folder on your computer and double click the RemoteAction.ahk script to launch it. You will see the AutoHotKey icon appear in the Taskbar. Since this is the first time running the script you will see the HanDBase Conduit window open quickly. You will also see another AutoHotKey icon appear in the Taskbar. You will be able to watch the script select your user, if you have more than one, and then move to the Synchronize button and then press Enter to start the sync. You will then see the Conduit progress appear. After a minute or so, unless you changed the time in the RemoteHDBSync.ahk file, you will see the HanDBase Conduit window close. If the script could not find a matching user, the Conduit will exit without you seeing a sync happen. If that does happen, double check the SyncUserName.txt file to make sure the name matches what is in the Conduit. You should then only see one of the AutoHotKey icons in the task bar as well. Wait another couple of minutes and then open your Dropbox or Google Drive folder on your computer and you should see that the RemoteHDBSync.ahk file is no longer there. If you open the remoteAction_ran folder you will see the file in there with the Date and Time the script was run in the file name now. When you open the database on your Android device and your computer you should see the test record you added in both places.

To make it easier to remotely sync, using as few applications and steps as possible I altered this section to put all the files into that RemoteActions folder. You will not set up FolderSync to sync this folder as well.

  • Open FolderSync.
  • Tap Folderpairs
  • Tap the + near the upper right corner to Add a new “Folderpair”.
  • Enter a Unique Name for the “Pair”. I entered “RemoteActionFiles”, for example.
  • Select the Account you are using for this Sync.
  • Select “To remote folder” for the Sync type.
  • Tap the “Remote Folder” field and select the folder you created for your device, eg “MotoX”. Then open the RemoteActions folder there. Tap the Check Mark near the upper right corner to selec this folder for the “Remote Folder”.
  • Tap the “Local Folder” field and locate the RemoteActions folder. If you have a Memory Card, and you put the folder there, you may need to switch to that before you’ll find RemoteActions. Open the RemoteActions folder and then tap the Check Mark near the upper right corner to select that folder.
  • Tap the Sync Options edit option.
  • Uncheck the “Sync subfolders” option.
  • Check the box for “Retry sync if failed”.
  • Change the “If conflicting modifications” option to ” Overwrite oldest”.
  • At this time don’t change any other options, unless you need to be able to Synchronize over 2G/3G/4G. By default it will only sync over WiFi.
  • Tap the Save option to save changes.
  • Tap the “Sync” button to synchronize the files.

NOTE: If you have not yet copied the files to your Android device you can first set the “Sync Type” to “To local folder”. Then on the first sync it will copy those files from your Cloud Storage to the device. If you choose to do this you will then need to change the “Sync Type” back to “To remote folder” for this to work properly.

The Setup for Remote, Automatic syncing using Dropbox or Google Drive is now finished.

There will be one additional step needed when you are ready to sync your databases to the cloud and then your computer. For remote, automatic syncing to work you will need to run the Sync for this “RemoteActionFiles” folderpair AFTER you sync your HanDBase files with FolderSync. The reason for this is because of the option in the script to move the RemoteHDBSync.ahk file to the remoteAction_ran folder after a successful sync. Adding this step with the above options will automatically sync that file back to your computer so the actual syncing occurs. It will then be moved and renamed as mentioned above. If you don’t do this the actual sync with HanDBase won’t happen on your computer. If you Sync this folderpair first though, the sync on the computer may happen before your HanDBase files have been fully synched to it through Dropbox or Google Drive. Please be sure to test this yourself to make sure it is working properly.

You can add an additional folderpair in FolderSync to sync the renamed script files back to your device, so you can see the Dates and Times the sync was run. With Dropbox and Google Drive this is optional as you can check for those in the apps themselves. If you do want to do that though, follow the steps above for the “RemoteActionFiles” but give it a different name, I used “RemoteActionLogs”. Then follow the same steps above but use “To local folder” for the “Sync Type” instead. All the other options are the same. Then simply run that Sync AFTER you follow the instructions to initiate the Automatic, Remote sync in the last section of the tutorial to copy those files to your device.

Download the AutoHotKey Script files

If you don’t want to create the AutoHotKey Script files yourself you can click the link below to download the default ones that I created. Clicking the link below will download a .ZIP file that contains the RemoteAction.ahk and RemoteHDBSync.ahk Script files and a sample SyncUserName.txt file. You can modify these as needed or use them as is.

Download Updated AutoHotKey Script Files

One Response to this post.

  1. […] Tutorial Update – Enhanced AutoHotKey Scripts when Remotely Syncing HanDBase Database for Android … […]

Respond to this post

You must be logged in to post a comment.