Question

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.

Était-ce utile?

La 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 & "/'"

Autres conseils

I like small challenges like this Sam. They're fun to me... maybe I'm sick ;). Anyway, I wrote you a handler to clean the file name as you requested. It's not really hard to manipulate text in applescript if you're comfortable with text item delimiters and such. These small challenges keep my text skills sharp.

NOTE: in the nameList property the name must end with a period or whatever character is just before the letter D in the number sequence DxxPxx as you mentioned.

So give this a try. Plug in a variety of fileNames and ensure it works how you want. Of course you need to put more values into the nameList and nameReplaceList properties too.

property nameList : {"Yellowst.Nat.Park."}
property nameReplaceList : {"Yellowstone National Park"}

set fileName to "Yellowst.Nat.Park.D12P55.DMS.3248.jpg"
cleanFilename(fileName)


(*================ SUBROUTINES ================*)
on cleanFilename(fileName)
    -- first find the base name and file extension of the file name
    set tids to AppleScript's text item delimiters
    set ext to ""
    if fileName contains "." then
        set AppleScript's text item delimiters to "."
        set textItems to text items of fileName
        set ext to "." & item -1 of textItems
        set baseName to (items 1 thru -2 of textItems) as text
        set text item delimiters to ""
    else
        set baseName to fileName
    end if

    -- next find the pattern D, 2 numbers, P, and 2 numbers in the baseName
    set chars to characters of baseName
    set theSequence to missing value
    repeat with i from 1 to (count of chars) - 6
        set thisChar to item i of chars
        if thisChar is "d" and item (i + 3) of baseName is "p" then
            try
                set firstNum to text (i + 1) thru (i + 2) of baseName
                firstNum as number
                set secondNum to text (i + 4) thru (i + 5) of baseName
                secondNum as number
                set theSequence to text i through (i + 5) of baseName
                exit repeat
            end try
        end if
    end repeat

    -- now make the changes
    if theSequence is not missing value then
        set AppleScript's text item delimiters to theSequence
        set theParts to text items of baseName
        set fixedFirstPart to item 1 of theParts
        repeat with i from 1 to count of nameList
            if item i of nameList is fixedFirstPart then
                set fixedFirstPart to item i of nameReplaceList
                exit repeat
            end if
        end repeat
        set fixedName to fixedFirstPart & " [" & firstNum & "x" & secondNum & "]" & ext
    else
        set fixedName to fileName
    end if

    set AppleScript's text item delimiters to tids
    return fixedName
end cleanFilename

Now if you want to automate this for a folder full of files you can use this code. Just replace lines 3 and 4 of the above script with this. I didn't check this code but it's simple enough it should work as-is.

NOTE: you don't need to worry if non-image files are in the folder you choose with this code because they won't (I'm assuming this) have the DxxPxx number sequence and thus this script will not change them in any way.

set theFolder to choose folder
tell application "Finder"
    set theFiles to files of theFolder
    repeat with aFile in theFiles
        set thisName to name of aFile
        set newName to my cleanFilename(thisName)
        if newName is not thisName then
            set name of aFile to newName
        end if
    end repeat
end tell
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top