Monday, April 22, 2019

#1 2019-03-22 05:45:07 am

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

File vs Alias - Advantages and disadvantages ?

I should know .... but I don't ....
Which are the advantages (or disadvantages) to work with files or aliases?

If I have this script (see below), when should I use alias and when files?


Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set myPath to "MacBook HD:Users:ldicroce:Desktop:Mol Cell_Best.pdf" -- this is a path for a real file

set myAlias to myPath as alias
checkIfExist(myAlias)


set myFile to POSIX file (POSIX path of myPath)
checkIfExist(myFile)

on checkIfExist(path)
   tell application "Finder"
       if path exists then
           display dialog "Exists"
       else
           display dialog "No exists"
       end if
   end tell
end checkIfExist

Offline

 

#2 2019-03-22 06:30:23 am

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 864

Re: File vs Alias - Advantages and disadvantages ?

Hi. An alias checks itself for existence; e.g.:

Applescript:

set myPath to "MacBook HD:Users:ldicroce:Desktop:Mol Cell_Best.pdf"
alias myPath --fails or doesn't

Another advantage is that an alias has a memory, in that it can be tracked after moving, however, some apps specifically want to receive a string, alias, file, or—rarely—a POSIX file, as evidenced by their dictionary requirements.

Offline

 

#3 2019-03-22 07:41:18 am

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

Re: File vs Alias - Advantages and disadvantages ?

Thanks, Marc.
Those are good points.

Offline

 

#4 2019-03-23 03:39:30 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 556

Re: File vs Alias - Advantages and disadvantages ?

Marc Anthony wrote:

Another advantage is that an alias has a memory, in that it can be tracked after moving…

Are you not now referring to a Finder alias? Not the same thing, IMHO.
A Finder alias is a file, an A/S alias is not. I can have

Applescript:

set thisAlias to alias "path:to:some:file"
-- and
set thatAlias to alias "path:to:some:Finder_alias"
-- Note: this code will not compile when those files do not exist

Yes, a Finder alias "knows" where its original is, even when the original is moved.
An A/S alias cannot do that - if you move the file that it points to the connection is lost.
Unless, of course, you point it to a Finder alias. But you cannot move the alias file, only the original, for the connection to remain intact.

Offline

 

#5 2019-03-23 05:51:37 am

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

Re: File vs Alias - Advantages and disadvantages ?

alastor933 wrote:

Yes, a Finder alias "knows" where its original is, even when the original is moved.
An A/S alias cannot do that - if you move the file that it points to the connection is lost.




From the AppleScript Language Guide:

An alias object is a dynamic reference to an existing file system object. Because it is dynamic, it can maintain the link to its designated file system object even if that object is moved or renamed.



Later on there's an example of an alias in code, and it says:

Once you run the previous example, creating the alias, the script will be able to find the original file when you run it again, even if the file’s name or location changes.



However…

I wonder if that has changed recently. I'm pretty sure that something like this:

Applescript:

set docsFolder to path to documents folder
set thePath to (path to desktop as text) & "Test.app"
set theAlias to alias thePath
tell application "Finder"
   move file thePath to docsFolder
end tell
display dialog (theAlias as text)

Used to display the HFS path of the moved file. But when I run it here under Mojave, it actually displays the POSIX path of the original location of the file. Coercion of an alias to text should always return an HFS path.

So I'm wondering if resolving of modified aliases has been broken in recent versions of the OS. If so, it's not totally surprising. AppleScript aliases used the same resolving mechanism as alias files used to, but alias files now new use a completely different mechanism (bookmarks). There's no sign that AppleScript has been similarly updated. (I guess it could also be related to APFS rather than the OS version, too.)


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

Offline

 

#6 2019-03-23 06:21:00 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 556

Re: File vs Alias - Advantages and disadvantages ?

I did check before I posted, and here A/S aliases don't follow a moved file. I'm running 10.11.

Offline

 

#7 2019-03-23 07:26:19 am

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 864

Re: File vs Alias - Advantages and disadvantages ?

