AppleScript to skip Automator workflow step - when no files are provided

My current Automator workflow:

Get Specified Finder Items (path to folder auto-populating with images from a camera)

Get Folder Contents

Sort Finder Items - by Creation Date in descending order (gets oldest item first)

Limit Number of Items - Limit to First 1 items

Move Finder Items (If no items are passed to this step skip it)

Loop - Automatically; Stop after 20 minutes; Use the original input


What I am trying to accomplish:

I'm trying to write an AppleScript to skip the "Move Finder Items action" when it is not supplied with any files (from the previous workflow step), moving on to the next step which is a loop to begin my workflow again.


Big picture:

I have a workflow, where I am moving files from one folder to another folder (one at a time). When the input folder is empty, Automator comes back with "No item references were passed to the Move Finder Items action (-50)"; which it should - because it is not passed any files. I would like to ignore this error (or skip this step) and continue with the workflow if no files are provided to the "Move Finder Items action". I currently have an AppleScript (see below) that stops the workflow when this error occurs, but I would like to have this flow continue, not stop.


AppleScript:

on run {input, parameters}

if input is {} then error number -128 -- 'user cancelled'

return input

end run


Please help me create a script to skip this step, or to restart the Automator workflow when no files are passed to the "Move Finder Items action".

OS X Mountain Lion (10.8.3)

Posted on Jul 25, 2013 10:00 AM

Reply
13 replies

Jul 25, 2013 7:42 PM in response to leorw

I'm not aware of a way to do this with a workflow saved as an application, but Automator is scriptable, so you can manipulate a workflow document. For an example, build the follwing workflow:


  1. Ask for Text (just to get some input)
  2. Run AppleScript:
    on run {input, parameters}         set currentAction to index of current action of front workflow -- the most recent completed action   if input is {} then -- if no input then disable the following action     tell Automator action index (currentAction + 2) of front workflow to set enabled to false   else -- enable it     tell Automator action index (currentAction + 2) of front workflow to set enabled to true   end if         return input end run
  3. Ask for Confirmation (just to show a dialog when enabled - this action will be enabled/disabled based on the text input)
  4. Loop


You can also use action names, but I think it is less confusing to use indexes, since names can be edited and an action can be used more than once.

Jul 27, 2013 11:40 AM in response to red_menace

Thank you so much for your help red_menace! The script you provided is working perfectly!!



It works no matter if there is an image in the folder or not, and I can continually feed the folder with images as they come in from the camera, and the script will move it to the designated folder.



Thanks again for your scripting knowledge.

Feb 11, 2014 3:06 PM in response to red_menace

Great stuff! This works perfectly when running it inside Automator, but when saving the action as a Service (or inside Hazel) it fails on the AppleScript part.


Probably something with the way one refers to the workflow - like the “of front workflow” part. I wonder if it's possible to refer to the current workflow in another way - maybe by service location?


Anyone knows how one can fix this?

Sep 13, 2015 5:26 PM in response to leorw

Hi Leorw, Red_Menace, does this still work for you after recent OS X upgrades?


I was able to get Red_Menace's Test Workflow running smoothly (Ask for text, Applescript, Confirmation, Loop) but cannot get this working in my main script. Does anyone know if Automator / Applescripts has changed since this was written?


I'm trying to auto-sort any time a file is added to the Downloads folder (and move it into one of six sub-folders). I've seen many ways of doing it but none seem to work after the Filter process turns up with an empty input. This thread has seemed the most promising. I feel I'm close but just can't get there...


Can anyone give some advice on how they'd structure this workflow (is it any different to Leroy's? Would you use a variable?)?


ps I've been at this about 6 hours and have seen so many inconsistencies I actually wonder if Automator isn't just buggy. Is there another program anyone would use? Is it possible to do this all in Applescript?

Sep 14, 2015 6:07 PM in response to alexwookiee

The test workflow still works for me, so without seeing exactly what you are doing my guess would be that there is an issue with the name or index of the action you are trying to disable.


Automator is definitely goofy, but it isn't really designed for conditional execution of actions. AppleScript is more flexible and would be capable of performing your workflow, since it has access to the same resources that an Automator action uses.

Mar 23, 2016 11:51 PM in response to red_menace

Hi menace,

I'vet got somewhat of the same problem as leorw above and have been trying for hours to solve it but to no avail. This seems to be the most promising solution I've found so far but I cannot seem to get your script to run properly.


