Finder - Duplicate, Rename, Reveal ISSUE ?

I’m writing what should be a simple script, but I’m obviously missing something between renaming & revealing the file (I haven’t spent much time with duplicating files w/applescript). I have simplified my script, because it will be triggered within a FileMaker Database, and am using the reveal just for testing –


set testFileOld to POSIX file "/Users/schuylerjohnson/Desktop/duplication test/testfile.rtf"
set testFileNewName to "testfile2.rtf"

tell application "Finder"
	
	set testFileNew to duplicate file testFileOld
	
	set name of testFileNew to testFileNewName
	
	reveal testFileNew
	
end tell

The issue is AppleScript is trying to reveal “testfile copy.rtf”, not “testfile2.rtf”!

“error “Finder got an error: Can’t get document file "testfile copy.rtf" of folder "duplication test" of folder "Desktop" of folder "schuylerjohnson" of folder "Users" of startup disk.” number -1728 from document file “testfile copy.rtf” of folder “duplication test” of folder “Desktop” of folder “schuylerjohnson” of folder “Users” of startup disk”

What step am I missing so that “testFileNew” becomes associated with “testfile2.rtf” rather than “testfile copy.rtf” ?

Thank you guys!

Hi. Welcome to MacScripter.

Your duplication and renaming are working OK. The problem in the ‘reveal’ line is that the Finder references to the files are name references and ‘testFileNew’ is a reference to the duplicate file before it was renamed! There are various ways to code round this. The simplest here would be to coerce the duplicate’s reference to an alias as soon as you get it. An alias refers to the item whatever it may subsequently be named or wherever it may subsequently be moved.


set testFileOld to POSIX file "/Users/schuylerjohnson/Desktop/duplication test/testfile.rtf"
set testFileNewName to "testfile2.rtf"

tell application "Finder"
	
	set testFileNew to (duplicate file testFileOld) as alias
	
	set name of testFileNew to testFileNewName
	
	reveal testFileNew
	
end tell

Ah! I knew it must have been something simple like that! I need to refresh on when to use aliases, etc, to avoid situations like these :smiley:

Thank you so much! :cool: