Tuesday, February 24, 2015

Simple lock file scripts for Moneydance

I created these wrapper scripts for Linux (CentOS 7) and Windows PowerShell to implement a very simple file locking mechanism for use with Moneydance 2015.  You just get a warning about the lock file and have to either close the other Moneydance client or manually remove the lock file.  Below is the Linux script followed by the Windows PowerShell script.

*****************************
*****************************

#!/bin/sh

# I named this file "startMD.sh" and made it executable

# This option makes your script bail out when it detects an error (a command exiting with a non-zero exit code).
set -e

# Variables
MDLOCKFILE="/mnt/fileserver/moneydance/yourLock.txt"
MDPROGRAM="/opt/Moneydance/Moneydance"


# If the lock file exists, then print a warning and skip running Moneydance
if [ -f $MDLOCKFILE ]; then
    echo "!!! Warning, \"$MDLOCKFILE\" already exists !!!"
    echo
    echo "This usually means that John Doe already has this file open.  Ensure no other copies of Moneydance are running, then delete this lock file and try opening Moneydance again."
    echo
    echo "cat $MDLOCKFILE"
    cat $MDLOCKFILE
    echo

    # Send a notification to Gnome (useful if you run this script from a Gnome shortcut)
    notify-send "!!! Warning, \"$MDLOCKFILE\" already exists !!!"

    # I used to pause the script and wait for input to acknowledge this warning
    #read -n1 -r -p "Press any key to quit now (Moneydance will not be started)" key
# Else create the lockfile and run Moneydance
else
    # Create lockfile
    echo "Lockfile created on" `date` from computer `hostname` >> $MDLOCKFILE

    # Run Moneydance
    echo "Starting Moneydance with this command:"
    echo $MDPROGRAM
    $MDPROGRAM
    echo
    echo "Mondeydance finished"
    echo

    # Remove lockfile
    rm $MDLOCKFILE
fi

exit 0

*****************************
*****************************

# I named this file "startMD.ps1"
# Run with this command:
#    powershell -executionPolicy bypass -file "startMD.ps1"

# Variables

$MDLOCKFILE="C:\moneydance\yourLock.txt"
$MDPROGRAM="C:\Program Files\Moneydance\Moneydance.exe"

# Next line is required to create the message box.  Remove the leading [void] to have more details output to the console
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")


# If the lockfile exists don't run Moneydance
If (Test-Path $MDLOCKFILE) {
    echo "!!! Warning, ""$MDLOCKFILE"" already exists !!!"
    echo ""
    echo "This usually means that Jane Doe already has this file open.  Ensure no other copies of Moneydance are running, then delete this lock file and try opening Moneydance again."
    echo ""
    echo "type ""$MDLOCKFILE"""
    type "$MDLOCKFILE"
    echo ""
    [void] [System.Windows.Forms.MessageBox]::Show("""$MDLOCKFILE"" already exists!  Ask Jane for help (see console for details).  If Jane isn't available just click OK to exit.", "!!! WARNING !!!")
    }
# Else run create the lockfile and run Moneydance
Else {
    # Create lockfile
    Get-Date -Format g >>$MDLOCKFILE
    echo "Lockfile created from computer:">> $MDLOCKFILE
    hostname >>$MDLOCKFILE

    # Run Moneydance
    echo "Starting Moneydance with this command:"
    echo """$MDPROGRAM"""
    Start-Process -Wait "$MDPROGRAM"
    echo ""
    echo "Mondeydance finished"

    # Remove lockfile
    rm $MDLOCKFILE
    }

*****************************
*****************************

No comments: