7 Replies Latest reply: May 19, 2009 3:13 PM by David Blomstrom
David Blomstrom Level 1 Level 1 (5 points)
I'm not sure if I'm posting this in the right forum, as I don't know exactly what AppleScript is, but here goes...

I have a folder full of web pages, some ending with the extension .html, others with .php. I want to change all of them to .php.

Someone told me about a download called Automator...

http://www.apple.com/downloads/macosx/automator/changeextensions.html

So I downloaded and installed it - but I can't figure out how it works. I opened up the program and selected "Change Extensions" on the left. On the right, under "Get Specified Finder Items," I clicked Add, navigated to the folder, then selected every file in the folder.

I then click Add again to close the window, then click Run and Continue. The program makes a noise (presumably indicating it's processing my request), but no file extensions are changed. In fact, I must be missing a step, because I have to tell it what to change the file extensions to.

I also saved the program as a Finder Plug-In. When I right-click on a file in Finder, I now see the Change File Extension option, but nothing happens when I run it.

Can someone tell me how I tell the program that I want to change all the file extensions to .php?

Thanks.

MacBook Pro
  • Bernard Harte Level 4 Level 4 (3,180 points)
    Cut and paste the following script into a Script Editor document. (You can find Script Editor in the ApplieScript folder of your Applications folder.)

    Open a finder window containing your files, and click the Run button in Script Editor. You'll be ask if you want to change file or folder names or both (I suggest you choose file only) and then to enter the strings you want to change. enter .html and click OK, then enter .php and click OK. That should do what you want.

    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px; height: 340px;
    color: #000000;
    background-color: #FFDDFF;
    overflow: auto;"
    title="this text can be pasted into the Script Editor">
    (*
    Replace Text In Item Names

    This script is designed to replace text in the names of specific files and/or folders in the front window of the desktop. 
    If no folder windows are open, the script will affect files and/or folders on the desktop.

    Copyright © 2001–2007 Apple Inc.

    You may incorporate this Apple sample code into your program(s) without
    restriction.  This Apple sample code has been provided "AS IS" and the
    responsibility for its operation is yours.  You are not permitted to
    redistribute this Apple sample code as "Apple sample code" after having
    made changes.  If you're going to redistribute the code, we require
    that you make it clear that the code was descended from Apple sample
    code, but that you've made changes.
    *)

    --set the source_folder to choose folder with prompt "Folder containing items to edit:"

    -- get the path to the folder of the front window
    -- if no windows are open, the desktop folder will be used
    try
    tell application "Finder" to set the source_folder to (folder of the front window) as alias
    on error -- no open folder windows
    set the source_folder to path to desktop folder as alias
    end try
    display dialog "Search and replace in:" buttons {"File Names", "Folder Names", "Both"} default button 3
    set the search_parameter to the button returned of the result

    repeat
    display dialog "Enter text to find in the item names:" default answer "" buttons {"Cancel", "OK"} default button 2
    set the search_string to the text returned of the result
    if the search_string is not "" then exit repeat
    end repeat

    repeat
    display dialog "Enter replacement text:" default answer "" buttons {"Cancel", "OK"} default button 2
    set the replacement_string to the text returned of the result
    if the replacement_string contains ":" then
    beep
    display dialog "A file or folder name cannot contain a colon (:)." buttons {"Cancel", "OK"} default button 2
    else if the replacement_string contains "/" then
    beep
    display dialog "A file or folder name cannot contain a forward slash (/)." buttons {"Cancel", "OK"} default button 2
    else
    exit repeat
    end if
    end repeat

    display dialog "Replace “" & the search_string & "” with “" & the replacement_string & "” in every item name?" buttons {"Cancel", "OK"} default button 2

    set the item_list to list folder source_folder without invisibles
    set source_folder to source_folder as string
    repeat with i from 1 to number of items in the item_list
    set this_item to item i of the item_list
    set this_item to (source_folder & this_item) as alias
    set this_info to info for this_item
    set the current_name to the name of this_info
    set change_flag to false
    if the current_name contains the search_string then
    if the search_parameter is "Folder Names" and ¬
    folder of this_info is true then
    set the change_flag to true
    else if the search_parameter is "File Names" and ¬
    folder of this_info is false then
    set the change_flag to true
    else if the search_parameter is "Both" then
    set the change_flag to true
    end if
    if the change_flag is true then
    -- replace target string using delimiters
    set AppleScript's text item delimiters to the search_string
    set the text_item_list to every text item of the current_name
    set AppleScript's text item delimiters to the replacement_string
    set the new_item_name to the text_item_list as string
    set AppleScript's text item delimiters to ""
    my set_item_name(this_item, new_item_name)
    end if
    end if
    end repeat

    beep 2

    on set_item_name(this_item, new_item_name)
    tell application "Finder"
    --activate
    set the parent_container_path to (the container of this_item) as text
    if not (exists item (the parent_container_path & new_item_name)) then
    try
    set the name of this_item to new_item_name
    on error the error_message number the error_number
    if the error_number is -59 then
    set the error_message to "This name contains improper characters, such as a colon (:)."
    else --the suggested name is too long
    set the error_message to error_message -- "The name is more than 31 characters long."
    end if
    --beep
    tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
    copy the result as list to {new_item_name, button_pressed}
    if the button_pressed is "Skip" then return 0
    my set_item_name(this_item, new_item_name)
    end try
    else --the name already exists
    --beep
    tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
    copy the result as list to {new_item_name, button_pressed}
    if the button_pressed is "Skip" then return 0
    my set_item_name(this_item, new_item_name)
    end if
    end tell
    end set_item_name</pre>
  • red_menace Level 6 Level 6 (14,840 points)
    AppleScript is Apple's system scripting language. Automator is a different application that uses pre-written routines, or "actions", that can be strung together to make a workflow. There are separate Automator forums for both OS 10.4 Tiger and OS 10.5 Leopard (you might consider updating your profile to indicate what OS you are running).

    There is an option for each Automator action that will show it when it is run - the intent is so that you can "fill in the blanks" as you go. It sounds like you have that option selected, but you may also be using the wrong extension - note that a file extension does not include the period (that is just a separator), so to find an extension of "html", you would need to use html and not .html.

    An example workflow would look something like the following:
    Automator workflow

    To make it a Finder plugin, you don't need a specific action to get a selection (the selection is automatically passed to the workflow via the plugin), so just remove the *Ask for Finder Items* action.
  • David Blomstrom Level 1 Level 1 (5 points)
    Wow, that worked like a charm. Thanks.
  • Bernard Harte Level 4 Level 4 (3,180 points)
    You're welcome!
  • David Blomstrom Level 1 Level 1 (5 points)
    Thanks for the tip, but either my Automator program isn't working correctly, or I'm doing something wrong. The image you posted shows two windows. On my computer, I don't see the second window - "Change Extensions." I've clicked just about everything that can be clicked, but it doesn't give me a window to type in the extension names.

    But since this is the wrong forum, I'll start a new thread in the Automator (Leopard) forum. Thanks!
  • red_menace Level 6 Level 6 (14,840 points)
    In Automator, drag the desired action(s) listed on the left to the workflow window on the right. See http://automator.us/leopard/index.html for some examples and videos.
  • David Blomstrom Level 1 Level 1 (5 points)
    OK, that worked. I read the instructions with the program and on the Autmator website but didn't see that. Thanks.