如何通过“丢弃"将路径名传递给Python脚本脚本上的文件/文件夹图标 [英] How to pass path names to Python script by "dropping" files/folders over script icon
问题描述
我在Mac OS X中工作,一直在用Python编写简单的文件/文件夹复制脚本.是否可以将文件夹拖放到Python脚本图标上方,并将文件或文件夹的路径作为脚本中的参数传递?
I am working in Mac OS X and have been writing simple file/folder copy scripts in Python. Is there a way to drag and drop a folder on top of a Python script icon and pass the file or folder's path as an argument in the script?
当前,我有一个AppleScript Droplet(支持拖放),它将放置的文件夹和文件的路径传递给Python.但是,我只希望有一个Python脚本,而不是AppleScript和Python脚本.
Currently, I have an AppleScript droplet (which supports drag and drop) that passes the paths of dropped folders and files to Python. However, I would like to have just one Python script instead of an AppleScript and a Python script.
任何帮助将不胜感激.
推荐答案
只需使用构建小程序"实用程序:
Just use the "Build Applet" utility:
/Developer/Applications/Utilities/MacPython\ 2.5/Build\ Applet.app
和删除的文件路径将通过sys.argv可用.
and the dropped file paths will be available thru sys.argv.
请注意,您可能必须使用Python2.5(或修补版本)- 请参阅此注释: https://bitbucket.org/ronaldoussoren/py2app/issue/16/argv-emulation-code-needs-rewrite
Note that you may have to use Python2.5 (or a patched version) -- See this note: https://bitbucket.org/ronaldoussoren/py2app/issue/16/argv-emulation-code-needs-rewrite
快速示例-编辑此文件并将其放在您的桌面上:
Quick example -- edit this file and put it on your desktop:
#!/usr/bin/python2.5
import sys
print sys.argv
按住Control键单击它,然后选择构建小程序(2.5.4)"打开
Control-click on it, and select open with "Build Applet (2.5.4)"
应用程序图标将出现在桌面上.
App icon will appear on desktop.
打开控制台实用程序&清除显示.
Open Console Utility & clear display.
将某些文件拖放到应用程序"图标上-您将在控制台窗口中看到打印内容.
Drop some files onto App icon -- you'll see the print in the console window.
这篇关于如何通过“丢弃"将路径名传递给Python脚本脚本上的文件/文件夹图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!