All aliases are file pointers, but not all aliases are files in the alias class; i.e. Finder aliases.
This works as expected under Sierra. I can't speak to later versions' behaviour.

Applescript:


tell application "Finder" to make file with properties {name:"test File"}
set targ to alias ((path to desktop as text) & "test File") --not a Finder alias
tell application "Finder" to move targ to trash
log targ --note that this resolves to the same file, but HFS path is updated
tell application "Finder" to move targ to (make folder)

Last edited by Marc Anthony (2019-03-23 07:41:50 am)

Offline

 

#8 2019-03-23 08:33:06 am

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

Re: File vs Alias - Advantages and disadvantages ?

Marc Anthony wrote:

All aliases are file pointers, but not all aliases are files in the alias class; i.e. Finder aliases.
This works as expected under Sierra. I can't speak to later versions' behaviour.


It doesnt work in Mojave.
When I do "log targ", I get:
(*alias /Users/ldicroce/Desktop/test File*)
It is not updated.

And when I do "tell application "Finder" to move targ to (make folder)", I get:
Finder got an error: File some object wasn’t found

Last edited by ldicroce (2019-03-23 08:33:36 am)

Offline

 

#9 2019-03-23 12:38:45 pm

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

Re: File vs Alias - Advantages and disadvantages ?

Marc Anthony wrote:

All aliases are file pointers, but not all aliases are files in the alias class; i.e. Finder aliases.
This works as expected under Sierra. I can't speak to later versions' behaviour.


The script behaves flawlessly under High Sierra.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 23 mars 2019 19:38:39

Offline

 

#10 2019-03-23 02:58:49 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 60

Re: File vs Alias - Advantages and disadvantages ?

I'm running Mojave and I get the same exact result as Idicroce. This is an interesting thread.

Offline

 

#11 2019-03-23 04:08:46 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: File vs Alias - Advantages and disadvantages ?

You should not expect AppleScript aliases to behave in the same way as Finder aliases. Do not wait for this is just logical.

Accurate tracking of the movement of the original files of Finder aliases is possible because the Finder is active all time while your computer is turned on. Interpretator AppleScript is active only during the execution of the script.

Applescript alias exists only for the duration of the script. This unit is stored in the RAM, not on the disk. That is, it is also always a "file" (some record, which is actually some variable), but this is a temporary 'file", which is immediately lost after the script is completed. But logically, this AppleScript alias should track the movement of the original file (using the script itself!) at least during the execution of the script. Otherwise, it would be a bug of AppleScript.

For example, Applescript alias should track such a move of the source file:

Applescript:



set myTestAlias to alias "Χωρίς τίτλο:Users:123:Desktop:Infix_Google_Translate.scpt"

tell application "Finder"
   set sourceFolder to folder "Desktop" of folder "123" of folder "Users" of startup disk
   set destinationFolder to folder "Movies" of folder "123" of folder "Users" of startup disk
   set myFile to file "Infix_Google_Translate.scpt" of sourceFolder
   move myFile to destinationFolder
end tell

myTestAlias

Run the script and see the result (You must replace the path of my test file with your own test file path). This test will show you that AppleScript tracking alias-value while executing a script is excellent:

           alias "Χωρίς τίτλο:Users:123:Desktop:Infix_Google_Translate.scpt"

automatically is changed to this:

            alias "Χωρίς τίτλο:Users:123:Movies:Infix_Google_Translate.scpt"

