applescript - 在Finder中获取文件的文件路径
问题描述
我想在 Finder 中获取前端文件的文件路径,例如/Users/user/Downloads/file.png
,我对macOS
AppleScript 处理文件路径的方式感到非常困惑,POSIX 带有斜线,而本机带有冒号。我尝试了这两个:
tell application "Finder"
set temp to selection
repeat with i from 1 to length of temp
set the_item to item i of temp
set item_posix to the_item as POSIX file
set the_result to POSIX path of item_posix -- returns only the file's name and extension
return get the path of the_item -- returns an error
end repeat
end tell
我曾经成功过,它是如此令人费解,as file
以至于as alias
我不记得它是如何工作的。
如何在 Finder 中获取前端文件的文件路径?
更新:为了语法,我对单个路径感兴趣,我可以使用循环处理多个路径。
解决方案
在 Finder 中,该selection
属性始终返回Finder 文件说明符列表或空列表。
使用您的语法的最简单方法是强制选择alias list
并从别名获取 POSIX 路径
tell application "Finder"
set temp to selection as alias list
repeat with i from 1 to length of temp
set the_item to item i of temp
set the_result to POSIX path of the_item
return the_result
end repeat
end tell
POSIX file
只需要相反的情况,从 POSIX 路径获取 HFS 路径或别名。
如果您只想要第一项,则selection
不需要循环,但必须检查空列表
tell application "Finder"
set temp to selection as alias list
if temp is not {} then
return POSIX path of item 1
end if
end tell
推荐阅读
- php - get_terms() 顺序不遵循层次结构
- java - OkHTTP 客户端:在重新部署应用程序时获取内存泄漏警告(tomcat)
- java - Micronaut 文档中的 REST 调用示例返回错误
- java - @PreAuthorize 注解不适用于 Spring 中的 JpaRepository
- r - Tensorflow:二进制分类结果全零或全一(网络本身没有训练)
- objective-c - 想要更改标记颜色 HighCharts
- laravel - “函数 App\Http\Controllers\Backend\BlogController::edit() 的参数太少,通过了 0,预期正好 1”
- laravel - Laravel忘记密码功能不起作用
- spring - 如何配置 RabbitMQ(在 Spring Boot 2.x 中)以便手动确认有效
- apache-spark - pyspark(2.4版)group by,总和不起作用