Shell script to launch Moneydance from Terminal Command line

Stuart Beesley (Mr Toolbox)'s Avatar

Stuart Beesley (Mr Toolbox)

12 Mar, 2021 06:45 AM

Attached is a (zipped) shell script that allows you to launch Moneydance from the terminal command line in a manner that replicates launching from the installed app icon. At this point I must send credit and thanks to hleofxquotes who provided me with the initial framework (as I was stuck). This has been written for Mac, but can easily be adopted for Windows and Linux. I've tried before, but invariably it wouldn't work, or Moneybot wouldn't work, or the base folders were different to when running from the icon (and thus it wasn't loading config.dict).

I'm uploading this shell script as an example for other who might want to do this.

UPDATE: Please visit my GitHub site for the latest version of this code: https://yogi1967.github.io/MoneydancePythonScripts/

This script sets up your environment to be virtually identical to when running from the icon. There is one thing it cannot do, and that is to run within a Mac App sandbox. I've tried, but you need to build an app launcher for that, and then why bother as we already have the MD App... But this script does replicate the folder structures that you get when running in a sandbox....

So why would you bother:
1. Ability to run without Mac sandbox restrictions (access to all folders, no silly Gatekeeper jffi*.dylib messages with Python)
2. Ability to launch MD with parameters - e.g. -d for debug mode or -invoke_and_quit=x or importfilename
3. Ability to swap in your own java libraries (assuming you are a developer)

You need to do a couple of things first (all documented within the script):
- Download/install Java FX (allows Moneybot Console) to run: https://gluonhq.com/download/javafx-15-0-1-sdk-mac/ - Download/install OpenAdoptJDK (Hotspot) v15: https://adoptopenjdk.net/?variant=openjdk15&jvmVariant=hotspot - Edit the script with the correct install folder paths

  • Update the script with your own user path - this is critical

  • The script as is will launch the installed \Applications\Moneydance.app package, but you might for example have different versions, so update the name accordingly...

The rest is for you to play and experiment with... If it works for you, let me know?

