Python Mac Execute App With File

Jun 26, 2020  When Python interpreter reads a source file, it will execute all the code found in it. When Python runs the 'source file' as the main program, it sets the special variable (name) to have a value ('main'). When you execute the main function, it will then read the 'if' statement and checks whether name does equal to main. Using the subprocess Module¶. The recommended approach to invoking subprocesses is to use the run function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly. The run function was added in Python 3.5; if you need to retain compatibility with older versions, see the Older high-level API section. Getting and Installing MacPython¶. Mac OS X 10.8 comes with Python 2.7 pre-installed by Apple. If you wish, you are invited to install the most recent version of Python 3 from the Python website (current “universal binary” build of Python, which runs natively on the Mac’s new Intel and legacy PPC CPU’s, is available there. PyInstaller freezes Python applications into a stand-alone executables under Windows, GNU/Linux, Mac OS X, FreeBSD, Solaris and AIX. The user can run and execute the resulting packaged application without installing a Python interpreter or any modules.

If you type


/usr/bin/python /Applications/MyProg/myPythonProg.py


Python Mac Execute App With File

Python Mac Execute App With File Windows 10

at the Terminal command line prompt, does it work?


If it works from a Terminal session, then there must be something in your Terminal environment that does not exist when running from Automator. Look at your python script for any environment variables you expect to use and then run add


Run Shell Script

printenv >/tmp/automator.environment.txt


to your Automator script and after running it, use the Terminal to look at /tmp/automator.environment.txt and see if it has the variables you need.


Do you really have the command between '...' in Automator? or did you just do that for your forum posting? If it is inside '...' the sell will not parse it properly.


Can you describe what your script is suppose to do?

Jul 26, 2012 6:11 AM

Basically, a Mac application has a .app extension, but it’s not really a file — it’s a package. You can view the application’s contents by navigating to it in the Finder, right-clicking it and then choosing “Show Package Contents”.

Python Execute Command

The internal folder structure may vary between apps, but you can be sure that every Mac app will have a Contents folder with a MacOS subfolder in it. Inside the MacOS directory, there’s an extension-less file with the exact same name as the app itself. This file can be anything really, but in its simplest form it’s a shell script. As it turns out, this folder/file structure is all it takes to create a functional app!

Enter appify

After this discovery, Thomas Aylott came up with a clever “appify” script that allows you to easily create Mac apps from shell scripts. The code looks like this:

Installing and using appify is pretty straightforward if you’re used to working with UNIX. (I’m not, so I had to figure this out.) Here’s how to install it:

  1. Save the script to a directory in your PATH and name it appify (no extension). I chose to put it in /usr/local/bin, which requires root privileges.
  2. Fire up Terminal.app and enter sudo chmod +x /usr/local/bin/appify to make appify executable without root privileges.

After that, you can create apps based on any shell script simply by launching Terminal.app and entering something like this:

Obviously, this would create a stand-alone application named Your App Name.app that executes the your-shell-script.sh script.

After that, you can very easily add a custom icon to the app if you want to.

Python Mac Execute App With File

Adding a custom app icon

Python Mac Execute App With File Free

  1. Create an .icns file or a 512×512 PNG image with the icon you want, and copy it to the clipboard (⌘ + C). (Alternatively, copy it from an existing app as described in steps 2 and 3.)
  2. Right-click the .app file of which you want to change the icon and select “Get Info” (or select the file and press ⌘ + I).
  3. Select the app icon in the top left corner by clicking it once. It will get a subtle blue outline if you did it right.
  4. Now hit ⌘ + V (paste) to overwrite the default icon with the new one.

Note that this will work for any file or folder, not just .app files.

Examples

Execute Python Online

Chrome/Chromium bootstrappers

I like to run Chrome/Chromium with some command-line switches or flags enabled. On Windows, you can create a shortcut and set the parameters you want in its properties; on a Mac, you’ll need to launch it from the command line every time. Well, not anymore :)

The & at the end is not a typo; it is there to make sure Chromium is launched in a separate thread. Without the &, Chromium would exit as soon as you quit Terminal.app.

Launch a local web server from a directory

Say you’re working on a project and you want to debug it from a web server. The following shell script will use Python to launch a local web server from a specific directory and open the index page in your default browser of choice. After appifying it, you won’t even need to open the terminal for it anymore.

More?

Python Execute Exe

Needless to say, the possibilities are endless. Just to give another example, you could very easily create an app that minifies all JavaScript and CSS files in a specific folder. Got any nice ideas? Let me know by leaving a comment!