You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Use Applescript to Launch Python

Let's be 100% clear about what I am not trying to do. I am not trying to run a python script or start a python process in terminal with Applescript. Instead I am running a Python script which launches an easygui message box. This easygui messagebox then launches some weird Python application. I am trying to find out what that application and set it to frontmost. I run the following Python code:


from easygui import *
b = enterbox("what time then?")


which opens an easygui entrybox like so:


User uploaded file


If you look at that fountain pen it says Python. That has to launch in order for that messagebox to pop up. If I could figure out how to tell applescript to find that app, I could then bring the entrybox to the front, but I cannot now. When you look up top at what the name of the app is you will see:User uploaded file


When you click on Python and get the information, you find:


User uploaded file

I cannot find the path that launches that application. I have found the path of the Python Launcher and I can open it with applescript using:


os.system(f'''/usr/bin/osascript -e 'tell application "Python Launcher" to activate' ''')

The Python launcher is located in my apps folder and you would think that there is where Python is located but it is not:

User uploaded file


The other areas that have Python file will launch the Python process in terminal so they're not helpful.

This is the applescript code I'm using that will set Python to the front if it is open


os.system(f'''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')

MacBook Pro, OS X Yosemite (10.10.1)

Posted on Sep 1, 2018 3:04 AM

Reply

Similar questions

4 replies

Sep 1, 2018 9:35 AM in response to kyle76

You have installed Python 3 from Python dot org and its python3 binary is symlinked into /usr/local/bin from its framework in /Library/Frameworks/Python.framework/Versions/3.6/bin/python3. Just forget about Python Launcher.


Your non-standard easygui module transforms your python interface requests to its methods and then asks the Tcl/Tk frameworks in /System/Library/Frameworks to create that gui element. If it does just that, then the owner of the gui is python. If it does something dummer, like create a temporary script, and then asks /usr/bin/tclsh to run it, then python is the parent process, but /usr/bin/tclsh is likely the controlling process.


I do have Python 3 (3.7.0) from Python dot org installed, but have no plans to use easygui. I do however, have several python programs here that directly ask Tcl/Tk to produce some visual gui, which would then be automatically front most when run. But, If I cover that Tcl gui with another window, the following code will tell python (gui owner) to make that gui front most again.

User uploaded file


Whether this will work with your easygui is a matter for testing. If easygui is building a script in the background, and then telling /usr/bin/tclsh, or /usr/bin/wish to run that script, then python may not be the owning process, and frontmost directives may aggravate you.


Ideally, one would expand on that AppleScript to test if Python is running, and if not return false, then test for that return value in Python after the subprocess. Otherwise, the code above, will croak if no Python is running.

Sep 1, 2018 8:40 PM in response to VikingOSX

Hi Viking, Thanks for your help. I tried you code but my problem is that I need to have the Tcl/TK interface open and running before I execute the easygui. I figured out how to get the Tcl/TK interface running with command line: python -m tkinter But even if I were able to get that command line executed directly from python, I still would have a problem in that when I run os.system(f'''/usr/bin/osascript -e 'tell application "Python" to activate' ''') It sets the window on the left to the frontmost not the window on the right.


User uploaded file



I think the most likely solution to my problem is to be able to find someway to launch the TCL/TK interface without launching that left pop-up box or closing that left pop-up box with python.

Sep 2, 2018 12:54 AM in response to kyle76

I was able to find the following workaround. I found this module which will pop-up a countdown timer: https://github.com/furas/python-examples/blob/master/easygui/timerbox/easygui_ti merbox.py I set the countdown timer for 1, then that will leave the tkinter interface on. I can then focus that and run the easygui line which will now be frontmost. Not the most elegant solution but it will serve as a temporary band-aid until I learn how to use tkinter properly.

Sep 2, 2018 1:34 AM in response to kyle76

Actually I spoke too soon. When I run the program from terminal it will set the pop-up to the front but the whole point was to run the program from Keyboard Maestro so that I can run the program with the click of one button. When I run it from Keyboard Maestro or Automator it will not set to the front.

Use Applescript to Launch Python

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.