Running a file automatically on mac [duplicate] - python

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

Related

Run a Python script from a .command file with Spotlight on MacOS

I am trying to run a Python script from 'Automate the Boring Stuff' on MacOS by opening a .command file with Spotlight. I have been able to successfully run the script in Terminal by navigating to the directory containing the .py file and then entering: python3 mclip.py [keyword] , but when trying to run it with my mclip.command file, terminal opens a new window and displays this:
I'm not sure why it says no such file or directory, because that file definitely exists in said directory. Additionally, even if mclip.command did run successfully, how would I pass to it the [keyword] argument?
The contents of my mclip.command file are:
#!/usr/bin/env bash
python3 /path/to/my/pythonScript.py
and i'm attempting to run it by opening spotlight and typing in 'mclip.command'.
How can I make this work so that I don't have to navigate to the folder containing the .py file every time I want to run the script?
If you want more context, here are some links to the instructions I've been following: First is the instructions for the script itself is under Project: Multi-Clipboard Automatic Messages, and the instructions for creating and running the .command file are under Running Python Programs on MacOS.
Once you have created your mclip.command file:
Go to the Terminal and type cd /your/folder/location/where/command/file/located/. This changes the current directory where the your command file is located.
Then type chmod u+x mclip.command. This will make your file executable.
Now you should be able to run your script on Spotlight. Either:
Press Cmd + space on your keyboard or clic on the magnifying glass on the upper-right corner.
type mclip.command (or just mclip and Spotlight will do the rest) and press enter.
I still have a problem though:
I am unable to pass arguments to my command file on Spotlight.
I would like to do something like, Go to Spotlight and type:
First mclip.command save <keyword> in order to save what is copied in the clipboard to the key.
Then mclip.command <keyword> in order to reuse what has previously been saved.
It seems to be very easy to do it on Windows (press Win-R and type mcb save <keyword>) but somehow it does not seem possible on Mac OS.
Or is it?
I'd love to find a solution or at least a workaround to quickly execute scripts passing arguments directly on Spotlight...
Let us know if you have the answer =)

How to execute a python script over a file when double clicking on it in ubuntu

I have a python script that can open a particular kind of a file. It is an SPE file, opened using with the spe2py library (https://pypi.org/project/spe2py/).
I'd like, when I double click on a file of that kind, to launch the script passing to it the path of the file where I clicked. In this way my script could show the content of that file. How could I do to do that?
I am using Linux, Ubuntu 18.04.
Thanks,
I use Linux Mint which is based on Ubuntu but it uses MATE (desktop environment) with file manager Caja (previously Nemo/Nautilus) so last point (5) can be different on Ubuntu or other Linux.
You need few things:
script has to use sys.argv to get selected filename
you have to add shebang in first line of script so system will know to use Python to run this script
#!/usr/bin/env python
you have to set it executable so system will use shebang to run it (and you don't have to use python script.py)
chmod +x script.py
you could put script in folder which is in PATH so system can run it in any folder without using full path to script. On PATH could be folder ~/bin for your scripts - so you can put script in this folder.
in file manager Nemo/Nautilus/Caja you can right click on SPE file to see menu - there should be "Open with ... > Other program" and you can set your script. You can use full path if you didn't put script in folder from PATH. If you use different file manager then you will have to search similar option
Nemo/Nautilus/Caja also sets variables with names of selected/highlighted files so you can run script with all files at once.
CAJA_SCRIPT_NEXT_PANE_SELECTED_URIS
CAJA_SCRIPT_NEXT_PANE_SELECTED_FILE_PATHS
CAJA_SCRIPT_NEXT_PANE_CURRENT_URI
CAJA_SCRIPT_SELECTED_URIS
CAJA_SCRIPT_SELECTED_FILE_PATHS
CAJA_SCRIPT_CURRENT_URI
In script they should be avaliable
import os
print( os.environ['CAJA_SCRIPT_NEXT_PANE_SELECTED_URIS'] )
or safer if variable doesn't exists
import os
print( os.environ.get('CAJA_SCRIPT_NEXT_PANE_SELECTED_URIS') )
EDIT: 2021.08.05
Currently Python 3 is main used version and on some systems it may need python3 instead of python in shebang to work with Python 3
I would suggest you to run your python script using bash script program.
You can make your bash script like this:
Blockquote
#!/bin/sh
python3 python_script.py
Save this text file with .sh extension

create an application in excel using python for a user without python

I am doing an application in Excel and I'd like to use python language. I've seen a pretty cool library called xlwings, but to run it a user need to have python installed.
Is there any possibility to prepare this kind of application that will be launch from a PC without Python?
Any suggestion are welcome!
A small workaround could be to package your application with cx_freeze or pyinstaller. Then it can run on a machine without installing python. The downside is of course that the program tend to be a bit bulky in size.
no. you must need install a python and for interpreting the python function etc.
It is possible using xlloop. This is a customized client-server approach, where the client is an excel .xll which must be installed on client's machine.
The server can be written in many languages, including python, and of course it must be launched on a server that has python installed. Currently the .xll is available only for 32 bits.
This might not be exactly what you're looking for (i.e.- "without python"), but you could ship it as a virtualenv .zip folder with all the necessary packages and respective python installation included in the folder... and also include a run.command bash executable inside the folder that the user can click on to run your program. You'll have to tell the user (inside a README.txt or something) how to enable the executable... For a particular application mine looked like...
Please complete the following steps after installation of MyApplication.zip:
Step 1: Move the MyApplication folder to your Desktop.
Step 2: Open your Terminal in Go then Applications then Utilities.
Step 3: In the Terminal, paste cd ~/Desktop/MyApplication
then press Enter.
Step 4: Then, paste chmod +x run.command
then press Enter.
From now on, clicking the “run” file in MyApplication will run the application.
If clicking the “run” file still doesn’t work, then do…
Click Ok on the popup then Open System Preferences then Security & Privacy
then Click on Open Anyway button then Click Open button on the popup.
NOTE: Please do not move or delete the MyApplication folder, or any of its files.
Then my run.command executable looked like the following:
cd ~/Desktop/MyApplication
PATH=$PATH:$HOME/Desktop/MyApplication/lib/python2.7/site-packages:$HOME/Desktop/MyApplication/lib/python2.7/site-packages/aeosa:$HOME/Desktop/MyApplication/lib/python2.7/lib-dynload:$HOME/Desktop/MyApplication/lib/python2.7/lib-old:$HOME/Desktop/MyApplication/lib/python2.7/lib-tk:$HOME/Desktop/MyApplication/Extras/lib/python:$HOME/Desktop/MyApplication/lib/python2.7/plat-mac/lib-scriptpackages:$HOME/Desktop/MyApplication/lib/python2.7/plat-mac:$HOME/Desktop/MyApplication/lib/python2.7/plat-darwin:$HOME/Desktop/MyApplication/lib/python2.7:$HOME/Desktop/MyApplication/lib/python27.zip
source bin/activate
python main.py
Three things are important here:
1) In run.command, I first change directory into the virtualenv I created for this project. I'm not trying to be dynamic here, since this is all just a quick hack of a solution, so as part of the README.txt, I simply tell the user to put the folder they downloaded onto their desktop. Otherwise, the cd command wouldn't work.
2) I set the client's PATH variable, separating entries with colons. You can see a list of the paths you'd need using a quick check on your computer...
import sys
for path in sys.path:
print path
3) I then call source bin/activate to activate the virtualenv.
4) I then execute my main.py file, which is in the top level of my virtualenv, along with run.command.
5) I only wrote this bash file for Mac OS X, so I don't know how it would translate to windows PCs. I think the only thing that'd change is that the path literals would need to use backslashes instead of forward slashes, but again, I haven't done this for windows so I'm uncertain.
Read more about setting up a virtualenv here: https://virtualenv.readthedocs.org/en/latest/index.html
Finally, note that you will not be able to send your final, working .zip folder over most email services, since they tend to prevent people from sending executables back and forth, so you'll have to use a service like Dropbox or something to send it to people.

