AppleScript to count files and email results

Discussion in 'Mac Programming' started by vloryan, Aug 4, 2014.

  1. vloryan macrumors member

    Joined:
    Jan 11, 2014
    #1
    Hi guys,

    i have no idea how to do this. i would like to have an AUTOMATOR action check a folder for an amount of files containing a similar term and then email these results to recipient A, B and C. for example...

    3 files containing "term a"
    4 files containing "term b"
    1 file containing "term c"

    all those filename look like "xyz 20140804 150235.pdf". everything beginning with 2014... up to the end can be ignored. "xyz" in this case would be a term to look for.

    huuuuuuuuuuuh :( no idea. i hope someone can help me out there!

    thanks!
     
  2. numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #2
    See if this gives you a starting point.

    Code:
    on countFiles(prefix)
    	tell application "Finder"
    		set f to folder "Test Files" of folder "Desktop" of home
    		set fileList to every item of f whose name starts with prefix
    		--log fileList
    		log {prefix, count of fileList}
    	end tell
    end countFiles
    
    countFiles("a")
    countFiles("b")
    countFiles("c")
    countFiles("d")
     
  3. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #3
    hi, i tried this as an Automator workflow using the script on a folder. Testing shows green check icons only :) Good sign!

    But i have to mention that there a filenames that look like this "a b 2014...". Would those be cut off after the "a" (in my example, shown here)?.

    And now i'd like to transfer those results in an email to recipient a,b and c.

    Thank you so much!
     
  4. numero, Aug 4, 2014
    Last edited: Aug 4, 2014

    numero macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #4
    In the case of a file called "a b 1234" then the "starts with a" script example would choose this file, but a "starts with b" would not. Be careful if you have prefixes that are roots of other prefixes. If you have a file "abc 123" then a "starts with a", "starts with ab" and "starts with abc" would all choose the same file. If you are looking to only test against the letters up to the first space then we can work that condition into the script.

    Do you want the email to report the number of files or do you want to send the actual files? In the case of the latter you will have to be careful about not breaking any email server message size limits.

    The two questions to answer:
    1) Do you want the test on the file to be against the first letters or against the first word of the file name?

    2) Are you just sending a report of the number of files or the actual files in the email message?

    One more question.
    3) Why Automator and not just an AppleScript?
     
  5. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014
    #5
    1 / It would be ok if the second word in the filename will be ignored. Don't wanna waste your time here :) If possible we could ignore everything from the date in the filename on as filenames are all build like "xyz 20140804 125946".

    2 / I am just sending the text message. No files.

    3 / Because i have no idea how to do this :) Automator is scripting for dummies like me :)

    THANKS!
     
  6. vloryan thread starter macrumors member

    Joined:
    Jan 11, 2014

Share This Page