Duplicate file in finder (Script Editor vs Xcode)

Hello
i want to duplicate and rename a file in Finder.
But the script works well in script editor but not in xcode

In script Editor :

tell application "Adobe Illustrator"
	set pathfichier to POSIX path of (file path of current document as string)
	-- set myFile to save current document in file (pathfichier) with options {PDF compatible:false, embed ICC profile:true}
end tell
set ABC to POSIX file (pathfichier as string)
tell application "Finder"
	set newFile to duplicate file (ABC as alias)
	set name of newFile to "theNewfile.ai"
end tell

in Xcode: a button call the action “duplicateFile” :

script AppDelegate
    property parent : class "NSObject"
    
    -- IBOutlets
    property theWindow : missing value
    
    on duplicateFile_(sender)
        tell application "Adobe Illustrator"
            set pathfichier to POSIX path of (file path of current document as string)
            -- set myFile to save current document in file (pathfichier) with options {PDF compatible:false, embed ICC profile:true}
        end tell
        set ABC to POSIX file (pathfichier as string)
        tell application "Finder"
            set newFile to duplicate file (ABC as alias)
            set name of newFile to "theNewfile.ai"
        end tell
    end duplicateFile_
    
    on applicationWillFinishLaunching_(aNotification)
        -- Insert code here to initialize your application before any files are opened
    end applicationWillFinishLaunching_
    
    on applicationShouldTerminate_(sender)
        -- Insert code here to do any housekeeping before your application quits
        return current application's NSTerminateNow
    end applicationShouldTerminate_
    
end script

I have the error : “2020-04-20 13:52:01.817903+0200 duplicate-file[3010:2255951] *** -[AppDelegate duplicateFile:]: Can’t make «script» into type Unicode text. (error -1700)”

Have you an idea why it doesn’t work in xcode ?
Thanks
regards
Chris

You are converting and coercing the types too much back an forth

This is sufficient

tell application "Adobe Illustrator"
	set pathfichier to file path of current document
	-- set myFile to save current document in file (pathfichier) with options {PDF compatible:false, embed ICC profile:true}
end tell
tell application "Finder"
	set newFile to duplicate pathfichier
	set name of newFile to "theNewfile.ai"
end tell

But in Xcode it’s highly recommended not to use the Finder to copy files

on duplicateFile:sender
	tell application "Adobe Illustrator"
		set pathfichier to POSIX path of (file path of current document as text)
		-- set myFile to save current document in file (pathfichier) with options {PDF compatible:false, embed ICC profile:true}
	end tell
	set sourceURL to current application's NSURL's fileURLWithPath:pathfichier
	set destinationURL to sourceURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:"theNewfile.ai"
	set {success, FMError} to current application's NSFileManager's defaultManager's copyItemAtURL:sourceURL toURL:destinationURL |error|:(reference)
	if FMError is not missing value then
		display dialog FMError's localizedDescription() as text
		return
	end if
end duplicateFile:

Thanks Stefan
i will try it today. I have just question : why do you prefer to use NSURL than POSIX ?
I tough POSIX is more use in last system like High sierra and other after.
Regards
Chris

NSURL and POSIX are not related.

NSURL belongs to AppleScriptObjC and represents an object with a lot of capabilities like gathering meta data.

POSIX (path) belongs to AppleScript and is just a string path (slash separated).

The reason not to use the latter with the Finder is that the Finder doesn’t support POSIX paths and the reason to use NSURL in an Xcode project is that the Foundation framework is much more efficient than sending Apple Events to the Finder.

Not to mention that addressing fewer apps means fewer authorization dialogs to deal with.

i try to find on developper apple all description about NSURL, but it’s not very explicit how to use it.
Do you know where i can find example for those function.
For example : how can i found the parent folder of this file by using deletingLastComponent ?
Regards