Jump to content

Alfred 4.0 - How to get selected images in Finder and pass to shell script


Recommended Posts

I am sure I've been able to do this easily in the past. I want the currently selected HEIC files in the Finder app to be passed as filenames to a shell script to convert to JPG files and remove the HEIC image files.

 

Nothing I have tried works. Screenshot below shows a "File Action" specifying .HEIC file types feeding the shell script with a keyword feeding into the shell script as pictured.

 

Script is simply:

 

/usr/local/bin/magick mogrify -monitor -format jpg "{query}"

 

image.thumb.png.769079a23ca212328e2cef01725a7862.png

Link to comment

When asking for help with a Workflow, please upload it somewhere (transfer.sh is a fast solution without ads; drop your Workflow in the box in the middle) as we can’t properly help you without access to it. Debugging can already be hard with access to the code, and you’re asking us to guess yours from a description. There are multiple places where your code or Workflow setup may be going wrong. Without looking at it we’re shooting in the dark.


Read the Reporting Problems with Workflows topic, as it gives a nice overview on how to build an effective report.

 

Also, it helps if you provide a HEIC file for testing.

Link to comment

Workflow and HEIC image .zip link:

https://alpha.transfer.sh

 

Google Drive link:

https://drive.google.com/drive/folders/12dLkyce2Ll00XnTzAe61kyHy1qbbza2N?usp=sharing

 

 

What I am trying to achieve:

  • Workflow triggered from keyword
  • Take one or many selected HEIC files from current Finder window and pass each sequentially to the shell script that runs a simple imagemagick command
  • Post a notification to the O/S once each image is converted
Edited by ilium007
Link to comment

If you go the Hotkey route, you can be done in under two minutes. Change Run Script to use with input as argv and Script to:

for file in "${@}"; do
  /usr/local/bin/magick mogrify -monitor -format jpg "${file}"
  /usr/bin/osascript -e "display notification \"${file}\" with title \"Converted image\""
done

 

If you really want to go the Keyword route, you’ll have to grab the paths to the selected files with AppleScript, which is can be a pain to manage. This is a start and will get you most of the way there (change the Run Script language to /usr/bin/osascript (JS)):

app = Application.currentApplication()
app.includeStandardAdditions = true

Application("Finder").selection().forEach(item => {
  path = decodeURI(item.url().slice(7))

  app.doShellScript("/usr/local/bin/magick mogrify -monitor -format jpg " + path)
  app.displayNotification(path, { withTitle: "Converted image" })
})

 

That code will work as is for most cases, but will fail if the path contains characters like ?. I’ll leave fixing those as an exercise to you, but I’d stay with the Hotkey route; the alternative will be a hassle.


In both cases you need to delete the Post Notification at the end. If you want one notification after each image is done and don’t want to needlessly complicate the Workflow, your best bet is to call the notifications in code.

Link to comment
  • 1 year later...

Maybe a dumb question @vitor, but why is the following approach bad (or not recommended, as you suggested AppleScript)?

 

Workflow:

image.png.8f398e263e5b8d0ae7be4aefd0525d7e.png

 

File Action:

image.png.d1f8de326fec5300b7dac8afef6bc777.png

For testing I've only added public.heic . Drag other image formats in here as needed

 

Script

/bin/bash with input as argv

for file in "${@}"; do
	output_filename="${file%%.*}.JPG"
	sips -s format jpeg "$file" -o "$output_filename"
	/usr/bin/osascript -e "display notification \"${file}\" with title \"Converted image to JPG\""
done

 

 

This works for me, with single or multiple items.

 

sips is an inbuilt mac tool, Scriptable Image Processing System. Can be used to resize or compress too:

- resize to 1920 pixels, maintaining aspect ratio: -Z 1920

- compress maintaining 65% quality: -s formatOptions 65

 

eg: sips -Z 1920 -s formatOptions 65 -s format jpeg file.HEIC -o file.jpeg

Link to comment
15 minutes ago, cliaz said:

why is the following approach bad (or not recommended, as you suggested AppleScript)?

 

On the contrary, that is the way I recommend! It’s easier to implement correctly.


Note my comment above:

 

On 4/15/2020 at 5:12 AM, vitor said:

If you go the Hotkey route, you can be done in under two minutes.

 

“The Hotkey route” is as simple to implement as the File Action route you demonstrated.

 

The AppleScript route is more convoluted (emphasis added):

 

On 4/15/2020 at 5:12 AM, vitor said:

If you really want to go the Keyword route, (…) AppleScript, which can be a pain to manage.

 

But it is necessary if you want to use a Keyword in this situation and not a Hotkey or File Action. And @ilium007 did really want a Keyword.

 

I gave my opinion on what I consider the most practical way and explained why. But I know people have different preferences, and sometimes you just want to do it the way you want to do it, so I also gave the answer on how to do that. This way the person on the receiving end considers new possibilities but also gets to go with their original plan if they so wish. I find that to be a kind way to help. Even if the original requester only takes value from one of the solutions, someone coming afterwards (you) may benefit from the other.

 

31 minutes ago, cliaz said:

sips is an inbuilt mac tool

 

I like sips and use it when it’s enough for my needs, But @ilium007 used magick in the request. Suggesting another tool amidst the other explanation would have made the reply more complex than (what I judged to be) worth it.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...