'open file (e.g. right click/ open with) event for python3 script on a mac
I would like my python3 script to react to an open file event. Is there any way to achieve that? The following python3 script is named example
#!/path/to/python3
from PyQt5.QtWidgets import QApplication, QWidget
def window():
app = QApplication(sys.argv)
widget = QWidget()
widget.setWindowTitle("Simple example")
widget.show()
sys.exit(app.exec_())
if __name__ == '__main__':
window()
I generated the following directories to form a bundle:
example.app/
-- Contents/
-- Info.plist
-- MacOS/
-- example
The Info.plist file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>example</string>
</dict>
</plist>
However, while I can execute it by, e.g., double clicking the .app file itself, I could not have it react to 'open file'.
The following apple script wrapper works, but is quite ugly:
on open theDroppedItems
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
set posixfile to (POSIX path of theCurrentDroppedItem)
# display dialog posixfile
do shell script "/path/to/python3 /path/to/python3script " & posixfile
end repeat
end open
It 'converts' open file to a command line argument. Supposedly, Qt could work and Tk as well (l.90), but I could not find a simple example implementing it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|