I've created an .app (a macOS bundle) where the main executable is a Bash script, following the instructions I found in StackOverflow and other places. It works perfectly except for the fact that when I double click on a file associated with the .app, the script is run but it doesn't get the clicked file name as an argument.
Looks like the problem is that the script doesn't handle the "OpenFile" event, but I don't know if there's a way where the user double-clicks a file and the file name is passed to the .app bundle executable as a command line parameter and not through some event.
#! /usr/local/bin/bash
source ~/.bashrc
python3 final_script.py $1
# Above, "$1" is empty. I've tried some variations,
# including not running the second script, to no avail. 
I know I can use py2app to achieve something similar, or Platypus, or Automator, etc. but using a Bash script is better for my workflow and also I'm curious about how macos passes parameters to apps when a file is double-clicked.
Thanks a lot in advance!
Finally I found the way. Simpler than I thought, and it was not easy to find but...
The Bash launcher won't get anything in the command line because that's NOT the way macOS handles arguments in app bundles, it uses Apple Events. I didn't know this, and it's my fault, my lack of expertise in macOS.
Turns out tkinter actually supports Apple Events, at least the odoc one, which is the one the app bundle gets when the user double clicks a document to be opened by an app.
So, what I did was modifying final_script.py, adding the following code:
import sys
import tkinter
def handle_opendocument(widget, *args):
    message = ''
    for arg in args:
        message += str(arg) + '\n'
    widget.configure(text=message.rstrip())
...
# More code here...
...
root = tkinter.Tk()
root.title('Testing AppleEvent handling in Python/Tk')
root.createcommand('tk::mac::OpenDocument', lambda *args: handle_opendocument(label, *args))
label = tkinter.Label(root)
label.configure(text='Starting up...')
label.pack()
root.mainloop()
Of course, the real handle_opendocument function in my app does more things, but I wanted to show the bare minimum necessary to make this work. I hope is helpful!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With