How to pass file to terminal command?

I want to automate running the simple command to compress a PNG:
sips -s format jpeg -s formatOptions low [input file] --out [output file]

I’d like to be able to choose a picture and have the script output the file in the same folder, perhaps with an appended “c” for compressed.

Ideally, it could be drag-and-drop as well.

I understand how to execute a terminal command in automator, but how do pass a file to it?

Sorry - newbie question - I just need a push to get started.

Thanks.

For it to work as a droplet, you must create it as an application.

When you create a new application in automator, at the top of the window, it advises that the ‘application receives files and folders as input’.

Add the run shell script action and change the ‘pass input’ dropdown from ‘to stdin’ to ‘as arguments’. You should see the editable text inside the action change from cat to something like this:

for f in "$@"  
do  
	echo "$f"  
done
  • $@’ should be the list of dropped files
  • $f’ should be each file in the list

Change the word ‘echo’ to ‘open’, save the application and drop a couple of images or text files on it. They should open in whatever the default application is for them.

NB When testing an droplet ‘application’ rather than a ‘workflow’, you obviously can’t drop any files so you have to fake the input by adding the ‘get specified finder items’ action at the top. Then when you want to test the dropping function, you can right-click and ‘disable’ the action so it doesn’t interfere with its operation.

It appears that the OP wants an Automator solution and Mockman has answered that question. FWIW, a simple AppleScript solution that does what the OP wants. If saved as an app, it can be run as a regular AppleScript, in which instance it prompts the user for the source files, or the source files can be dragged onto the app.

open {}

on open theFiles
	if theFiles is {} then set theFiles to (choose file with multiple selections allowed)
	repeat with aFile in theFiles
		set pngFile to POSIX path of aFile
		if pngFile does not end with "png" then display alert "A file does not have a PNG extension and will be skipped"
		set jpgFile to text 1 thru -5 of pngFile & " c.jpg"
		do shell script "sips -s format jpeg -s formatOptions low " & quoted form of pngFile & " --out " & quoted form of jpgFile
	end repeat
end open

A shortcut can do what the OP wants, although it requires macOS Monterey or newer. When run, this shortcut prompts the user to select the source files. If the shortcut is added to the Dock, the source files can also be dropped on the shortcut’s Dock icon.

PNG to JPG.shortcut (22.9 KB)

Convert to jpg using sips
https://github.com/b0gdanw/AppleScripts/blob/master/ConvertToJPG.scpt

set question to display dialog "Select" buttons {"Folder", "Files", "Cancel"}
set answer to button returned of question
if answer is equal to "Folder" then
	set theSourceFolder to choose folder with prompt "Please select the folder containing image files"
	set question to display dialog "Please select the quality " buttons {"low", "normal", "high"}
	set theQuality to button returned of question
	tell application "Finder"
		set theFiles to (get every file of theSourceFolder whose name extension is in {"HEIC", "HEIF", "PNG", "BMP", "jpg", "JPEG", "jp2", "TIFF", "PSD", "WebP"})
		repeat with currentFile in theFiles
			set extension hidden of currentFile to true
			set theName to displayed name of currentFile
			set theOutputFolder to quoted form of (POSIX path of ((container of currentFile) as text))
			set theImage to quoted form of POSIX path of (currentFile as string)
			do shell script "sips" & " -s format jpeg -s formatOptions " & theQuality & " " & theImage & " -o " & theOutputFolder & theQuality & "-" & quoted form of theName & ".jpg"
			set extension hidden of currentFile to false
		end repeat
	end tell
	set ConvertedFiles to do shell script "ls " & theOutputFolder & " | grep " & theQuality & " | grep 'jpg' "
end if
if answer is equal to "Files" then
	set theImages to choose file with prompt "Please select the image files:" of type {"public.image"} with multiple selections allowed
	set question to display dialog "Please select the quality " buttons {"low", "normal", "high"}
	set theQuality to button returned of question
	tell application "Finder"
		repeat with currentFile in theImages
			set extension hidden of currentFile to true
			set theName to displayed name of currentFile
			set theOutputFolder to quoted form of (POSIX path of ((container of currentFile) as text))
			set theImage to quoted form of POSIX path of (currentFile as string)
			do shell script "sips" & " -s format jpeg -s formatOptions " & theQuality & " " & theImage & " -o " & theOutputFolder & theQuality & "-" & quoted form of theName & ".jpg"
			set extension hidden of currentFile to false
		end repeat
	end tell
	set ConvertedFiles to do shell script "ls " & theOutputFolder & " | grep " & theQuality & " | grep 'jpg' "
end if
display dialog "The file(s):" & return & return & ConvertedFiles & return & return & "saved in: " & theOutputFolder buttons {"Close"} default button 1