Jump to content
imurban

Newbie question: Simple Alfred "File Action" to Automator workflow?

Recommended Posts

Hello all.

 

Looking for some simple directions, as I'm not skilled at scripting in the least.

 

I've had success creating keyword-initiated workflows that run Automator workflows on selected Finder items, but I am completely stumped as to how I should structure and then run an Automator workflow (or app) as the result of an Alfred "File Action".

 

My successful Automator workflows (triggered by Alfred keywords) function because the targeted Automator workflows all begin with "Get Selected Finder Items".

 

But for this new Alfred workflow (pointing to an image resizing workflow in Automator) I'm trying to bypass Finder and pass multiple images (file buffered through Alfred) to the Automator workflow by means of an Alfred "File Action"

 

In this case, I supposed (maybe mistakenly) that my Automator workflow shouldn't begin with that "Selected Finder Item" call. So, I dropped that out…still the "File Action" doesn't run the Automator workflow on the specified image(s).

 

Could you please help me figure what I must change with my Automator workflow, Alfred Workflow setup or both?

Share this post


Link to post

Hello all.

 

Looking for some simple directions, as I'm not skilled at scripting in the least.

 

I've had success creating keyword-initiated workflows that run Automator workflows on selected Finder items, but I am completely stumped as to how I should structure and then run an Automator workflow (or app) as the result of an Alfred "File Action".

 

My successful Automator workflows (triggered by Alfred keywords) function because the targeted Automator workflows all begin with "Get Selected Finder Items".

 

But for this new Alfred workflow (pointing to an image resizing workflow in Automator) I'm trying to bypass Finder and pass multiple images (file buffered through Alfred) to the Automator workflow by means of an Alfred "File Action"

 

In this case, I supposed (maybe mistakenly) that my Automator workflow shouldn't begin with that "Selected Finder Item" call. So, I dropped that out…still the "File Action" doesn't run the Automator workflow on the specified image(s).

 

Could you please help me figure what I must change with my Automator workflow, Alfred Workflow setup or both?

 

The way the file action works is, it will send a tab delimited string of the file names that are selected to the workflow so, yes, this indeed wouldn't work for an automator workflow that was looking for the selected finder items. I'm not a wizard with automator by any means so I'm not 100% sure how this would be done but I would think that you would either need to break the list of files and call the automator workflow over and over for each file in the list (scripted loop, not manual). You can call automator from a Run Script item using the automator command from the command line.

Share this post


Link to post

 

 

You can call automator from a Run Script item using the automator command from the command line.

 

I appreciate the response. I don't even know what the above quote means (Ha! I'm so in over my head!) I hoped it was just a matter of tweaking my approach, but If it's not immediately obvious to someone with your expertise, then it's probably way too far in the tall grass for me :)

 

Could you tell me if this same limitation of file actioning a batch of images applies with Applescript (vs. Automator)? If it's just a obstacle with Automator, I'm happy to investigate Applescript solutions. I just want to keep it all in Alfred! 

 

I've looked up and down the forums, but I can't seem to find any example of file action to Automator and/or Applescript to resize image files. If I could find something functioning along those lines, I'm sure I could adapt it to my needs.

Share this post


Link to post

I appreciate the response. I don't even know what the above quote means (Ha! I'm so in over my head!) I hoped it was just a matter of tweaking my approach, but If it's not immediately obvious to someone with your expertise, then it's probably way too far in the tall grass for me :)

 

Could you tell me if this same limitation of file actioning a batch of images applies with Applescript (vs. Automator)? If it's just a obstacle with Automator, I'm happy to investigate Applescript solutions. I just want to keep it all in Alfred! 

 

I've looked up and down the forums, but I can't seem to find any example of file action to Automator and/or Applescript to resize image files. If I could find something functioning along those lines, I'm sure I could adapt it to my needs.

 

Well it's just that it is not immediately obvious to me because my knowledge of Automator is extremely basic.

 

I took a stab at this for a sec though.. and here is what I came up with and here's a little better explanation of the info from before.

 

When you select multiple items in Alfred, either from selecting multiple in Finder and pressing Cmd+Opt+\ or by using the File Buffer to action multiple items from Alfred... the list of file paths are passed through Alfred as a tab separated list. So for 3 files in your ~/Documents folder, call them file1.txt, file2.txt, and file3.txt.. when you select the file action in Alfred, it will be passed to the next step of the workflow like this, as a single string.

/Users/imurban/Documents/file1.txt     /Users/imurban/Documents/file2.txt     /Users/imurban/Documents/file3.txt

Now, you could attach a Run Script action to the tail end of the File Action and leave the default script language set to bash and use code similar to the following..

IFS=' ' read -ra FILE <<< "{query}"
for i in "${FILE[@]}"; do
  //commands here
done

 