Last edited by KniazidisR (2019-03-23 06:31:15 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#12 2019-03-23 07:17:10 pm

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

Re: File vs Alias - Advantages and disadvantages ?

KniazidisR wrote:

You should not expect AppleScript aliases to behave in the same way as Finder aliases. Do not wait for this is just logical.



The reason they were called aliases is precisely because they behaved the same as file aliases, and the underlying code was the same.

Accurate tracking of the movement of the original files of Finder aliases is possible because the Finder is active all time while your computer is turned on. Interpretator AppleScript is active only during the execution of the script.



Sorry, but that's just not true.

Applescript alias exists only for the duration of the script. This unit is stored in the RAM, not on the disk.



Again, this is just not so.


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

Offline

 

#13 2019-03-23 07:21:03 pm

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

Re: File vs Alias - Advantages and disadvantages ?

Yvan Koenig wrote:

The script behaves flawlessly under High Sierra



And fails here under Mojave. From the log:

Applescript:

tell application "Finder"
   make new folder
       --> folder "untitled folder 3" of folder "Desktop" of folder "shane" of folder "Users" of startup disk
   move alias "/Users/shane/Desktop/test File" to folder "untitled folder 3" of folder "Desktop" of folder "shane" of folder "Users" of startup disk
       --> error number -43
Result:
error "Finder got an error: File some object wasn’t found." number -43


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

Offline

 

#14 2019-03-24 12:02:12 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: File vs Alias - Advantages and disadvantages ?

Shane Stanley wrote:



The reason they were called aliases is precisely because they behaved the same as file aliases, and the underlying code was the same.

Sorry, but that's just not true.

Again, this is just not so.



'Everyday AppleScriptObjC', Third Edition, by Shane Stanley.

Oh, thank you, author. I scratch my head. Probably, I spoke here somewhat inaccurately in the subject. It will be interesting to know what I am mistaken about AppleScript. I do not know if your book exists in Russian and in Greek. If not, I would love to translate it into these languages perfectly. I myself read and understand English fluently, so that I will read your book thoroughly.

Last edited by KniazidisR (2019-03-24 12:09:38 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#15 2019-03-24 04:55:23 am

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

Re: File vs Alias - Advantages and disadvantages ?

@ Shane STANLEY

I wish to know exactly the step, which introduce a new behavior.
Here these instructions :

Applescript:

tell application "Finder"
   set targ to make file with properties {name:"test File"}
   log "point 1"
   set targ1 to targ as alias
   log "point 2"
end tell
set targ2 to targ as alias

issue the log:

tell application "Finder"
    make with properties {name:"test File"} new file
        --> document file "test File" of folder "Desktop" of folder "**********" of folder "Users" of startup disk
    (*point 1*)
    get document file "test File" of folder "Desktop" of folder "**********" of folder "Users" of startup disk
        --> alias "SSD 500:Users:**********:Desktop:test File"
    (*point 2*)
    get document file "test File" of folder "Desktop" of folder "**********" of folder "Users" of startup disk
        --> alias "SSD 500:Users:**********:Desktop:test File"
end tell

If I understand well, on your side, one of these steps return a POSIX path (or a POSIX file).

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mars 2019 11:55:13

Offline

 

#16 2019-03-24 05:09:52 am

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

Re: File vs Alias - Advantages and disadvantages ?

Yvan Koenig wrote:

I wish to know exactly the step, which introduce a new behavior.



It happens after the file has been moved.


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

Offline

 

#17 2019-03-24 12:18:42 pm

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

Re: File vs Alias - Advantages and disadvantages ?

Thanks Shane.
So, if I understand well, the instruction move targ to trash doesn't change the contents of targ.
What if you code :

Applescript:

tell application "Finder"
   make file with properties {name:"test File"}
   --> document file "test File" of folder "Desktop" of folder "**********" of folder "Users" of startup disk
   --> are you getting that or "/Users/shane/test File"
   set targ1 to result as alias
   --> alias "SSD 500:Users:**********:Desktop:test File"
end tell
set targ to alias ((path to desktop as text) & "test File")
--> alias "SSD 500:Users:**********:Desktop:test File"
tell application "Finder"
   set targ2 to (move targ to trash)
   --> document file "test File" of folder ".Trash" of folder "**********" of folder "Users" of startup disk
   --> if I understand well here you would get : "/Users/shane/.Trash/test File"
   if (class of targ2) is text then set targ2 to POSIX file targ2
end tell
tell application "Finder" to move targ2 to (make folder)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 24 mars 2019 19:13:45

Offline

 

#18 2019-03-24 04:35:53 pm

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

Re: File vs Alias - Advantages and disadvantages ?

No, that works fine. It's not any command that causes the issue -- it's simply the fact that the file is not where it originally was. Aliases are meant to track their files, but it looks like under Mojave -- or perhaps under APFS -- they no longer do.


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

Offline

 

#19 2019-03-24 05:23:16 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: File vs Alias - Advantages and disadvantages ?

Shane Stanley wrote:

No, that works fine. It's not any command that causes the issue -- it's simply the fact that the file is not where it originally was. Aliases are meant to track their files, but it looks like under Mojave -- or perhaps under APFS -- they no longer do.



I run OS X Captain, so I can’t check if the following alias tracking example works under Mojave:

Applescript:



property the_alias : missing value
property last_pathname : ""

if the_alias is missing value then
set the_alias to choose file
else
set this_pathname to the_alias as string
if this_pathname = last_pathname then
display dialog "The file hasn't moved"
else
display dialog "Old file path:" & return & ¬
last_pathname & return & ¬
"New location: " & return & ¬
this_pathname
end if
end if

set last_pathname to the_alias as string

This is an example from one famous book. It was tracked the alias between runs of the script, even when the source file was moved or renamed, unless script was recompiled.

It would be interesting to know if it still works.

Last edited by KniazidisR (2019-03-24 05:33:27 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#20 2019-03-24 05:39:14 pm

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

Re: File vs Alias - Advantages and disadvantages ?

So I did a simple test again, this time on an external HFS+ disk:

Applescript:

set theAlias to alias "Untitled:Test.rtf"
tell application "Finder"
   move file "Untitled:Test.rtf" to folder "Untitled:untitled folder"
end tell
theAlias as text
--> "Untitled:untitled folder:Test.rtf"

This time it worked as documented. This rather confirms my suspicion that it's an APFS issue rather than OS-version related. Which makes sense: APFS doesn't support the underlying file ids used by the alias manager, and as the alias manager has been deprecated since macOS 10.8, it was never likely to be updated.


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

Offline

 

#21 2019-03-24 06:49:16 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 62

Re: File vs Alias - Advantages and disadvantages ?

Shane Stanley wrote:

This time it worked as documented. This rather confirms my suspicion that it's an APFS issue rather than OS-version related. Which makes sense: APFS doesn't support the underlying file ids used by the alias manager, and as the alias manager has been deprecated since macOS 10.8, it was never likely to be updated.



APFS has also another important for me issue. I couldn’t copy, and drag and drop files larger than 2 GB onto the USB-disk. It just killed me, since I had to use a terminal window for copying. So I abandoned High Sierra and rolled back to Captain.

Last edited by KniazidisR (2019-03-24 06:53:50 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#22 2019-03-25 12:28:10 am

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

Re: File vs Alias - Advantages and disadvantages ?

Shane Stanley wrote:

This rather confirms my suspicion that it's an APFS issue rather than OS-version related. Which makes sense: APFS doesn't support the underlying file ids used by the alias manager, and as the alias manager has been deprecated since macOS 10.8, it was never likely to be updated.



@ Shane
Hey,
what does this mean in practical terms  for us?
What should we use and what should we not use to be sure that our scripts will work in the future?
Should we avoid to use AS alias? Is that what you are suggesting ?
Thanks!

Offline

 

#23 2019-03-25 01:19:56 am

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

Re: File vs Alias - Advantages and disadvantages ?

In practical terms, all it means is that you can't rely on using properties to hold aliases in the expectation they'll resolve even if the files they refer to are moved. I don't think anything other than a small proportion of scripts ever did rely on this -- perhaps more did in the days pre-OS X, when where to store files was a more free-wheeling affair. But the lack of screaming suggests it's more one of those things that sounded good but wasn't often rally needed.

If you need this feature now, you can always use my PrefsStorageLib.


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

Offline

 

#24 2019-03-25 02:52:13 am

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

Re: File vs Alias - Advantages and disadvantages ?

Thanks !

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)