CyX BeAtZ

Q: Convert Automator Flow to AppleScript??

Hello AppleScript/Automator 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.

 

/___sbsstatic___/migration-images/194/19479509-1.png

 

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).

 

 

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.

MacBook Pro, OS X Mountain Lion (10.8.1), AppleScript & Automator

Posted on Sep 5, 2012 6:36 AM

Close

Q: Convert Automator Flow to AppleScript??

  • All replies
  • Helpful answers

  • by Camelot,Helpful

    Camelot Camelot Sep 5, 2012 9:56 AM in response to CyX BeAtZ
    Level 8 (47,285 points)
    Mac OS X
    Sep 5, 2012 9:56 AM in response to CyX BeAtZ

    If this has to be done all in Applescript from Step 1-6 I'm SOL

     

    What's your aversion to AppleScript? This kind of task is trivial in AppleScript.

     

    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.

     

    As for how to do it in AppleScript, open /Applications/Utilities/AppleScript Editor.app and paste the above line into a new document.

  • by CyX BeAtZ,

    CyX BeAtZ CyX BeAtZ Sep 5, 2012 12:03 PM in response to Camelot
    Level 1 (0 points)
    Sep 5, 2012 12:03 PM in response to Camelot

    No aversion, just a complete newbie to AppleScript. Reviewing what you mentioned, seems extremely simple. Even moreso than Automator in a way.

     

    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.

     

    Thanks a ton for your help and wicked quick response!

     

    Nick

  • by Frank Caggiano,

    Frank Caggiano Frank Caggiano Sep 5, 2012 12:24 PM in response to CyX BeAtZ
    Level 7 (25,796 points)
    Sep 5, 2012 12:24 PM in response to CyX BeAtZ

    What you are looking to do will be easier in Applescript, looping and variables in Automator can be a real pain. Applescripts can be made into apps and can also be made into folder actions you don't need to use Automator for that.

     

    The simplest way to accomplish what you are looking to do would be to add additional move instructions to Finder with the filetype and folder hard coded in. As long as you're only looking at a few filetypes this won;t be to bad.

     

    So building on Camalot's script

     

    tell application "Finder"

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

     

         move (every file of folder "Split Filetypes" of desktop whose name extension is"jpg") to folder "Path:to:your:JPGs"

     

         etc..

    end tell

     

     

    There are many good sites that deal and teach Applescript as well as many good books. If you plan on getting into scripting I'd suggest you check some of them out.

     

    regards

  • by Camelot,Solvedanswer

    Camelot Camelot Sep 5, 2012 12:31 PM in response to CyX BeAtZ
    Level 8 (47,285 points)
    Mac OS X
    Sep 5, 2012 12:31 PM in response to CyX BeAtZ

    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.

  • by CyX BeAtZ,

    CyX BeAtZ CyX BeAtZ Sep 5, 2012 12:38 PM in response to Frank Caggiano
    Level 1 (0 points)
    Sep 5, 2012 12:38 PM in response to Frank Caggiano

    Great! I posted too soon.. Camelot I do appreciate your help tons and again thanks for taking time of your day to help me out on this. I will use this new knowledge and build on it. Since this coding seems very logical, I think I will take advantage of learning some Applescript to simply life

     

    Thanks for all who posted and helped me understand exactly what I was looking for.

    Also thanks for the tip Frank.

     

    Bless

    Nick

  • by Frank Caggiano,Helpful

    Frank Caggiano Frank Caggiano Sep 5, 2012 1:09 PM in response to CyX BeAtZ
    Level 7 (25,796 points)
    Sep 5, 2012 1:09 PM in response to CyX BeAtZ

    Macscripter is definitely worth a look, lots of good information.

     

    Book wise Learn Applescript from Apress is good both as a reference and as a learning tool and of course there is O'Reilly's AppleScript The Definitive Guide

  • by CyX BeAtZ,

    CyX BeAtZ CyX BeAtZ Sep 5, 2012 1:20 PM in response to Frank Caggiano
    Level 1 (0 points)
    Sep 5, 2012 1:20 PM in response to Frank Caggiano

    Awesome stuff! You rock Frank!