Search code examples
applescriptfile-renamebatch-rename

Applescript repetitive renaming


I'm trying to make an AppleScript droplet to rename a bunch of images annoyingly formatted, but I found out my AppleScript skills have become nonexistent and I'm getting nowhere. So if possible, full code, not just snippets.

The file setup is always the same, but there are many variations (ex: Yellowst.Nat.Park.D12P55.DMS.3248.jpg)

  • It starts with a place name, should be a find and replace for a bunch of different strings, ("Yellowst.Nat.Park" -> "Yellowstone National Park")
  • Then it is followed by two numbers that should be changed in format (D12P55 -> [12x55]). They're always set up in a "D" followed by two numbers, a "P" and again two numbers.
  • And it ends with a random string, can be numbers, letters etc, which all have to go. They differ in format and length, no pattern in them.

Basically I want to go from "Yellowst.Nat.Park.D12P55.DMS.3248.jpg" to "Yellowstone National Park [02x03] .jpg" I want to add text afterwards so want to end with a space.

The best way to do this seems to me a repetitive find and replace for the first part, Make a list for a bunch of terms wich have to be replaced by a bunch of respective terms. Followed by a detection of the number format and ending with deleting of the random string after it.


Solution

  • Here is another approach.

    property pictureFolder : (alias "Mac OS X:Users:Sam:Pictures:test:")
    property findList : {"Yellowst.Nat.Park", "Jellyst.Nat.Park"}
    property replaceList : {"Yellowstone National Park", "Jellystone \\& National Park"}
    
    tell application "System Events"
    set nameList to (name of every file of pictureFolder whose visible = true)
    repeat with i from 1 to count of (list folder pictureFolder without invisibles)
        set fileName to item i of nameList
        set fileExtension to (name extension of (file fileName of pictureFolder))
    
        repeat with j from 1 to count of findList
            if fileName contains item j of findList then
                set tempName to do shell script "echo " & fileName & " | sed 's/.D\\([0-9][0-9]\\)P\\([0-9][0-9]\\).*/[\\1x\\2] " & i & "." & fileExtension & "/'"
                set tempName to do shell script "echo " & tempName & " | sed 's/^" & item j of findList & "/" & item j of replaceList & " /'"
                set name of (file fileName of pictureFolder) to tempName
                exit repeat
            else if j = (count of findList) then
                set tempName to do shell script "echo " & fileName & " | sed 's/[.]/ /g'"
                set tempName to do shell script "echo " & tempName & " | sed 's/.D\\([0-9][0-9]\\)P\\([0-9][0-9]\\).*/ [\\1x\\2] " & i & "." & fileExtension & "/'"
                set name of (file fileName of pictureFolder) to tempName
            end if
        end repeat
    end repeat
    end tell
    

    To avoid duplicate names, I added a counter to the end of the file name. If there are no duplicates, you can use this instead:

    set tempName to do shell script "echo " & fileName & " | sed 's/.D\\([0-9][0-9]\\)P\\([0-9][0-9]\\).*/[\\1x\\2] " & "." & fileExtension & "/'"