从 HFS 字符串创建文件对象

发布于 2025-01-17 01:05:43 字数 2054 浏览 1 评论 0原文

我正在从各种在线文章和教程中学习 AppleScript,并且对整个文件和别名主题有了很大的了解。

根据这个指南,(清单15.8 )我应该能够使用行

set theFile to file "Macintosh HD:Users:yourUserName:Desktop:My File.txt"

这会引发错误

脚本编辑器错误

我已经做了很多谷歌搜索,只发现对该问题的一个参考

奇怪的是,您无法将文件对象说明符分配给变量,或将其作为值返回。如果您尝试,您会收到运行时错误消息:

将 x 设置为文件“feathers:Users:mattneub:”
-- 错误:无法将文件“feathers:Users:mattneub:”制作为类型引用

相反,您必须生成对文件对象的引用,如下所示:

将 x 设置为对文件“feathers:Users:mattneub:”的引用
x——文件 «script» 的“feathers:Users:mattneub:”

AppleScript 语言指南 file 对象是通过伪类 POSIX file 创建的

处理文件
AppleScript 使用文件对象来表示文件 脚本。文件对象可以存储在变量中并在整个过程中使用 一个脚本。以下脚本首先创建一个文件对象 变量notesFile中的现有文件,然后在a中使用该变量 打开文件的tell语句:

将notesFile设置为POSIX文件“/Users/myUser/Feb_Meeting_Notes.rtf”
告诉应用程序“TextEdit”打开notesFile

我觉得这很奇怪!

据我了解,文件对象内的文件路径存储为 HFS,因此如果我想从 HFS 字符串创建文件对象,我需要

  • 从 HFS 字符串创建 POSIX 路径
  • ,将 POSIX 路径放入

返回的 POSIX 文件中一个文件对象,其路径再次存储为 HFS 字符串!

(虽然显然 POSIX 文件甚至不输出文件对象,但输出文件 URL?!)

简而言之,我的问题是:为什么不能通过将文件附加到 HFS 字符串来将变量设置为文件对象我是否总是必须通过 POSIX 路径和 POSIX 文件?

我想知道代码是否确实有效,只是不能单独工作?我实际上还没有弄清楚为什么我需要或如何使用文件对象,所以我的理解可能是不完整的......

在关于 POSIX 和 HFS 路径之间转换的教程中,以及别名,我决定尝试将别名转换为文件对象并打开这罐蠕虫。

非常感谢任何帮助,即使答案是:您永远不需要这样做,所以忽略它!

I'm learning AppleScript from various online articles and yt tutorials and I've come a cropper over the whole Files and Alias topic.

According to this guide, (listing 15.8) I should be able to create a file object with the line

set theFile to file "Macintosh HD:Users:yourUserName:Desktop:My File.txt"

This throws up an error

Script Editor error

I've done a lot of googling and only found one reference to the issue:

Oddly, you can't assign a file object specifier to a variable, or return it as a value. If you try, you get a runtime error message:

set x to file "feathers:Users:mattneub:"
-- error: Can't make file "feathers:Users:mattneub:" into type reference

Instead, you must generate a reference to the file object, like this:

set x to a reference to file "feathers:Users:mattneub:"
x -- file
"feathers:Users:mattneub:" of «script»

In the AppleScript Language Guide a file object is created via the pseudo class POSIX file

Working With Files
AppleScript uses file objects to represent files in
scripts. A file object can be stored in a variable and used throughout
a script. The following script first creates a file object for an
existing file in the variable notesFile, then uses the variable in a
tell statement that opens the file:

set notesFile to POSIX file "/Users/myUser/Feb_Meeting_Notes.rtf"
tell application "TextEdit" to open notesFile

I find this bizarre!

As I understand it, file paths inside the file object are stored as HFS, so should I want to create a file object from an HFS string, I need to

  • create a POSIX path from the HFS string
  • put the POSIX path through POSIX file

which returns a file object with the path stored as an HFS string again!

