Duplicate and Rename a file

This worked in 10.5, but now it doesn’t.

Could’ve been neater, but it worked. Basically copy all PDFs or one folder to another and rename them.

set this_folder to ("/Volumes/PDF'd Pages" as POSIX file) as string
tell application "System Events"
	set these_files to every file of folder this_folder whose name does not start with "." and (file type is "PDF" or file type is "pdf" or name extension is "pdf")
end tell
set myNewFilePath to ("/Volumes/Web Files/" as POSIX file)
tell application "Finder"
	repeat with i from 1 to the count of these_files
		set thisFile to (item i of these_files as alias)
		set thisName to name of thisFile
		set newFile to duplicate thisFile to myNewFilePath
		set newname to "pg" & i & ".pdf"
		set name of newFile to newname ---errors here
	end repeat
end tell

newFile is returning as a document file, which I believe is a class. I can not have the script return properties of the thing either.

return properties of newFile

error “Finder got an error: Can’t get document file "799495.pdf".” number -1728 from document file “799495.pdf”

Hey There,

You don’t mention which OSX you’re using now.

Try this:

tell application "Finder"
	set this_folder to disk "PDF'd Pages" as alias
	set myNewFilePath to disk "Web Files" as alias
	set fileList to (every file of this_folder whose name does not start with "." and (kind is "PDF" or kind is "pdf" or name extension is "pdf")) as alias list
	if fileList ≠ {} then
		repeat with _idx from 1 to the length of fileList
			set thisFile to (item _idx of fileList)
			set newFileName to "pg" & _idx & ".pdf"
			set newFile to duplicate thisFile to myNewFilePath
			set name of newFile to newFileName
		end repeat
	end if
end tell

This looks a bit redundant, but YMMV.

Sorry, meant to include the system info, but didn’t check the box.

I can not test the code you provided at this time, but I wanted to thank you for offering assistance.

AppleScript: 2.2.4
Browser: Safari 537.36
Operating System: Mac OS X (10.8)

Don’t use POSIX path there. The “set name .” line as is, needs mac path. This works for my in ML 10.8.4:

set this_folder to path to desktop as text -- just for testing
tell application "System Events"
	set these_files to every file of folder this_folder whose name does not start with "." and (file type is "PDF" or file type is "pdf" or name extension is "pdf")
end tell
set myNewFilePath to (path to desktop as text) & "test folder" -- just for testing
tell application "Finder"
	repeat with i from 1 to the count of these_files
		set thisFile to (item i of these_files as alias)
		set thisName to name of thisFile
		set newFile to duplicate thisFile to myNewFilePath
		set newname to "pg" & i & ".pdf"
		set name of newFile to new name -- it works here
	end repeat
end tell

Lithodora used POSIX file, not POSIX path:

set myNewFilePath to ("/Volumes/Web Files/" as POSIX file) as text
-->"<boot drive name>:Volumes:Web Files:"

Perfectly OK, when you remember it is text, not a Finder object specifier.

The first one fails because the terminating slash is missing, so the result has no terminating colon.
The second one fails because the as text clause is missing.

Strange as it may seem almost none of this has worked for me:

error "Finder got an error: Can't set document file \"799495.pdf\" to \"pg1.pdf\"." number -10006 from document file "799495.pdf"

The line it fails on regardless is:

set name of newFile to newname 

It copies the file just fine, but fails to rename it.

With the exception of the code posted by Flex20:

set this_folder to path to desktop as text -- just for testing
tell application "System Events"
   set these_files to every file of folder this_folder whose name does not start with "." and (file type is "PDF" or file type is "pdf" or name extension is "pdf")
end tell
set myNewFilePath to (path to desktop as text) & "test folder" -- just for testing
tell application "Finder"
   repeat with i from 1 to the count of these_files
       set thisFile to (item i of these_files as alias)
       set thisName to name of thisFile
       set newFile to duplicate thisFile to myNewFilePath
       set newname to "pg" & i & ".pdf"
       set name of newFile to new name -- it works here
   end repeat
end tell

This works great if I’m working on the Desktop, which means it is something about how I’m pointing to the folder on the server.

I had simplified the path of the folders I was using to make the example easier to read, but looking back it was the wrong thing to do as it made the folders seem to be Volumes…

   set this_folder to disk "PDF'd Pages" as alias
   set myNewFilePath to disk "Web Files" as alias

Will not actually work as the path is actually:

/Volumes/SpecialSections/Special Pages O-R/R/Web Files/

The only reason I used POSIX file is… because I’m lazy. When you drag a folder from the Finder into the editor that is what it gives you. Rather than reformat the path I coerce it as needed. In order to figure out if this was the problem I did change it to “SpecialSections:Special Pages…” which gives me the same ‘can not rename file’ error.

AppleScript: 2.2.4
Operating System: Mac OS X (10.8)

I have figured it out using a bit of a workaround…instead of using:

set name of newFile to new name

I have used:

set name of alias ((myNewFilePath & thisName) as text) to newFileName

Since that seems to work I guess it’ll have to do…