Convert Automator Flow to AppleScript??

Discussion in 'Mac Programming' started by CyXBeAtZ, Sep 5, 2012.

  1. CyXBeAtZ macrumors newbie

    Joined:
    Sep 5, 2012
    #1
    Hello to MacRumors & AppleScript Gurus

    Firstly, I just started Automation this week with Automator but I've ran into a wall.. x.x I'm hoping I can resolve this by means of AppleScripting.

    Below is an image of what I'm trying to do. It works great, unless there's no files matching. At this point, it denies going any further because theres nothing to 'Move'.

    What I actually want to do is (in steps):

    1. Get specified finder items >
    2. Get folder contents >
    3. Filter Finder Items by extension w/no folders selected >
    4. Move Finder Items to specified folder >
    5. If there is no Finder Items found to move skip >
    6. Repeat from step 1 to filter other extensions into appropriate folders unless there are no more steps or files then stop.

    Now I'm thinking if I could replace Step 4 (Move Finder Items) with an Applescript that would do this. Then insert Step 5. Eliminating the break in chain, so it can continue on to the next extension and do the same Steps 1-5.
    Then for the last move of the last group, an end if there are no files to move and no next step.

    If this has to be done all in Applescript from Step 1-6 I'm SOL (hoping not since I'm posting here).



    [​IMG]

    Thank you all very much for your attention and help. I apologize if this has been discussed before but I've been researching for a couple of days now and born unto Applescript as of yesterday I see it as a logical process but I have no idea where to begin or how to write Applescript. I'm a new Mac user since Feb, Automator since about 3 days ago and complete noob to Applescript. Any help is appreciated.
     
  2. CyXBeAtZ thread starter macrumors newbie

    Joined:
    Sep 5, 2012
    #2
    I posted this in discussions.apple.com and someone replied with this:

    ------------------------
    The entire Automator workflow, as presented above, could be written in one line of AppleScript:

    tell application "Finder"
    move (every file of folder "Split Filetypes" of desktop whose name extension is "wav") to folder "Path:to:your:WAVs"
    end tell

    Repeat the move line for each appropriate file type/destination.
    ------------------------

    and my reply was:

    So question on the move line, what if there are no files of that filetype left in the folder and there are only, let's say mp3s. What would I put for it to ignore a null return and go on to the next action? Then at the very end to safely stop if there are no files found out of 4 different file extension move lines. This is really where I'm stuck. What you posted seems to be on point as to what I've set up in Automator.

    Can I just put in a move function to move those filetypes, written in applescript, that goes on to the next step in automator if no files are found? Replacing the move finder items Automator action in essence so it keeps going or is it going to be easier and faster through Applescript to do everything.

    I just like the option to make the Automation an app or folder action. This is why Im aiming towards an Applescript/Automator hybrid on this one.

    ... If anyone can help or add to this please do and thanks in advance!!
     
  3. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #3
    You should post your entire workflow as now I'm just guessing what actions you use below the Move Finder Items. There are plenty of Applescripts to be found that move files based on extensions. You can also do something like :

    1. Get Specified Finder Items
    2. Get Folder Contents
    3. Set Value of Variable(ex folderContents)
    4. Filter Finder Items
    5. Move Finder Items
    6. Get Value of Variable( ex folderContents and ignore input from the previous action)
    7. Filter Finder Items
    8. Move Finder Items
    9. Get Value of Variable( ex folderContents and ignore input from the previous action)
    10. Filter Finder Items
    11. Move Finder Items
    12. etc
     
  4. CyXBeAtZ, Sep 5, 2012
    Last edited: Sep 5, 2012

    CyXBeAtZ thread starter macrumors newbie

    Joined:
    Sep 5, 2012
    #4
    I think this may be what I'm looking for but I've no idea how to write it.

    The entire workflow is exactly the same just repeated 5 times for 5 different filetypes. wav, m4a, mp3, mid, (aif, aiff, aifc)

    At the move portion it halts since no files are found but i want it to continue onto the following set in this case. So putting an applescript or get variable after the move action may not suffice since it halts before the next action.
     
  5. CyXBeAtZ thread starter macrumors newbie

    Joined:
    Sep 5, 2012
    #5
    Solution

    This was the last reply and answer to my question. I'm posting it here in case anyone else needs this same information.

    Reviewing what you mentioned, seems extremely simple. Even moreso than Automator in a way

    In my opinion, AppleScript is easier - it's certainly easier to string together multiple discrete tasks, plus it's easier to read that one line and understand it's purpose than it is reading each step in an Automator workflow and knowing what it's trying to do.

    what if there are no files of that filetype left in the folder and there are only

    That's a fair question, right now, as it stands, it would throw an error ("can't get every file of folder.."). There are multiple ways of dealing with that. One is to add a specific test to check that files exist, another is to assume the best and just catch errors.

    To specifically check that some files were found the script needs to be expanded some:

    tell application "Finder"
    set files2Move to (every file of folder "Split Filetypes" of desktop whose name extension is "wav")
    if count files2Move > 0 then
    move files2Move to folder "Path:to:your:WAVs"
    end if
    end tell

    Here the first statement gets a list of matching files and puts them in a variable I've called files2Move.
    I then check to make sure that files2Move actually contains some data (i.e. there aren't 0 files). If there are any files, I move them. If there are no files the move statement never executes.

    The alternative approach is to use a 'try' statement. This tells AppleScript to fail gracefully, rather than reporting an error, so in this case I can just try to move the files and ignore any failures:

    tell application "Finder"
    try
    move (every file of folder "Split Filetypes" of desktop whose name extension is "wav") to folder "Path:to:your:WAVs"
    end try
    end tell

    Here I've wrapped the move command in a try/end try block. If an error occurs the script moves silently to the 'end try' statement without reporting an error to the user. Note that there are multiple things that cold constitute an error - the source or destination directories might be invalid or missing, there might be zero files that match the criteria, the destination directory might be read-only, etc. This script doesn't differentiate between any of those cases, it'll just try its best and move on.

    Can I just put in a move function to move those filetypes, written in applescript, that goes on to the next step in automator if no files are found?

    You can do that - Automator is built on top of the same underlying engine as AppleScript is. Indeed, there is a Run AppleScript action in Automator, so anything you can do in AppleScript you can add as a step in Automator. My problem with this though, is in passing data between the workflow and the AppleScript action. For me, I find it easier to write in AppleScript, so as soon as I find myself thinking in AppleScript I move the whole project there since integration is just so much easier.

    Applicatons and Folder Actions are not Automator-exclusives. Indeed, the original Folder Actions spec was AppleScript entirely - Automator put a slightly prettier front-end on it, but it was originally all AppleScript.
    Granted, building a Folder Action in Automator is easier since it takes care of saving the script in the right place and attaching it to the folder in question, but it's not hard to do in 'pure' AppleScript - you just need to add an appropriate handler so that the OS knows what to do when the folder is triggered.

    Of course, as a folder action you're no longer concerned with checking a specific directory. Since the folder action is attached to a folder (or, really, any folder) you should check the data that's passed in rather than rely on a hard-coded path.

    For example, to turn my script into a Folder Action that triggers on newly-added files, you wrap it like;

    on adding folder items to theFolder after receiving theNewItems
    repeat with each_file in theNewItems
    tell application "System Events"
    if name extension of each_file is "wav" then
    move each_file to folder "Path:to:your:WAVs"
    end if
    end tell
    end repeat
    end adding folder items to

    Now, this is a little different. Since it's a Folder Action it inherently knows the files that have just been added, so there's no need to query the Finder to find the WAV files - you can just look at the list of files that were passed in. You can just duplicate the 'if name extension... end if' statements for each of your file types, and you don't need to worry about there being zero files to move since the 'if' statements will identify the file types.
     
  6. kryten2, Sep 5, 2012
    Last edited: Sep 5, 2012

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    Sorry, I forgot Automator stops the workflow if an action encounters an error.
     
  7. CyXBeAtZ thread starter macrumors newbie

    Joined:
    Sep 5, 2012

Share This Page