Total Newb Here! - Help with Applescript & file names

Discussion in 'Mac Programming' started by vapegirl18, Feb 8, 2014.

  1. vapegirl18 macrumors newbie


    Feb 8, 2014
    Hello Everyone!

    I want to first start off with - I'm new here! :)

    Secondly, I am *very* new to Applescript. What I do know, is that it's sorta like english language code that automates tasks on your computer.

    The only thing I know how to do up to this point, is literally copy & paste code into the applescript window. (Hopefully that will change over time)

    Well, I spent all morning trying to research this on my own - but I am so lost I have no clue how to accomplish what I want to do. So I am hoping to meet someone who can write this code, and hopefully that will be a starting ground for me learning it and even customizing the code even further.

    Here is what I want to do...

    1.) Look within a certain folder (which can change from time to time)
    2.) Look through all files, and make note of the ones that follow this format.

    IE: SSS.vapegirl18.AAA.extension

    I am ONLY interested in running this script on file names that have "vapegirl18" between two periods.

    3.) If the code finds a file that says "vapegirl18" - then keep the VERY beginning of the file name, UP TO the first period.

    So in the example above, the file should be renamed to "SSS.extension" (And when I say extension it could be .jpg, .png, etc.)

    "vapegirl18.AAA" should be removed.

    I hope that made sense. In my mind, I would think that's an easy few lines of code for someone to write - but with my luck, while I think it's simple, - it's probably super involved.

    I hope you can help! :)

    I should be watching this post closely incase someone responds to me with questions - so if you got something to ask, just ask and i'll respond ASAP.

    Thank you in advance for your help!

  2. superscape macrumors 6502a


    Feb 12, 2008
    East Riding of Yorkshire, UK
    Here's a start...


    Well, I guess the big question is whether the files will all be in the top level of your folder or whether they may be in sub-folders?

    If they're all in the top level of your folder then its pretty easy to do in AppleScript. If they may be in sub folders then (I thiiiiink) you'd need some kind of recursion which gets a bit fiddly for a beginner. In that case you might be better off using a shell script with a 'find' command.

    e.g. Something like:

    find /path/to/your/folder/ -type f -name "*.vapegirl18.*"

  3. kryten2 macrumors 6502a

    Mar 17, 2012
    Try this :

    -- If your folder contains sub-folders use the line below
    tell application "Finder" to set myFiles to (every file of entire contents of (choose folder with prompt "Please select directory." default location path to home folder) whose name contains ".vapegirl18.") as alias list -- first line
    -- If your folder does not contain sub-folders then use line below eg uncomment the second line by removing -- and put -- before the first line
    --tell application "Finder" to set myFiles to (every file of (choose folder with prompt "Please select directory." default location path to home folder) whose name contains ".vapegirl18.") as alias list -- second line
    repeat with aFile in myFiles
    	set newFileName to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of (aFile as string) & " | awk -F \".\" '{print $1}'"
    	tell application "System Events" to set name of aFile to newFileName & "." & name extension of aFile
    end repeat
    Note : I would not use the script if your folder contains a few thousand files. If it does you would be better off using a shell script.

Share This Page