简体   繁体   English

当bundle可执行文件是Bash脚本时,将参数传递给macos app

[英]Passing arguments to macos app when bundle executable is a Bash script

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. 我已经创建了一个.app(一个macOS包),其中主要的可执行文件是一个Bash脚本,遵循我在StackOverflow和其他地方找到的指令。 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. 它完美地工作,除了当我双击与.app相关联的文件时,脚本运行但它不会将单击的文件名作为参数。

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. 看起来问题是脚本没有处理“OpenFile”事件,但我不知道是否有用户双击文件并将文件名作为传递给.app包可执行文件的方式命令行参数而不是通过某些事件。

#! /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. 我知道我可以使用py2app来实现类似的东西,或Platypus或Automator等,但使用Bash脚本对我的工作流程更好,而且我很好奇macos如何在双击文件时将参数传递给应用程序。

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. Bash启动程序不会在命令行中获取任何内容,因为这不是macOS处理应用程序包中的参数的方式,而是使用Apple Events。 I didn't know this, and it's my fault, my lack of expertise in macOS. 我不知道这一点,这是我的错,我缺乏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. 原来tkinter居然还支持Apple事件,至少odoc一个,这是一个当用户双击文档由一个应用程序打开的应用程序软件包获得。

So, what I did was modifying final_script.py , adding the following code: 所以,我所做的是修改final_script.py ,添加以下代码:

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. 当然,我的应用程序中真正的handle_opendocument函数做了更多的事情,但我想展示使这项工作所需的最低限度。 I hope is helpful! 我希望对你有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM