How to pass path names to Python script by “dropping” files/folders over script icon

后端 未结 2 2167
粉色の甜心
粉色の甜心 2021-02-15 23:40

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 fi

2条回答
  •  逝去的感伤
    2021-02-16 00:17

    Just use the "Build Applet" utility:

    /Developer/Applications/Utilities/MacPython\ 2.5/Build\ Applet.app
    

    and the dropped file paths will be available thru sys.argv.

    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-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.

提交回复
热议问题