Rename and moving files without loosing the link to them

My understanding is that a link to a file as alias should be retained even if i move or rename the file.
But that seems not the case. If I do that (see script below) I get an error.

tell application “Finder”
set this_item to (choose file) as alias
set the name of this_item to “Luciano.jpg”
set TargetFolderName to alias “MacBook HD:Users:ldicroce:Desktop:Test2:”
move this_item to TargetFolderName – with replacing
open this_item – as alias
end tell

The only way around is to update the full name (including the path) of the item after rename it or after move it. Such as the script below.

tell application “Finder”
set this_item to (choose file) as alias
set the name of this_item to “Luciano.jpg”
display dialog (this_item as string)
set this_item to “MacBook HD:Users:ldicroce:Desktop:Luciano.jpg” as alias
set TargetFolderName to alias “MacBook HD:Users:ldicroce:Desktop:Test2:”
move this_item to TargetFolderName – with replacing
set this_item to “MacBook HD:Users:ldicroce:Desktop:Test2:Luciano.jpg” as alias
open this_item – as alias
end tell

Is there any better way to do this? Thanks !

Hi. Welcome to MacScripter.

The first script should work! As long as you’re moving the file to another location on the same disk, the alias returned by choose file should still refer to it. :confused: What system version are you using?

If it’s definitely not working for you, another way round the problem would be to use the reference returned by the Finder’s move command:

tell application "Finder"
	set this_item to (choose file) as alias
	set the name of this_item to "Luciano.jpg"
	set TargetFolderName to alias "MacBook HD:Users:ldicroce:Desktop:Test2:"
	set newLoc to (move this_item to TargetFolderName) -- Returns a Finder name reference to the file in its new location.
	open newLoc
end tell

When posting scripts on MacScripter, would you mind using our special [applescript] and [/applescript] tags? There’s a button for them on the posting form. They make the scripts appear in a box with a clickable link, as above.

Here, this simple code behaves flawlessly.

# Trigger features of the OSAX StandardAdditions out of the tell application block.
set this_item to (choose file)
set TargetFolderPath to ((path to desktop as text) & "Test2:") as alias

tell application "Finder"
	set the name of this_item to "Luciano.jpg"
	move this_item to TargetFolderPath -- with replacing
	open this_item -- as alias
end tell

Yvan KOENIG running High Sierra 10.13.1 in French (VALLAURIS, France) dimanche 26 novembre 2017 11:28:08

FWIW, a similar issue came up elsewhere recently. I believe the combination of 10.13.x and APFS is involved.

Thanks a lot for the Welcome and for the suggestions.
(I will use tags next time. Sorry!)

It is indeed a problem of the disk formatting.
When I run the script on the iMac (with Journaled HFS+) I have no problem. But the same script on my MacBook (APFS) I get the error. In both cases the OS is the same (macOS 10.13.1 (17B48)).

This is quite annoying …
Anybody know when they will fix it?
Thanks again !