Try Toolbox 🧰 and my other extensions...:
https://yogi1967.github.io/MoneydancePythonScripts/

  1. 1 Posted by tgilbert666 on 14 Mar, 2021 02:47 AM

    tgilbert666's Avatar

    Thanks Stuart. I've book-marked this. I'm sure I'll have cause to investigate it one day!

  2. 2 Posted by Ric Werme on 19 Mar, 2021 02:00 PM

    Ric Werme's Avatar

    It's good to see -invoke_and_quit is still around, I used that in my old networth.py hack. Do you know if there's a mechanism to pass parameters to the python script to run? I haven't investigated yet.

  3. 3 Posted by Stuart Beesley ... on 19 Mar, 2021 07:29 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    I haven't tested it but my read of the code was that whilst it detects a PY script, it actually ignores this and doesn't run it - needs testing.. If you do test, let me know? As regards invoke and quit, you pass the URI to all an extension and in this you pass a parameter.... (again, not tested)

    (not support, just a fellow user)

  4. 4 Posted by Ric Werme on 20 Mar, 2021 01:57 AM

    Ric Werme's Avatar

    I will be taking a closer look at this, likely pretty soon, as I have my old networth program actually running already, somewhat to my amazement.

    I did notice the script has a lot of Mac-specific stuff, I'm running on Linux. I've been using the MoneyBot Python console for all this. I had had been running my program with this:

    /opt/Moneydance/moneydance -invoke_and_quit moneydance:fmodule:jpython:runfile?=/home/werme/moneydance/networth.py

    This current system wants to start Moneydance with /opt/Moneydance/Moneydance. It's a Linux-specific shell script, I'll pass on anything that seems interesting.

  5. 5 Posted by Ric Werme on 13 Apr, 2021 01:09 PM

    Ric Werme's Avatar

    Yeah, your script is way more Mac specific than I want to deal with. e.g. paths like "-Duser.dir="${my_user_path}/Library/Containers/com.infinitekind.MoneydanceOSX/Data" simply don't exist anywhere on my Linux system. I'll abandon it.

    FWIW, on my Linux installation (run as root), the startup script expands into an equally ugly command line. With spaces and colons replaced with line breaks, I ultimately see:

    $ cat Desktop/Moneydance.desktop
    #!/usr/bin/env xdg-open
    [Desktop Entry]
    Type=Application
    Name=Moneydance
    Exec=/bin/sh "/opt/Moneydance/Moneydance"
    Icon=/opt/Moneydance/.install4j/Moneydance.png

    $ /bin/sh -x "/opt/Moneydance/Moneydance"
    ...
    exec \
     /opt/Moneydance/jre/bin/java \
     -client \
     -Dinstall4j.jvmDir=/opt/Moneydance/jre \
     -Dexe4j.moduleName=/opt/Moneydance/Moneydance \
     --add-modules \
     javafx.swing,javafx.controls,javafx.graphics \
     -Dinstall4j.launcherId=4 \
     -Dinstall4j.swt=false \
     -Di4jv=0 \
     -Di4jv=0 \
     -Di4jv=0 \
     -Di4jv=0 \
     -Di4jv=0 \
     -Xmx1024m \
     -Di4j.vpt=true \
     -classpath \
     /opt/Moneydance/.install4j/i4jruntime.jar:
    /opt/Moneydance/lib/activation.jar:
    /opt/Moneydance/lib/antlr-3.2.jar:
    /opt/Moneydance/lib/appsrc.jar:
    /opt/Moneydance/lib/bcprov-jdk16-146.jar:
    /opt/Moneydance/lib/commons-lang3-3.5.jar:
    /opt/Moneydance/lib/commons-logging-1.2.jar:
    /opt/Moneydance/lib/darcula.jar:
    /opt/Moneydance/lib/dropbox-core-sdk-3.0.11.jar:
    /opt/Moneydance/lib/gson-2.5.jar:
    /opt/Moneydance/lib/httpclient-4.5.6.jar:
    /opt/Moneydance/lib/httpcore-4.4.10.jar:
    /opt/Moneydance/lib/httpmime-4.5.6.jar:
    /opt/Moneydance/lib/i4jruntime.jar:
    /opt/Moneydance/lib/jackson-core-2.7.4.jar:
    /opt/Moneydance/lib/jcommon-1.0.16.jar:
    /opt/Moneydance/lib/jfreechart-1.0.13.jar:
    /opt/Moneydance/lib/json_simple-1.1.jar:
    /opt/Moneydance/lib/junit-4.10.jar:
    /opt/Moneydance/lib/mdpython.jar:
    /opt/Moneydance/lib/moneydance.jar:
    /opt/Moneydance/lib/natty-0.3.jar:
    /opt/Moneydance/lib/PDFRenderer.jar:
    /opt/Moneydance/lib/sqlitejdbc-v056.jar:
    /opt/Moneydance/lib/swingx-core-1.6.2.jar:
    /opt/Moneydance/lib/TableLayout-bin-jdk1.5-2007-04-21.jar \
     com.install4j.runtime.launcher.UnixLauncher \
     launch \
     [hex number that might be a license key] \
     0 \
     0 \
     Moneydance

    As for your observation "my read of the code was that whilst it detects a PY script, it actually ignores this and doesn't run it - needs testing"

    So far I agree completely. As I mentioned elsewhere, I'll explore further and likely start a new discussion. I will note that what would have worked in 2007 doesn't work now:

    $ /bin/sh "/opt/Moneydance/Moneydance" "-invoke_and_quit moneydance:fmodule:jpython:runfile?=/home/werme/moneydance/hello.py"
    ignoring invalid argument: -invoke_and_quit moneydance:fmodule:jpython:runfile?=/home/werme/moneydance/hello.py

    I thought I saw it run without the error last night (and with no visible "Hello, world" output), but I may have had Moneydance already running. The shell script may have been a no-op.

    Likely not today - I have too many things to do that I set aside while trying to get on speaking terms with Java Swing.

  6. 6 Posted by Ric Werme on 13 Apr, 2021 01:20 PM

    Ric Werme's Avatar

    One more on invoke_and_quit, see the old (and stale, it refs changes in 2019) but interesting https://infinitekind.tenderapp.com/discussions/problems/47682-windows-md-20178-1691-command-line-interface-cli-script-bugs#comment_46147450

  7. 7 Posted by hleofxquotes on 13 Apr, 2021 04:28 PM

    hleofxquotes's Avatar

    $ /bin/sh "/opt/Moneydance/Moneydance" "-invoke_and_quit moneydance:fmodule:jpython:runfile?=/home/werme/moneydance/hello.py" ignoring invalid argument: -invoke_and_quit moneydance:fmodule:jpython:runfile?=/home/werme/moneydance/hello.py

    Likely due to quoting issue,

     "-invoke_and_quit moneydance:fmodule:jpython:runfile?=/home/werme/moneydance/hello.py"
    

    is being treated as single argument. Also using " with special char such as ? could be problematic due to shell expansion.

    My suggestion try

    ...  '-invoke_and_quit' 'moneydance:fmodule:jpython:runfile?=/home/werme/moneydance/hello.py'
    
  8. 8 Posted by Stuart Beesley ... on 13 Apr, 2021 04:36 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    A few things:

    • Yes my script is Mac specific, but you should be able to customise it easily. I suggest you run my toolbox extension and get the file paths from the diagnostic display and then hardcode them into the script.

    • -invoke_and_quit DOES work; so does -invoke (no quit)... I used it to call my own extension. As the 'wizard' says, you can separate the parameter into a separate argument and quote it.. Try that. HOWEVER, it needs to call your extension.. I don't think 'jpython' will work.

    • the argument option to pass a python script doesn't work... So you need to invoke your extension.

    I am thinking of writing a generic python extension that users can invoke to run scripts from the command line....

  9. 9 Posted by Ric Werme on 13 Apr, 2021 07:11 PM

    Ric Werme's Avatar

    @ hleofxquotes:

    Hey, I just cut & pasted something that worked in the old Jython interface in 2007.

    The quoting is likely ineffective, when shells can't do filename expansion (i.e. that '?'), they either report the error or pass the string as typed. The double quotes disabled the filename expansion anyway. [They would permit expanding $foo, single quotes would prevent even that.] I never did understand why there was that '?' anyway. I like the suggestion to make '-invoke_and_quit' stand alone I'll play with that, maybe after this Jackson Lab webinar that just started. It may well be that the old startup script managed to split those words (passing quoted strings around shell scripts gets weird).

    I had also tried replacing 'jython' with 'python', no good, but per the above, that may be expected.

  10. 10 Posted by Stuart Beesley ... on 13 Apr, 2021 07:33 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Well... I just used my launch script with these parameters:

    ./launch-moneydance.sh "/Users/stu/Library/Containers/com.infinitekind.MoneydanceOSX/Data/Documents/FAKE2.moneydance" "-invoke=moneydance:fmodule:toolbox:hello?bob"```
    

    and MD launched and called my toolbox.invoke() - see response in console:

    toolbox: @@ invoke script invoked... Parameter: hello?bob
    

    So the ? worked when the whole argument was wrapped in quotes..

    NOTE - there used to be an internal extension called jpython - this was the way that MD ran jython - BUT IT CHANGED and became core code (Moneybot etc) and hence there is NO jpython extension you can invoke. You have to call your own extension...

  11. 11 Posted by Ric Werme on 13 Apr, 2021 09:25 PM

    Ric Werme's Avatar

    Okay, -invoke_and_quit is happier by itself on the command line, as is -invoke.

    neither gripes that moneydance:fmodule:python:runfile=/home/werme/moneydance/xxx.py' has either a bogus whatever its is to the left of '=' or that xxx.py doesn't exist.

    So, if I need an extension to call my code, it looks like that will be a bigger learning curve than I can deal with for a couple weeks.

    I guess your https://raw.githubusercontent.com/yogi1967/MoneydancePythonScripts/master/source/extension_tester/readme.txt is the best starting point.

    I see in https://yogi1967.github.io/MoneydancePythonScripts/ says "My scripts and extensions are identical. The extension version(s) are simply a ‘packaged’ script version."

    That sounds easy, bu the former page says "the mxt contains your *.py file(s) and script_info.dict at root level, and then..: - meta_info.dict in a sub directory structure called ./com/moneydance/modules/features/extension_name/

    I see elsewhere I have to change networth.py to make it runnable as an extension. I think I see a learning curve....

    Let me know if you write an extension that runs a python script.

      -Ric

  12. 12 Posted by Stuart Beesley ... on 13 Apr, 2021 09:48 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Ric. Do you want to email me directly? Perhaps send me your networth.py file

  13. 13 Posted by Ric Werme on 14 Apr, 2021 12:28 PM

    Ric Werme's Avatar

    I'll do that. I spent most of Monday cleaning up code and dealing with a couple loose ends, I have to prepare a mailing list for the local Mensa newsletter, a task that's more messier in April than most months, and get busy buying a new car, and prepping for an exhaust fan in the garage, etc....

    Give me a couple days, remaining loose ends won't take long or can be deferred.

  14. 14 Posted by Stuart Beesley ... on 14 Apr, 2021 12:55 PM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Don't worry about cleaning it up, just email it to me directly. I will send you back a way to update the code as you see fit whenever you are ready.... S

  15. System closed this discussion on 14 Jul, 2021 01:00 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac