• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re:Renaming files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re:Renaming files


  • Subject: Re:Renaming files
  • From: email@hidden
  • Date: Thu, 21 Apr 2011 02:47:23 +0000
  • Sensitivity: Normal

Hi Thomas,

First off, I'm curious if a file with the same name (as what you're renaming
your file to) already exists in the same directory?  If this is the case, the
Finder (at least in OS 10.6) seems to happily continue without creating the
new file and not throwing an error.  In this case, you may want to use
'list folder' to get the current contents and check if that file name already
exists beforehand.

Most people that use AppleScript try to avoid using the Finder, since it's
quite clunky.  I usually only use it to move, duplicate, rename and update
items.  I've noticed too that sometimes when I try to rename folders/files,
it sometimes doesn't work like I was hoping.  Not sure why this is the case
though.  Another thing you might try is checking to see if your file was
renamed and if not, maybe loop a few times and see if you can force it.
I haven't tried this, but here's some code to give you a possible idea:

--

set folderPath to "Snow_Leopard:Users:jyoung:Desktop:test:"
set myFile to "Snow_Leopard:Users:jyoung:Desktop:test:textfile.rtf"
set newName to "newFile.txt"

renameFile(folderPath, myFile, newName)

on renameFile(folderPath, myFile, newName)
	set itemList to listFolder(folderPath)

	if newName is in itemList then
		error "File already exists!"
		-- If file already exists, maybe delete it beforehand?
		-- Be careful using system events to delete a file though
		-- because you won't get it back!
		-- tell application "System Events" to delete alias (folderPath & newName)
	end if

	repeat 5 times
		tell application "Finder" to set alias myFile's name to newName
		delay 1
		if newName is in listFolder(folderPath) then exit repeat
	end repeat
end renameFile

on listFolder(itemPath)
	tell application "System Events" to return (list folder alias itemPath without invisibles)
end listFolder

Jay
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Renaming files
      • From: Thomas Fischer <email@hidden>
  • Prev by Date: Re: Closing a file you left open
  • Next by Date: Re: system services ?
  • Previous by thread: Renaming files
  • Next by thread: Re: Renaming files
  • Index(es):
    • Date
    • Thread