如何拖放到OS X上的(python)脚本?

时间:2022-04-29 07:12:01

Is there any way to configure a recent OS X system so that python scripts automatically behave as drag and drop targets? If not, what is the most light-weight way (ideally scriptable) of configuring individual scripts for this behavior?

有没有办法配置最近的OS X系统,以便python脚本自动表现为拖放目标?如果没有,为这种行为配置单个脚本的最轻量级方法(理想情况下是可编写脚本的)是什么?

On Windows, I can simply drag and drop files onto a python script, and a python process will be launched with the full path names of the dropped arguments available in sys.argv. Amazingly, not only does this not happen on OS X, but there doesn't seem to be any way of configuring the system to do this automatically. All the solutions I've come across involve wrapping each of my scripts in an app.

在Windows上,我可以简单地将文件拖放到python脚本上,并且将启动python进程,其中包含sys.argv中可用的已删除参数的完整路径名。令人惊讶的是,这不仅不会发生在OS X上,而且似乎没有任何方法可以配置系统自动执行此操作。我遇到的所有解决方案都涉及将每个脚本包装在一个应用程序中。

Sure, this question has been asked and answered before. But not only is every single answer I could find limited to enabling drag and drop for one script at a time (by wrapping an app around them); it seems that all answers are obsolete and/or depend on broken links. Here's a sampler:

当然,这个问题之前已经被问过并得到了回答。但是,我发现的每个答案不仅限于一次启用一个脚本的拖放(通过将应用程序包裹在它们周围);似乎所有答案都已过时和/或依赖于断开的链接。这是一个采样器:

As mentioned, none of them seem to work anymore. I have OS X 10.8, Mountain Lion, and I've seen zilch that could be expected to work on later OS versions. Any up to date solutions?

如上所述,它们似乎都不再起作用了。我有OS X 10.8,Mountain Lion,我看过zilch可能会在以后的OS版本上运行。任何最新的解决方案?

PS: Conceptually this question is not necessarily limited to python: I'm pretty sure that any mechanism that would work with shell or perl scripts would also work with python. But given how hard this seems to be on OS X, I'm keeping the question specific since that's my use case, and all solutions I've seen seem to be language-specific.

PS:从概念上讲,这个问题不一定限于python:我很确定任何可以使用shell或perl脚本的机制也适用于python。但考虑到在OS X上看起来有多么困难,我保持这个问题具体,因为那是我的用例,我见过的所有解决方案似乎都是特定于语言的。

1 个解决方案

#1


Have you seen this answer? Try using Mac OS X Automator (available in current and past versions of the OS--just tested this in Yosemite). As far as "one at a time" vs "multiple", this SuperUser post about the same subject explains how to make it work for multiple files.

你看到这个答案吗?尝试使用Mac OS X Automator(可在当前和过去的操作系统版本中使用 - 仅在Yosemite中测试过)。至于“一次一个”与“多个”,这个关于同一主题的超级用户帖子解释了如何使其适用于多个文件。

At the end of the day, "Drag and Drop Support" is functionality of the Windowing System and has nothing to do with Python. That said, you can build OS X GUI apps in Python, which could be another solution to this. Check out this link about GUI Applications in Python for more info.

在一天结束时,“拖放支持”是Windowing System的功能,与Python无关。也就是说,您可以在Python中构建OS X GUI应用程序,这可能是另一种解决方案。有关更多信息,请查看此链接有关Python中的GUI应用程序。

#1


Have you seen this answer? Try using Mac OS X Automator (available in current and past versions of the OS--just tested this in Yosemite). As far as "one at a time" vs "multiple", this SuperUser post about the same subject explains how to make it work for multiple files.

你看到这个答案吗?尝试使用Mac OS X Automator(可在当前和过去的操作系统版本中使用 - 仅在Yosemite中测试过)。至于“一次一个”与“多个”,这个关于同一主题的超级用户帖子解释了如何使其适用于多个文件。

At the end of the day, "Drag and Drop Support" is functionality of the Windowing System and has nothing to do with Python. That said, you can build OS X GUI apps in Python, which could be another solution to this. Check out this link about GUI Applications in Python for more info.

在一天结束时,“拖放支持”是Windowing System的功能,与Python无关。也就是说,您可以在Python中构建OS X GUI应用程序,这可能是另一种解决方案。有关更多信息,请查看此链接有关Python中的GUI应用程序。