开发者

How to auto-run a script

I created a script that will tell me what to wear in the morning based on the weather (i.e. rain slicker if it will rain, heavy jacket if it will be cold, etc). I have fairly basic programming experience with python and the script works perfectly, but I want to be able to create a file that I can just double-click from my desktop and the script will automatically run.

My goal is to be able to simply double click [something] in the morning and it will autom开发者_高级运维atically run the script and thus tell me what to wear. How could I go about doing this?

System Specifications:

  • python
  • Mac OSX


This worked for me on Snow Leopard:

-Put the python script on the desktop.

-Right click on the script file, and choose "Get info"

-Find "Open With", and choose "Python Launcher" from the dropdown box

Now double-clicking the script file will run the script in a new terminal window.

I'm not sure what versions of OS X come with the Python Launcher application. If you don't have that, you can solve it with a couple extra steps:

-Put the python script anywhere

-Create a shell script on the desktop with one line:

python "/Users/john/scripts/what-to-wear.py"

(Where I've assumed your script is called what-to-wear.py and is in /Users/john/scripts. Be aware that you do need to use an absolute path.)

-Make the shell script executable. In a terminal:

chmod 755 what-to-wear-shell-script

-Double clicking the shell script should run it in a terminal, running your python script.


What you want to do is create an executable file.

I've never used a Mac or Python, but look at this question and the first answer:

How can I create a directly-executable cross-platform GUI app using Python?

Seems http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html is what you're looking for


Use a batch file to make it automatic

Example : 1. Open Notepad -> type the following. This one's for Windows..It might give you a hint :start C:\Python34\python.exe(your python file location)Your *.py file location.

:end Save this with a *.bat extension That's it ..you can configure more on this batch,I guess batch is the automation for day to day script


In Linux/unix based OS , add #!/usr/bin/python3 line on top of your script file with extension .py , if you have python version 3. Or change it to the version installed in the machine Further , make the file executable by

sudo chmod +x <fileName>

for windows, add windows python path and make the file executable


You want the script to download the weather information online and output the clothes based on your predefined rules?

If this is the case, use urllib to download the page and do some ad hoc parsing over the downloaded html page to get the whether information. And write your logic using nested IF THEN ELSE blocks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