Renaming a file using AppleScript

The following AppleScript function renames a file. If you thought this to be a simple thing, try to write it without looking here below. I spend a lot time on this, I might not even use it, but here it is for other mortals wishing to lose weight exercising AppleScript:

to renameFile(oldAlias, newFileName)
 tell application "Finder"
  set f to item (oldAlias as text)
  tell f
   set ext to its name extension
   set nFn to (newFileName & "." & ext)
   set its name to nFn
  end tell
 end tell
 set tmp to my splitString(oldAlias as text, ":")
 set the last item of tmp to nFn
 return my joinList(tmp, ":")
end renameFile

No comments in code, I do not want to spoil your fun!

I needed while writing an export script for Aperture. Here is how I used it after a photo was exported from Aperture:

tell application "Aperture"
 set dstFile to (export imageSel using export setting defExportSetting to fldAlbum)
end tell
set dstFile to my renameFile(dstFile, imgId)

Comments

Van Deputte Eric

Hi, sorry for bothering you …

I’m very ignorant about applescript .. but as I use Filemaker Pro (fop) I need the possibility to rename - by starting a applescript- some little incoming file, which has always a different name to the same name “medini.txt” By doing that I can then automate a importscript (in fmp) ( = to import and delete afterwards the little file)

Would you mind helping me for this simple(?) script …?

I’m trying the following: - given a file with a random name p.e.: “11077_80523812.txt” ( downloaded from a website) - in a folder let’s say Macintosh HD: Medini - By starting the script I would like to be invited to freely select/choose this kind of file - AND so that after my selection the filename of the chosen file changes automatically to (always the same) name: “medini.txt”… So I would be possible to import it in my fop-database and (after the import) delete the little file (that I can already do in .. applescript ;-))

Eric Van Deputte Lennik Belgium www.vwin.be evdp@vwin.be

Cagy
WTF? That many lines of ‘code’ to rename a file? Is there no ‘native’ method to do this? I’m new (and skeptic) to applescript but have to use it for a project…