Applescript 基于文件名选择POSIX文件

Applescript 基于文件名选择POSIX文件,applescript,Applescript,在开始字符的基础上尝试选择此文件时,访问此文件时遇到问题 set location to "/Users/myuser/Desktop/" set bom to POSIX file (location & (first file of location whose name begins with "thisFile")) tell application "Preview" to open bom 是路径/别名还是文本类型?只有系统事件和查找程序知道文件系统中的文件是什么。 Fin

在开始字符的基础上尝试选择此文件时,访问此文件时遇到问题

set location to "/Users/myuser/Desktop/"
set bom to POSIX file (location & (first file of location whose name begins with "thisFile"))
tell application "Preview" to open bom

是路径/别名还是文本类型?

只有
系统事件
查找程序
知道文件系统中的文件是什么。
Finder有一个属性
desktop
,它始终指向当前用户的桌面

tell application "Finder" to set bom to first file of desktop whose name begins with "thisFile"
tell application "Preview" to open (bom as alias)
或者使用任意POSIX路径

set location to POSIX file "/Users/myuser/Desktop" as text
tell application "Finder" to set bom to first file of folder location whose name begins with "thisFile"
tell application "Preview" to open (bom as alias)
需要使用
别名
强制,因为
预览
无法识别查找器文件说明符对象。

效果良好,但值得一提的是:

  • 即使在默认上下文、系统事件和查找程序的上下文之外,您也可以访问知名文件夹;e、 g:

    • 桌面路径
    • 主文件夹的路径
    • 例如,使用(主文件夹的路径)获取POSIX路径
  • 出于性能和可预测性的考虑,使用上下文
    系统事件通常比使用
    查找程序
    上下文更可取。

对于任意目标文件夹,使用POSIX路径:

告诉应用程序“系统事件”
将targetFolder设置为别名“/Users/jdoe/Desktop”
#等效于:将targetFolder设置为(桌面路径)
将targetFile设置为targetFolder的第一个文件,其名称以“thisFile”开头
结束语
告诉应用程序“预览”打开targetFile

或者,如果您知道如何使用shell,您可以尝试:

将targetFilePosixPath设置为执行shell脚本“fls=(~/Desktop/*.pdf);printf%s\“$fls”
告诉应用程序“预览”打开(POSIX文件targetFilePosixPath作为别名)

我确实需要位置变量,因为桌面只是我的临时测试位置。。。