Re: Finder seems to have trouble with POSIX
Re: Finder seems to have trouble with POSIX
- Subject: Re: Finder seems to have trouble with POSIX
- From: CK via AppleScript-Users <email@hidden>
- Date: Thu, 4 Aug 2022 18:49:26 +0100
Hi Gil,
> Could someone please help me understand what is happening here?
>
> set F1 to "/Users/gil/Desktop/Temp Test File.pdf"
> set PF1 to POSIX file F1
This is pertinent. `POSIX file` can be utilised in two ways: (1) as an object
specifier, which is the role it takes on here, whereby it precedes a path in
order to qualify it as a file reference (the path can be referred to as a name
specifier); (2) as a type class, used to coerce a string class object (namely,
a file path) into a file reference, which would be done like this:
• set PF1 to F1 as POSIX file
> tell application "Finder" to exists file F1 --> false (I expected an error)
Filenames can happily contain slashes. If you create a file on your desktop
with the literal filename "/Users/gil/Desktop/Temp Test File.pdf", this line
above would return `true`. Because Finder only deals with HFS paths (where
directories are delimited by colons), the slash character is treated just like
any other.
> tell application "Finder" to exists POSIX file F1 --> false (I expected
> "true")
This is a consequence of passing the name specifier as an expression rather
than a string literal, meaning that the it must be evaluated at runtime before
the object specifier can resolve it. Resolving isn't done by Finder, however,
as the `POSIX file` class (or pseudo-class, I think it's referred to) belongs
to AppleScript. Therefore, if you were to do this:
• tell application "Finder" to exists my POSIX file F1
that ought to return `true`, as Finder will now be handling a fully evaluated
`file` object reference.
> tell application "Finder" to exists (POSIX file F1) --> false (I expected
> "true")
This is identical to the previous case, parentheses notwithstanding.
> tell application "Finder" to exists PF1 --> true (I expected that!)
Here, `PF1` contains an pre-resolved `file` object reference, as it was created
in AppleScript context prior to its use within the Finder context. The reason
is similar to that outlined above.
As I intimated above, the `POSIX file` class can also be used to perform
coercions on data types (actually, just on strings). A coercion is an
operation that will be undertaken during runtime, and it must generally take
place with a greater priority than other operations in order to yield some
meaningful value that can be further operated upon. Thus, you can do this:
• tell application "Finder" to exists F1 as POSIX file
which is identical to:
• tell application "Finder" to exists (F1 as POSIX file)
which ought to return `true`, because `F1 as POSIX file` is going to be
evaluated first and return a fully-formed `file` reference that `exists` can
operate on without reservations.
This is the syntax I, personally, use the most within a Finder context, as it's
never going to cause unexpected behaviour (outside of things in AppleScript
that occasionally and briefly behave unexpectedly between macOS versions).
Using `POSIX file` as a specifier is prone to slightly more ambiguous intent
(more on the part of the scripter than AppleScript, which will behave
consistently, but our human fallibility is somewhat potentiated by the less
keen aspects of AppleScript's language design).
---
CK
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden