• 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: changing the name of a file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: changing the name of a file


  • Subject: Re: changing the name of a file
  • From: Yvan KOENIG <email@hidden>
  • Date: Sat, 10 Oct 2009 13:22:51 +0200


Le 10 oct. 2009 à 06:48, Doug Tallman a écrit :

I have a script that copies files from my download folder onto an external drive then deletes them from my work drive.

The guts of it look like this:

on run
tell application "Finder"
set fileList to the selection
repeat with fn in fileList
try
move fn to folder "Cupertino:Users:douglastallman:Documents:Downloads:" replacing ask
on error
set newName to my erred(fn)
set name of fn to newName
move fn to folder "Cupertino:Users:douglastallman:Documents:Downloads:" replacing ask
end try
move fn to trash
end repeat
end tell
end run

Here is my preferred code:

on run
tell application "Finder" to set fileList to the selection
set destFolder to (path to documents folder) as text
repeat with fn in fileList
set fn to fn as alias
tell application "System Events"
set newName to name of disk item (fn as text)
if exists disk item (destFolder & newName) then set newName to my buildNewName(newName)
end tell
set originalFile to quoted form of (POSIX path of fn)
set newFile to quoted form of (POSIX path of (destFolder & newName))
do shell script "cp -n -p " & originalFile & space & newFile
tell application "System Events" to delete disk item (fn as text)
end repeat
end run

on buildNewName(f)
set AppleScript's text item delimiters to "."
set l to text items of f
set newName to (items 1 thru -2 of l) as text
set ext to item -1 of l
set AppleScript's text item delimiters to ""
return newName & (do shell script "date +_%Y%m%d-%H%M%S") & "." & ext
end buildNewName

And I assume that there is a Shell commant able to replace the use ot System Events to delete the original file.

Yvan KOENIG (VALLAURIS, France) samedi 10 octobre 2009 13:22:21


 _______________________________________________
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

References: 
 >changing the name of a file (From: Doug Tallman <email@hidden>)

  • Prev by Date: Re: Addressing a cell in "Numbers"
  • Next by Date: Re: changing the name of a file
  • Previous by thread: changing the name of a file
  • Next by thread: Re: changing the name of a file
  • Index(es):
    • Date
    • Thread