On Jun 27, 2015, at 09:38, Jean-Christophe Helary <email@hidden> wrote:I've been struggling all day with a "duplicate file" command that would work in some context and not in others.
It took me a search on the net to eventually realise that duplicate "preferred" aliases to posix files.
But I could not find anything in the Finder dictionary that indicated that.
______________________________________________________________________
Hey Jean-Christophe,
The MacOS long predates its transformation to Unix, and Unix paths are not universally supported.
Rule-of-the-thumb — NEVER use Posix paths or Posix Files in the Finder.
---------------------------------------------------------------
# Works tell application "System Events" set _dir to POSIX path of disk item "~/Documents" end tell
---------------------------------------------------------------
# Fails tell application "Finder" set _dir to POSIX path of disk item "~/Documents" end tell
---------------------------------------------------------------
tell application "Finder" set _posix to POSIX file _dir end tell
---------------------------------------------------------------
# Works set _posix to POSIX file _dir # Fails tell application "Finder" set fileList to files of _posix end tell
---------------------------------------------------------------
# Works set _folder to path to documents folder tell application "Finder" set fileList to files of _folder as alias list end tell
---------------------------------------------------------------
In general I advise people to NEVER use Posix Paths or Posix Files ANYWHERE — UNLESS they are needed and you KNOW they will work.
Aliases are still more portable than anything else (although not completely universal).
In the Finder NEVER use Finder References where you don't have to. Aliases and Alias-Lists are much faster.
--------------------------------------------------------------- # Finder References --------------------------------------------------------------- set _folder to path to documents folder tell application "Finder" set itemList to items of _folder end tell ---------------------------------------------------------------
--> Approximately 0.2 seconds on my system for 91 items.
--> Approximately 2.7 seconds on my system for 500 items.
--------------------------------------------------------------- # Aliases --------------------------------------------------------------- set _folder to path to documents folder tell application "Finder" set itemList to items of _folder as alias list end tell ---------------------------------------------------------------
--> Approximately 0.12 seconds on my system for 91 items.
--> Approximately 1.8 seconds on my system for 500 items.
---------------------------------------------------------------
The speed difference becomes more pronounced as there are more items.
The Finder shows its age. It's rather creaky and slow, and when scripting it you need to know what is and what is NOT sensible to do.
And NO such things are not documented by Apple.
We should have a comprehensive AppleScript Wiki, but we don't.
Haphazardly searching the Internet is not so bad for discovering methods of doing things, but it's not an especially good means of getting sage and up-to-date advice for best practices.
The Applescript Users List and MacScripter.net are probably the best places for that, and since we're a worldwide community you can often get answers to questions quickly — no matter what time of day or night you post them.
System Events Groks Posix Paths and Posix Files much better than the Finder.
--------------------------------------------------------------- tell application "Finder" to set _dir to insertion location as alias set _dir to POSIX path of _dir
tell application "System Events" if quit delay ≠ 0 then set quit delay to 0 set fileList to POSIX path of files of disk item _dir end tell
repeat with i in fileList set (contents of i) to alias POSIX file (contents of i) end repeat ---------------------------------------------------------------
--> Approximately 0.24 seconds on my system for 500 items — despite the step of converting to an alias list for better use in the Finder.
(You can copy/move files en-mass in the Finder using an alias-list.)
Again though you must be aware of System Events' quirks.
Above I'm converting System Events' normal output:
file "Ryoko:Users:chris:test_directory:Many_Files_500:.DS_Store" of application "System Events"
To Posix Paths.
It's faster than using System Events-References.
It's really odd that SEV uses its own reference-form instead of a more portable file-Url («class furl»), but this is what we have to work with.
SEV even understands home-path notation:
--------------------------------------------------------------- set myDocumentsFolderPosixPathHome to "~/Documents" tell application "System Events" set myDocumentsFolderPosixPathExpanded to POSIX path of disk item myDocumentsFolderPosixPathHome end tell ---------------------------------------------------------------
So. In summary:
I ALWAYS use HFS paths or Aliases or an Alias-List.
UNLESS I have a very specific reason to do something else.
If I have to bring paths from somewhere else into the Finder I try to always convert them into HFS-Paths or aliases or alias-lists. (This can very case-by-case if I know my output will be compatible with action I'm taking in the Finder.)
Note that the Finder will operate perfectly well on a list of file-urls (which is what a Posix File actual is).
------------------------------------------------------------------------------------------- set _dest to alias ((path to downloads folder as text) & "dest:")
tell application "Finder" set fileList to (files 1 thru 10 of (get insertion location)) as alias list end tell
repeat with i in fileList set (contents of i) to POSIX file (POSIX path of (contents of i)) end repeat
tell application "Finder" duplicate fileList to _dest end tell -------------------------------------------------------------------------------------------
But you can run into trouble when working with them in the Finder:
------------------------------------------------------------------------------------------- # This is a consolidation of the above and it FAILS. set _dest to alias ((path to downloads folder as text) & "dest:")
tell application "Finder" set fileList to (files 1 thru 10 of (get insertion location)) as alias list repeat with i in fileList set (contents of i) to POSIX file (POSIX path of (contents of i)) end repeat duplicate fileList to _dest end tell -------------------------------------------------------------------------------------------
So to avoid complications and confusions I stick to my rule.
-- Best Regards, Chris
|