Run a python script as a quick action in finder on selected folder

Hi, not sure if this forum is the right place to ask, but it’s extremely difficult to search the web for answers about shortcuts.app (must be the name…).

I’m learning python currently and I’m trying to automate recurring tasks on my Mac easily. I have a python script as a first test-case that works (It basically makes a bunch of named folders).

I use Apples Shortcuts.app to execute the python-file with the shell-script action. I start this script in Finder from the Quick Actions menu. The script creates my folders, but not in the selected folder when executing it, but always in my users home folder /Users/markus/.

I suspect I have to somehow tell the Shortcut to take the currently selected folder as a variable or something. I tried to read the help but I don’t find anything useful and don’t understand the options of the Shell-Script Action in Shortcuts.app.

Any ideas how I can get this to work? Or does anyone have a link to a good forum to ask questions about Shortcuts.app and automation?

Thanks! Regards Markus

Post not yet marked as solved Up vote post of M_Ceh Down vote post of M_Ceh
1.1k views

Replies

I’m interested in connecting to discuss ideas sometime @JesseClouse on socials but my recommendation based on what I understand would be to look at Automator (built in) or highly recommended Keyboard Maestro. Take a look at the capabilities more so in the latter and hopefully that can help you get the job accomplished :)

I would suggest utilising AppleScript to do it. In Shortcuts you can add the action "Run Applescript" to change directory before calling python script. See the following example:

on run {input, parameters}
	set mPath to ""
	try
		tell application "Finder" to set mPath to quoted form of POSIX path of ((get target of front window) as alias)
	end try
	set outputString to do shell script "cd " & mPath & "; python3 -c 'import os; print(os.getcwd())'"
	return outputString
end run

You can also execute AppleScript commands directly from Python using osascript:

import subprocess
subprocess.run(['/usr/bin/osascript', '-e', 'try', '-e', 'tell application "Finder" to set mPath to quoted form of POSIX path of ((get target of front window) as alias)', '-e', 'end try'])

I recently had cause to do something like this. Well, I wanted to run a command-line tool rather than a Python script, but those are close enough.

I got this working as follows:

  1. In Shortcuts, I selected Quick Actions on the left.

  2. I clicked the add (+) button.

  3. In the shortcut, I added a Run Shell Script action.

  4. As the input for that, I changed “Receive Images and 18 more input from Quick Actions” to “Receive Files and Folders input from Quick Actions”. That involved clicking on the first item and unchecking everything except Files and Folders.

  5. I set the script text to:

    ~/bin/MyToolName "$@"
    
  6. I set the Shell popup to dash [1].

  7. I set the Pass Input popup to “as arguments”.

  8. I clicked on the name at the top and entered a meaningful name.

  9. In Finder, I control clicked on an item and chose Quick Actions > Customise to enable my shortcut.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] I use dash to avoid zsh-isms in my scripts.

I used Automator in a fashion documented here: https://superuser.com/questions/1160101/run-a-python-script-with-filename-arguments-as-a-macos-folder-action

  1. Open Automator
  2. Create a Quick Action and choose what you want to work on, in your case the drop-downs would be "files or folders" in "Finder".
  3. Choose "Run Shell Script"
  4. Choose /bin/bash or /bin/zsh or whatever shell you are comfortable with. Note: the drop-down also displays (for me) my python shells -- so you could have inline python code. However, I was to have a separate python file I run in multiple ways.
  5. For the script you would have

/path/to/python /path/to/script.py "$1" if you selected a folder.

If you wanted to select a file in the folder and get the base name to the folder, you would use /path/to/python /path/to/script.py "$(basename $1)"

$1 is the argument the Finder sends based on your selection when the Quick Action is run.