[thelist] Applescript to change file extensions

James Spahr james at designframe.com
Fri Sep 29 13:59:35 CDT 2000


on 9/29/00 2:03 PM, Charlie Llewellin at sallyevans at eden.com wrote:

> Any Applescript person have such a script - to run through a folder and
> change file extensions - ie, from .html to .php?
> 
> If you did, I would be very grateful!
> 
> Charlie Llewellin
> 

this should work, save as an application and just drop a folder on it.
(change the properties to accommodate something other than html -> php
conversions)

--- start applescript ----
property oldFileExt : ".html"
property newFileExt : ".php"

on ProcessAFile(theFile)
    set theFileInfo to info for theFile
    if folder of theFileInfo then
        ScanAFolder(theFile)
    else
        
        if ((theFile as string) ends with oldFileExt) then
            
            tell application "Finder"
                set n to name of theFile
                set ix to offset of oldFileExt in n
                set n to characters 1 thru i(x-1) of n as string
                set name of theFile to (n & newFileExt)
            end tell
            
        end if
        
    end if
end ProcessAFile

on ScanAFolder(theFolder)
    
    --
    --    This subroutine scans through a folder.  The ProcessAFile hander
    --    is called for each file
    --    found in the folder.  Folders are handled by calling ScanFolder to
    --    recursivly work through the folder tree
    --
    
    set fileNames to list folder theFolder
    repeat with aFile in fileNames
        set theFile to (theFolder as string) & contents of aFile
        ProcessAFile(alias theFile)
    end repeat
end ScanAFolder

on open of filesList
    
    --
    --    Drag and drop handler.  Pass each of the files or folders to
    --    ScanAFile which does the right
    --    thing.
    --
    
    repeat with aFile in filesList
        ProcessAFile(contents of aFile)
    end repeat
end open

on run
    
    --
    --    Run handler.  Prompt the user for a folder to process
    --
    
    set theFolder to choose folder with prompt "Select folder to scan:"
    ScanAFolder(theFolder)
end run

--- end applescript ---

James
http://spahr.org





More information about the thelist mailing list