How to turn a UNIX executable into a desktop icon shortcut?

I have a python program that is executable and I can run it on my terminal by typing: ./python_file_name
This is fine for my use, but I want the program to be accessible to non-computer people. I would like to be able to create a desktop shortcut that runs the UNIX executable file. Currently when I double click the executable file it just opens the python program in a text editor. I want to be able to have the end-user double click the file so that it executes in the background.
Just for completeness, the script generates a time-sensitive-data-scraping csv file in the user's downloads folder. Therefore, there is no need to create an application with a GUI or anything fancy like that. Also, I am using Ubuntu and the end-user will be on Mac OS X.
I think you're looking for py2app. It will allow you to send your user a nice little application bundle.

Python to .bat conversion

I would like a user on Windows to be able to run my Python program, so I want to convert it to a .bat file. Is there a way to convert it? I've tried searching, but didn't find anything.
Unless your script is trivial it will not be possible to 'translate' it into a batch file. However two options exist:
Create a batch file to run the python script
Attempt to compile the script into an executable
The first option is trivial. Simply create a batch file as so:
#ECHO OFF
PATH_TO_PYTHON\python.exe PATH_TO_SCRIPT.py
If you are in a corporate environment you could put a python installation on a network and create a batch file to run the script from there. Otherwise you will need the user to install python. If python is on their path then the batch file can be simplified to:
#ECHO OFF
python PATH_TO_SCRIPT.py
Alternatively, there are options available that attempt to compile scripts into .exe files. I've never had any success with them, but py2exe seems the most common.
No, I don't think you can reasonably expect to do this.
Batch files are executed by the Windows command interpreter, which is way way more primitive.
Python is a full-blown programming language with a rich and powerful library of standard modules for all sorts of tasks. All the Windows command interpreter can do is act like a broken shell.
On the other hand, Python is available on Windows, so just tell the user to install it and run your program directly.
you can do it in 2 ways :
create a normal text file with an extension .bat and write
#ECHO OFF
"python.exe location" "your file.py location"
create a normal text file with an extension .bat and write
"python.exe location" "your file.py location"
pause
(sorry for my English Im from armenia)
After 10 years I finaly did that without py2exe and some other things
I used openai playground to do that and thats worked:
I just writed "convert python code into .bat file: [my code]"
Just create a batch file that contains this two lines:
yourfilename.py
pause
Then run the batch file by double-clicking it.

Categories