根据文件名选择POSIX个文件
Selecting POSIX file based on file name
我在尝试根据开头字符 select 访问此文件时遇到问题...
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
它是path/alias 还是文本类型的东西?
只有System Events
和Finder
知道文件系统中的文件是什么。
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)
需要 alias
强制转换,因为 Preview
无法识别 Finder 文件说明符对象。
效果很好,但值得一提的是:
您甚至可以在 默认 上下文、 中访问知名文件夹在 System Events
和 Finder
的上下文之外;例如:
path to desktop
path to home folder
- 例如,使用
POSIX path of (path to home folder)
获取 POSIX 路径。
使用上下文 System Events
通常优于 Finder
上下文,因为性能和可预测性。
对于任意目标文件夹,使用 POSIX 路径:
tell application "System Events"
set targetFolder to alias "/Users/jdoe/Desktop"
# equivalent of: set targetFolder to (path to desktop)
set targetFile to first file of targetFolder whose name starts with "thisFile"
end tell
tell application "Preview" to open targetFile
或者,如果您熟悉 shell,您可以尝试:
set targetFilePosixPath to do shell script "fls=(~/Desktop/*.pdf); printf %s \"$fls\""
tell application "Preview" to open (POSIX file targetFilePosixPath as alias)
我在尝试根据开头字符 select 访问此文件时遇到问题...
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
它是path/alias 还是文本类型的东西?
只有System Events
和Finder
知道文件系统中的文件是什么。
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)
需要 alias
强制转换,因为 Preview
无法识别 Finder 文件说明符对象。
您甚至可以在 默认 上下文、 中访问知名文件夹在
System Events
和Finder
的上下文之外;例如:path to desktop
path to home folder
- 例如,使用
POSIX path of (path to home folder)
获取 POSIX 路径。
使用上下文
System Events
通常优于Finder
上下文,因为性能和可预测性。
对于任意目标文件夹,使用 POSIX 路径:
tell application "System Events"
set targetFolder to alias "/Users/jdoe/Desktop"
# equivalent of: set targetFolder to (path to desktop)
set targetFile to first file of targetFolder whose name starts with "thisFile"
end tell
tell application "Preview" to open targetFile
或者,如果您熟悉 shell,您可以尝试:
set targetFilePosixPath to do shell script "fls=(~/Desktop/*.pdf); printf %s \"$fls\""
tell application "Preview" to open (POSIX file targetFilePosixPath as alias)