Sunday, December 17, 2017

#1 2017-11-26 04:00:32 am

ldicroce
Member
Registered: 2017-11-25
Posts: 2

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 !

Offline

 

#2 2017-11-26 05:23:32 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4457

Re: Rename and moving files without loosing the link to them

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.  hmm  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:

Applescript:

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.

Last edited by Nigel Garvey (2017-11-26 05:28:03 am)


NG

Offline

 

#3 2017-11-26 05:29:30 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Rename and moving files without loosing the link to them

Here, this simple code behaves flawlessly.

Applescript:

# 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

Last edited by Yvan Koenig (2017-11-26 05:31:46 am)

Offline

 

#4 2017-11-26 06:41:36 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5200

Re: Rename and moving files without loosing the link to them

Nigel Garvey wrote:

The first script should work!



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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2017-11-27 06:29:20 am

ldicroce
Member
Registered: 2017-11-25
Posts: 2

Re: Rename and moving files without loosing the link to them

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 !

Last edited by ldicroce (2017-11-27 06:29:48 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)