AppleScript Q & A

  • Avatar

    Posted: 04 February 2002 01:17 PM

    Hi Folks!  I recieved this e-mail from an observer and decided to post it here.  I’d like to create an AS Q & A thread in the Coder’s Corner to help people out with their scripting dreams.  You can either post your question here or send me an email at .(JavaScript must be enabled to view this email address).

    Question:
    I’m using Mac OS X, v 10.1.2.  What I want to do is rename the files in a folder. Renaming them anything would
    do, but let’s say I want to prefix “temp ” to the name, so “file1name” becomes “temp file1name” and “file2name” becomes “temp file2name” etc. Here’s what I’ve tried:

    on run
        set myFolder to choose folder
        addprefix
    (item 1 of myFolder as alias)
    end run

    on addprefix
    (myFolder)
        
    tell application "Finder"
            
    repeat with x in every file of myFolder
                set name of x to 
    ("temp " name of x)
            
    end repeat
        end tell
    end addprefix 

    Why doesn’t it work?
    -Will

    Answer:
    Great question Will! Here’s my solution:

    > on run
    >  set myFolder to choose folder
    >  addprefix(item 1 of myFolder as alias)

    Your problem is that last line.  “item 1 of myFolder” is a specific command for the Finder.  It needs to be enclosed in tell “Finder end tell lines.  Like this:

    on run
      set myFolder to choose folder
      tell application “Finder”
          item 1 of myFolder
      end tell
    end run

    However, this will only return the value of the *first* item in that folder.  As I understand it you want to add the prefix “temp” to *all* files in that folder.
    >
    > on addprefix(myFolder)
    >  tell application “Finder”
    >    repeat with x in every file of myFolder
    >        set name of x to (“temp ” & name of x)
    >    end repeat
    >  end tell
    > end addprefix

    This script is using a technique called a subroutine—where a bunch of commands can be called up many times from within a script by typing one command.  In this case addprefix() calls up the on addprefix() commands.  I’ll be covering this in a future AppleScript article.  However, you don’t need to use a subroutine because the script is small.  You can use the script that I created:

    set myFolder to choose folder
    tell application 
    "Finder"
        
    activate
        set theFiles to every file of myFolder
        repeat with x in theFiles
            set name of x to 
    ("temp " displayed name of x)
        
    end repeat
    end tell 

    To point out of few things:
    1) Note that I had to convert every file of myFolder into a variable (theFiles) first.  Mac OS X’s finder requires that the files & folders need to be put into a variable first.
    2) I use displayed name because if I just used name, Mac OS X might add the .xxx file extention after my rename command.

    I learn something new everyday! 

    You’ll be running on OS X, so there’s no need to worry about the 31 character limit, so that script should run without any errors.  I hope this helps!


    _________________
    Stephen Swift - The Burnum Man
    AppleScript Guru - The Mac Observer
    Helping Scripters All Over the Web!

    <font size=-1>[ This Message was edited by: Burnum on 2002-02-04 20:16 ]</font>