(Although apparently POSIX file doesn't even output a file object, but a file URL?!)

My question in short: Why can't I set a variable as a file object by appending file to an HFS string as in the listing 15.8 quoted above? Do I always have to go via POSIX path and POSIX file?.

I'm wondering if the code does actually work, just not on its own? I haven't actually got as far as to work out why I need or how to use file objects, so my understanding could be patchy...

In a tutorial about converting between POSIX and HFS paths, and aliases, I decided to try and convert an alias to a file object and opened this can of worms.

Any help is much appreciated, even if the answer is: you'd never need to do this, so just ignore it! ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热风软妹 2025-01-24 01:05:43

这里你不明白手册解释的是什么。清单 15.8 显示了使用旧类型文件说明符(文件)创建文件引用的错误尝试。此说明符是过去遗留下来的,用于与 AppleScript 命令(例如 readwrite)配合使用。

该手册清楚地显示了从 HFS 路径正确创建旧类型文件引用(文件):

set theFile to a reference to file "Macintosh HD:Users:yourUserName:Desktop:My File.txt"

稍后,您可以随时通过 AppleScript 命令 read使用此引用 >写。例如:

read theFile

此外,手册还明确指出,还可以使用读取写入命令,而无需先创建单独的文件引用(变量 theFile 在你的情况下)。可以这么说,“即时”阅读或写作:

read file "Macintosh HD:Users:yourUserName:Desktop:My File.txt"

Here you do not understand what the manuals explain. Listing 15.8 shows exactly the wrong attempt to create a file reference using old type file specifier (file). This specifier is left over from the past to work with AppleScript commands such as read and write.

The manual clearly shows the correct creation of the old type file reference (file) from HFS path:

set theFile to a reference to file "Macintosh HD:Users:yourUserName:Desktop:My File.txt"

Later, you can use this reference any time with AppleScript commands read and write. For example:

read theFile

Also, the manual clearly states that it is additionally possible to use the read or write command without first creating a separate file reference (variable theFile in your case). So to speak, to read or write "on the fly":

read file "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
虫児飞 2025-01-24 01:05:43

简短版本:AppleScript 是一个绝对的垃圾箱文件。

更长的版本:AppleScript 重载对象说明符来执行 2 个截然不同的任务:

  1. 组装识别应用程序中数据的复杂查询,例如(在 Finder 中)主文件夹“Downloads”的文件“bar”,< /p>

  2. 创建原始AppleScript类型的新实例,例如别名“Macintosh” HD:用户:foo:下载:bar:".

在 OS X 之前,file STRING 说明符是文件规范 STRING 的语法简写,它创建了 AS 的文件规范 类型的新实例 (#2 )。由于其技术限制,该类型在 OS X 中已被弃用并删除,但无论出于何种原因,构造该类型的 file STRING 说明符都保留在原处并被保留,以便至少某些应用程序会接受生成的结果查询 (#1) 作为原始文件类型(别名POSIX 文件)的替代方案。

因此,如果您现在编写 [tell APPLICATION to] open file STRING,该语法[通常]会起作用,因为 AS 将 file STRING 查询按原样传递给应用程序的 open 命令来处理。然而,如果您将 set f to file STRING 写入,则会失败,因为 AS 不再知道如何为自身解析 file STRING [of AppleScript] 查询。

同样,这一切都非常令人困惑,如果 Mac 自动化团队在 15 年前在 AppleScript 2.0 中正确实现了 POSIX 文件,并将所有人从那些不可靠的 HFS 路径中迁移出来,那么整个混乱局面就可以完全避免。但如果通往地狱的道路是善意铺成的,那么 AppleScript 语法就是沿途的杀戮。

根据经验,如果您可以使用 POSIX 文件类型并完全避免遗留的 HFS 混乱,您将为自己省去很多麻烦:

set f to "/Users/foo/Downloads/bar/" as POSIX file -- convert string to file type

set p to POSIX path of f -- convert file to string type

注意强制的使用(STRING 作为 POSIX 文件)而不是对象说明符(POSIX 文件 STRING);强制是两者中麻烦程度较小的一个。

是的,POSIX 文件 无法正常工作,并且一些特别古老的 Mac 应用程序仍然坚持使用 HFS 路径/类型,即使 HFS 已被弃用 15 年以上。但是 AppleScript 已经到了它的生命周期的尽头,而负责它的 Mac 自动化团队也早已解散,所以你只需要在你还拥有它的时候充分利用你所拥有的垃圾。

TL;DR:您可以明白为什么我将简短版本放在顶部。

Short version: AppleScript is an absolute dumpster file.

Longer version: AppleScript overloads object specifiers to perform 2 very different tasks:

  1. assemble complex queries identifying data in apps, e.g. (in Finder) file "bar" of folder "Downloads" of home,

  2. create new instances of primitive AppleScript types, e.g. alias "Macintosh HD:Users:foo:Downloads:bar:".

Pre OS X, the file STRING specifier was syntactic shorthand for file specification STRING, which created a new instance of AS’s file specification type (#2). That type was deprecated and removed in OS X due to its technical limitations, but for whatever reason the file STRING specifier that constructed it was left in-place and bodged so that at least some apps will accept the resulting query (#1) as alternative to a primitive file type (alias, POSIX file).

Thus if you now write [tell APPLICATION to] open file STRING, that syntax [usually] works because AS passes the file STRING query as-is to the app’s open command to process. Whereas if you write set f to file STRING, that fails because AS no longer knows how to resolve a file STRING [of AppleScript] query for itself.

Again, it’s all horrendously confusing, and the whole mess could’ve been entirely avoided had the Mac Automation team implemented POSIX file properly and migrated everyone off those unreliable HFS paths 15 years ago in AppleScript 2.0. But if the road to Hell is paved with good intentions then AppleScript syntax is the roadkill strewn all along it.

As a rule of thumb, if you can use the POSIX file type and avoid the legacy HFS mess entirely, you will save yourself a lot of pain all round:

set f to "/Users/foo/Downloads/bar/" as POSIX file -- convert string to file type

set p to POSIX path of f -- convert file to string type

Note the use of a coercion (STRING as POSIX file) rather than an object specifier (POSIX file STRING); the coercion is the less troublesome of the two.

And yes, POSIX file doesn’t work properly, and some especially ancient Mac apps still insist on HFS paths/types even though HFS has been deprecated for 15+ years. But AppleScript’s at the end of its lifecycle and the Mac Automation team responsible for it long disbanded, so you just have to make the best out of the crap you’ve got while you’ve still got it.

TL;DR: You can see why I put the short version at the top.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文