When I copy it into the AppleScript editor, I get the Syntax Error "Expected end of line, etc. but found identifier" and then the word "action" in your code is highlighted.


on run {input, parameters}

set currentAction to index of current action of front workflow -- the most recent completed action


Any idea why?


I am trying to sort a folder of phone calls received by their area code into separate folders for each area code.


For example, I will have a folder of 100 phone calls, sent to me as .eml files. They all come in this format "Onebox Voicemail (Callback- xxx-555-5555)" with the xxx being different area codes. My work flow works fine for the current batch of calls, but as soon as there are no results for one of the area codes, the workflow stops working. I just want to add a condition for the workflow to skip the Move Finder Items if there are no files to move.


Basically my workflow consists of


1. Get Specified Finder Items

2. Get Folder Contents

3. Filter Finder Items

4. Move Finder Items

5. Repeat


Any light you could shed on this would be greatly appreciated!

Thanks

Mar 24, 2016 6:09 PM in response to travkroeg

The script is designed to run in a Run AppleScript action. It won't work correctly if you are doing something like putting it into the regular Script Editor, since the script is meant to work within the context of an Automator workflow (the command is from the Automator scripting dictionary, and outside of Automator would need to be within a tell application Automator statement).

Mar 25, 2016 12:49 PM in response to red_menace

Ahhh gotcha, makes sense. Im a little new to all this as you can tell haha.


Thanks a lot, it seems to be working now. However I have run into another issue, and I'm wondering if you'd be able to help me here as well.

All of these email messages come with voicemail attachments, and once the .eml files are sorted into their respective folders, I would like the attachments to then be downloaded and saved in the same place

I figured it would be as easy as making a new workflow as a folder action and then using the "Get Attachments from Mail Messages" command, but that only seems to work on messages that are still in my mail inbox. If I do the command on the folder containing the .eml files, it says "The action "Get Attachments from Mail Messages" was not supplied with the required data.

Any insight? Thanks again.

Mar 27, 2016 10:34 AM in response to red_menace

Hmm ok, gonna keep working on these AppleScript tutorials then. Thanks for all your help so far.

One more question. Do you know of a script that will automatically download attachments from an .eml file? This is the part that I seem to be hung up on the most.

I'd like to be able to run a script on a folder, have it go through all the .eml files and create a new folder, with the same name as the file for each one. Then move each .eml file into its respective folder and then download the attachment there as well.


Would this be a complex script? If it's not too much to ask, do you think you could attempt to mock up a script, or point me in the right direction as to how to do this?


Thanks again

Mar 28, 2016 6:23 PM in response to red_menace

Alright so I've decided to just try and sort them into separate mailboxes as the emails come in with a rule. (I'm trying to figure this out for my father and he wanted it done the other way, I feel this way will be easier.)


I've managed to get them to sort into their own mailboxes nicely, but the second part of the rule to download attachments doesn't seem to want to work properly. I found a script to start with online and modified it but it doesn't want to download attachments. No error messages or anything, just nothing happens. Could you take a look at my script, and let me know if you see anything amiss? I apologize for any crappy formatting, couldn't get it to paste nicely from the script editor.


Thanks.


using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule

-- The folder to save the attachments in (must already exist)


-- Save in a sub-folder based on the name of the rule in Mail


set subFolder to subject of eachMessage

tell application "Finder"

set attachmentsFolder to ((path to desktop as text) & "Radio Stations:" & "Area Code 352 Gainsville HANK 101.7") as text

if not (existsfoldersubFolder of folderattachmentsFolder) then

makenewfolderatattachmentsFolderwith properties {name:subFolder}

end if

end tell

tell application "Mail"


(*repeat with eachMessage in theMessages


set {year:y, month:m, day:d, hours:h, minutes:min} to eachMessage's date sent

set timeStamp to ("" & y & "-" & my pad(m as integer) & "-" & my pad(d) & "-" & my pad(h) & "-" & my pad(min))*)


try

-- Save the attachment

repeat with theAttachment in eachMessage'smail attachments


set originalName to name of theAttachment

set savePath to attachmentsFolder & ":" & subFolder & ":" & originalName

try

savetheAttachmentinfile (savePath)

end try

end repeat

display dialogsubFolder

end try

(*end repeat*)


end tell

end perform mail action with messages

end using terms from

u

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript to skip Automator workflow step - when no files are provided

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.