This would split the string using the tab as a delimiter then loop through and allow you to run a command or set of commands on each file name/path passed in. For your case, you would want to run an automator workflow with that file as the input. So it may would look something like...

IFS=' ' read -ra FILE <<< "{query}"
for i in "${FILE[@]}"; do
  automator -i "$i" /Users/imurban/Documents/yourworkflow.workflow/
done

 

This would run the workflow located here: /Users/imurban/Documents/yourworkflow.workflow and pass each file path into that workflow as input. Now, that works one file at a time. I haven't looked into this extensively yet so I'm not sure how to pass multiple files in at once but to use this input you would need to add a "Get Specified Finder Items" object at the top of your workflow to make it take that input and then work on those files. So for my example, I used the code above (obviously with a different path) and made a sample workflow that had 2 actions in it. The first was, as mentioned, Get Specified Finder Items, and the second was a Copy Finder Items with the destination path being the Desktop. I was able to run this and have it successfully copy each of the selected items to the Desktop.

 

Does that help at all?

Share this post


Link to post

Yes. Thank you. Your explanations and examples help a great deal. Very kind of you to take the time with me.

 

Unfortunately I can only get the workflow to output one (the last) of my Alfred buffered files (I tested three Tiff files). Perhaps it's the number of steps in my Automator workflow that causes a time-out of sorts?

 

Below are screen shots of my File Action, Run Script Item, and Automator workflow. Could you please tell me if these shed any light on it?

 

QJFzL1K.png

 

4hsrRCA.png

 

fMQxOHK.png

Share this post


Link to post

You probably want to get rid of that "Get Specified Finder Items" action.
 
Instead of a Workflow, try creating an Application with Automator. Then, instead of calling automator from your bash script, use:

open -a /path/to/your/jpeg_sRGB_1000.app "$i"

You could also pass all the images at once, but I'm not sure exactly how to unpack the FILE variable properly.
 
FWIW, I think you can do what you're trying to do with the sips command line app.

Edited by deanishe

Share this post


Link to post

FWIW, I think you can do what you're trying to do with the sips command line app.

 

Yep. You can do more with `ImageMagick`'s `convert`, but that isn't natively installed. Off the top of my head, I don't know how to do the color profile, but

sips --resampleWidth 1000 -s format jpg <path/to/IMG_NAME> --out <path/to/IMG_NAME>.jpg

should alter the image to a width of 1000px and convert the image to a jpg.

 

The sips manpage isn't too terribly helpful, but there are quite a few blog posts around that help out more.

Share this post


Link to post

This might work for you bash script (untested):

FILES="{query}"
for file in ${FILES[@]}; do
    if [[ "${file#*\.*}" != "jpg" ]]; then
        name="${file%*\.*}"
        sips --resampleWidth 1000 -s format jpg "${file}" --out "${name}.jpg"
    fi
done

Share this post


Link to post

You probably want to get rid of that "Get Specified Finder Items" action.

 

Instead of a Workflow, try creating an Application with Automator. Then, instead of calling automator from your bash script, use:

open -a /path/to/your/jpeg_sRGB_1000.app "$i"

 

Thanks, deanishe.

 

Unfortunately, I can't get that modified Automator approach to work, even on a single Tiff file passed by File Action. It does manage to copy (via Copy Finder Items in Automator) my entire user.workflow folder to the destination folder!

 

I am now trying to work out that sips suggestion of yours that Shawn Rice picked up on.

Share this post


Link to post

Automator can be great, but if you can get into the shell at all, then automating that way will always be more powerful. I'm pretty sure that Automator was built so that tech guys in companies could distribute a not-frightening way for others in the company to get through rote tasks. Basically, it provides a nice way to use the mouse to automate things. Alfred is basically our interface to do the same, so Alfred replaces most of that.

 

For the color spaces look here: http://superuser.com/questions/116648/can-i-convert-an-image-from-cmyk-to-rgb-in-mac-os-x. It looks like you'll just have to add a "--matchTo" flag to the commands examples that I wrote above.

 

If you're not too familiar with icc profiles, they're mostly used to calibrate your display; however, it's the same technology to calibrate images. Hence, SIPS at first appears to be a strange hybrid between display tuning and image manipulation. But, after looking much, much more deeply into these things, you'll see that the logic developed to tune these images was used to tune displays.

 

But, that means when using SIPS, you have to make sure that you're choosing the image options rather than the display options.

Share this post


Link to post

Thank you all for your advice. In the final estimation, I haven't really got competency to string it all together. I should be able to get by just keywording applescripts from Alfred that run specific Photoshop actions individually on open images. If I really need to batch process, I can just initiate those PS actions on whole sets of images from Bridge.

 

Again, I appreciate all the thoughtful responses. I'm happy to push Alfred to the limits of my capabilities, confident that smarter folks than I are really making it do some dazzling things. I will say, I am now very mindful of SIPS as a future option should my cobbled approach get too cumbersome. Thank you also for that.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...