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

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 =)

Related

Running a file automatically on mac [duplicate]

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.

Executing a python script through .sh or .desktop file not working

I am setting up a PC running Ubuntu 20.04.3 with a touchscreen and no keyboard/mouse. Due to this, I am putting desktop icons that will run the software needed for data taking/analysis. I was able to set one up with no problem first using a .sh file that executes with a single click, then by using a .desktop file.
The problem I am having is that this will not work with a python3 file that runs tkinter and ROOT. I started off by writing the shebang #!/usr/bin/env python3 at the beginning of the .py file and the .sh file that executes the python file. When I click on the .sh file, nothing happens. If I run the .sh file on the terminal by typing ./Bash_File.sh, it executes with no issues. Also, I did make the .py executable, so I can also run it by going to the directory and typing ./Python_File.py.
I then decided to try with a .desktop file, similar to the first program I got to work. I tried executing both, the .sh file and the .py file, from the .desktop file and I get the same error: ModuleNotFoundError: No Module named 'ROOT'. In the .desktop file, I execute the .py file using Exec=/filepath/Python_File.py. Again, I am able to run this file through the terminal, but not having any luck using the icons to run it. I've looked everywhere online for an answer, but no luck. Thanks for the help
Disclaimer: Generic answer as I know nothing about the ROOT module.
This is a python error, it can't find the ROOT module, mostly like a specific file named ROOT.py. The cause is that a Bash login session is different than a Bash system session.
Run this script from a desktop launcher and in the shell:
#!/usr/bin/env bash
echo $PATH | tr : \\n >my_path-`date +%s`
If the two output files are different, figure out which directory contains 'ROOT.py' and add this to your launcher script:
#!/usr/bin/env python3
import sys
sys.path.append('/the/missing/directory')
If this doesn't set you up right away, there are many articles on managing the Linux system path, the difference between a regular Bash shell and a Bash login shell, and how to manage your Python path. HTH

Python files not executed by command prompt

So I dont know what happened but my python files are not being executed by the command prompt.
C:/Users/MOHAMMED/Local/Programs/Python/Python39>button.py
Above is what im trying to do. but it keeps giving me errors.
This file does not have an app associated with it for performing this action.
I have been using cmd prompt everyday to execute python files but today it is not working. any help is appreciated.
(I use IDLE to do my code btw)
edit:
So this is what i do when im trying to execute files using cmd prompt.
I go into the directory of where all the python files are stored by using cd command.
2)then i just say the name of the file and it works.
but today i installed pip by using setup program and after pip was installed, i cant execute python file using cmd prompt.
You are trying to execute the wrong way it should be:
python button.py
If that doesn't work then :
The complete path of python.exe can be added by:
Right-clicking This PC and going to Properties.
Clicking on the Advanced system settings in the menu on the left.
Clicking on the Environment Variables button o​n the bottom right.
In the System variables section, selecting the Path variable and clicking on Edit. The next screen will show all the directories that are currently a part of the PATH variable.
Clicking on New and entering Python’s install directory.
Then again try :
python button.py

How to make a Python script run without it opening the console window?

I want to double click on my Python script (which uses a Tkinter GUI) and I just want it to open the Tkinter window, not the console window.
To do this, I changed the extension from .py to .pyw and this seems to work fine on Windows but when I double click my .pyw file on a Linux machine, it doesn't work. It simply froze and I had to restart my system.
Please suggest a platform-independent solution that would help me to run the Python script without opening the terminal/command prompt.
it's been a while since i tried on linux, but i believe it should be fairly simple, firstly you need to put a shebang at the top of the script so your shell knows which executable to use:
#!/usr/bin/python
or if you want a specific version you can expand this to:
#!/usr/bin/python3.2
using whichever version you want (only works for first 2 digits)
then you need to mark it as executable:
chmod 711 myfile.py
for more info on this see this page
then when you double click it, on the rpi (last i used linux) it asks if you want to execute it, or execute it in the terminal.
if you choose to execute it without the terminal, you should only see the tkinter GUI
You can use pyinstaller to create the executables for the different platforms you want.
For example,
The syntax of the pyinstaller command is:
pyinstaller [options] script [script ...] | specfile
In the most simple case, set the current directory to the location of your program myscript.py and execute:
pyinstaller myscript.py
PyInstaller analyzes myscript.py and:
Writes myscript.spec in the same folder as the script.
Creates a folder build in the same folder as the script if it does not exist.
Writes some log files and working files in the build folder.
Creates a folder dist in the same folder as the script if it does not exist.
Writes the myscript executable folder in the dist folder.
In the dist folder you find the bundled app you distribute to your users.
Normally you name one script on the command line. If you name more, all are analyzed and included in the output. However, the first script named supplies the name for the spec file and for the executable folder or file. Its code is the first to execute at run-time.
For certain uses you may edit the contents of myscript.spec (described under Using Spec Files). After you do this, you name the spec file to PyInstaller instead of the script:
pyinstaller myscript.spec
You may give a path to the script or spec file, for example
pyinstaller options... ~/myproject/source/myscript.py
or, on Windows,
pyinstaller "C:\Documents and Settings\project\myscript.spec"
pyinstaller

How to run python script on terminal (ubuntu)?

I'm new with python, I've been learning for a few weeks. However now I've just changed my OS and I'm now using ubuntu and I can't run any script on my terminal.
I made sure to have the #!/usr/bin/env python
but when I go to the terminal and type, for example python test.py
the terminal shows an error message like this
python: can't open file 'test.py': [Errno 2] No such file or directory
what do I do?
I must save the file in any specific folder to make it run on terminal?
This error:
python: can't open file 'test.py': [Errno 2] No such file or directory
Means that the file "test.py" doesn't exist. (Or, it does, but it isn't in the current working directory.)
I must save the file in any specific folder to make it run on terminal?
No, it can be where ever you want. However, if you just say, "test.py", you'll need to be in the directory containing test.py.
Your terminal (actually, the shell in the terminal) has a concept of "Current working directory", which is what directory (folder) it is currently "in".
Thus, if you type something like:
python test.py
test.py needs to be in the current working directory. In Linux, you can change the current working directory with cd. You might want a tutorial if you're new. (Note that the first hit on that search for me is this YouTube video. The author in the video is using a Mac, but both Mac and Linux use bash for a shell, so it should apply to you.)
Set the PATH as below:
In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python" and press Enter.
In the bash shell (Linux) − type export PATH="$PATH:/usr/local/bin/python" and press Enter.
In the sh or ksh shell − type PATH="$PATH:/usr/local/bin/python" and press Enter.
Note − /usr/local/bin/python is the path of the Python directory
now run as below:
-bash-4.2$ python test.py
Hello, Python!
Save your python file in a spot where you will be able to find it again. Then navigate to that spot using the command line (cd /home/[profile]/spot/you/saved/file) or go to that location with the file browser. If you use the latter, right click and select "Open In Terminal." When the terminal opens, type "sudo chmod +x Yourfilename." After entering your password, type "python ./Yourfilename" which will open your python file in the command line. Hope this helps!
Running Linux Mint
Sorry, Im a newbie myself and I had this issue:
./hello.py: line 1: syntax error near unexpected token "Hello World"'
./hello.py: line 1:print("Hello World")'
I added the file header for the python 'deal' as #!/usr/bin/python
Then simple executed the program with './hello.py'
First create the file you want, with any editor like vi or gedit. And save with a .py extension. In that the first line should be
#!/usr/bin/env python

Categories