• 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 a copied file with Finder
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Renaming a copied file with Finder


  • Subject: Re: Renaming a copied file with Finder
  • From: Simon Forster <email@hidden>
  • Date: Mon, 9 Oct 2006 15:55:33 +0100


On 9 Oct 2006, at 15:29, Yvan KOENIG wrote:

and what if (name of file_to_copy) & " copy" already exists ? ;-)

The following (below sig) is a (very) old library function of mine. No doubt far more verbose than it needs to be but it seems to work OK. It does assume that if a full point is present, the last bit after the last full point is a suffix. Oh, requires getTextItem and makeList functions which I've included.


HTH

Simon Forster
_______________________________________________________
 LDML Ltd, 62 Pall Mall, London, SW1Y 5HZ, UK
 Tel: +44 (0)20 8133 0528   Fax: +44 (0)70 9230 5247
_______________________________________________________


--Copy a file from one location to another without overwriting
--Adds copy n before suffix to give unique name
--on safeArchive("Macintosh HD:Users:fred:desktop:test.txt", "Macintosh HD:Users:fred:desktop:backup:bert.txt")
--will copy to file "bert copy n.txt" if "bert.txt" or "bert copy n-1.txt" exists
on safeArchive(sourceFile, targetFile)
global libString
try
set targetRoot to getTextItem(targetFile, ":", 1, -2)
set targetName to getTextItem(targetFile, ":", -1, -1)
tell application "Finder"
repeat while (alias targetFile) exists
set targetName to getTextItem(targetFile, ":", -1, -1)
if targetName contains " copy " then
set tmp1 to makeList(targetName, " copy ")
set tmp2 to makeList(item 2 of tmp1, ".")
set targetName to (item 1 of tmp1 & " copy " & ((item 1 of tmp2 as integer) + 1) as string)
if (count of items in tmp2) > 1 then set targetName to targetName & "." & item 2 of tmp2
else
set tmp1 to makeList(targetName, ".")
if (count of items in tmp1) > 1 then
set text item delimiters of AppleScript to {"", text item delimiters of AppleScript}
set targetName to (items 1 thru -2 of tmp1 as string) & " copy 1." & item -1 of tmp1
set text item delimiters of AppleScript to {item 2 of text item delimiters of AppleScript}
else
set targetName to targetName & " copy 1"
end if
end if
set targetFile to targetRoot & ":" & targetName
end repeat
set fileToMove to duplicate alias sourceFile
set fileToMoveName to name of fileToMove
move fileToMove to folder targetRoot
set name of alias (targetRoot & ":" & fileToMoveName) to targetName
end tell
on error errorMsg number errorNumber from errorObj
error "finder.scpt safeArchive(sourceFile, targetFile). Error message: " & errorMsg number errorNumber from errorObj
end try
end safeArchive


--Return text items from text theText using the delimiter delim
--Always strips trailing delim where present before getting text
--getTextItem("fred:bert:basset:", ":", -1, -1)
--getTextItem("fred:bert:basset", ":", -1, -1)
--will return same result
--return {true, "basset"} on success
--return {false, <error message>) on error
on getTextItem(theText, delim, indexStart, indexEnd)
if theText ends with delim then set theText to characters 1 thru -2 of theText as string
set text item delimiters of AppleScript to {delim, text item delimiters of AppleScript}
try
set tmp to (text items indexStart thru indexEnd of theText as string)
on error errorMsg number errorNumber from errorObj
set text item delimiters of AppleScript to {item 2 of text item delimiters of AppleScript}
error "string.scpt getTextItem(theText, delim, indexStart, indexEnd). Error message: " & errorMsg number errorNumber from errorObj
end try
set text item delimiters of AppleScript to {item 2 of text item delimiters of AppleScript}
return tmp
end getTextItem


on makeList(theText, delim)
set text item delimiters of AppleScript to {delim, text item delimiters of AppleScript}
try
set tmp to every text item of theText
on error errorMsg number errorNumber from errorObj
set text item delimiters of AppleScript to {item 2 of text item delimiters of AppleScript}
error "string.scpt makeList(theText, delim, indexStart, indexEnd). Error message: " & errorMsg number errorNumber from errorObj
end try
set text item delimiters of AppleScript to {item 2 of text item delimiters of AppleScript}
return tmp
end makeList



_______________________________________________ Do not post admin requests to the list. They will be ignored. Applescript-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Renaming a copied file with Finder (From: ishmael <email@hidden>)
 >Re: Renaming a copied file with Finder (From: Michelle Steiner <email@hidden>)
 >Re: Renaming a copied file with Finder (From: Yvan KOENIG <email@hidden>)

  • Prev by Date: Re: Renaming a copied file with Finder
  • Next by Date: Re: Creating an environment variable from AppleScript
  • Previous by thread: Re: Renaming a copied file with Finder
  • Next by thread: Re: Renaming a copied file with Finder
  • Index(es):
    • Date
    • Thread