macos - 有没有办法在 AppleScript 中获取文件的完整路径?
问题描述
使用 POSIX 路径会返回一个如下所示的字符串:
“/卷/父文件夹/File.fl”
但我需要一个看起来像这样的完整路径:
“smb://something.domain.com/父文件夹/File.fl”
有没有办法在applescript中做到这一点?我需要能够获取网络源或已安装卷的信息,以便我可以生成 URL 供人们单击。
解决方案
由于您没有在我的评论中提供命令的输出,因此这里有一个示例,您应该会发现它很有用:
这实际上是从macOS High Sierra到macOS Catalina上的SMB 共享进行测试的,并且是macOS High Sierra文件的实际情况。所以未注释的命令实际上在现实世界的测试中起作用。posixFileName
POSIX path
do shell script
-- # The value of posixFileName is statically assigned for demonstration purposes.
-- # It's assumed it will be assigned dynamically in the actually working script.
set posixFileName to "/Volumes/Temp Folder/Some Stuff/foobar.txt"
-- # Use AppleScript's text item delimiters to parse posixFileName
-- # to posixParentFolderName and posixChildFolderAndOrFileName.
-- #
-- # E.g. posixParentFolderName of posixFileName is: '/Volumes/Temp Folder'
-- # E.g. posixChildFolderAndOrFileName of posixFileName is: 'Some Stuff/foobar.txt'
-- #
set AppleScript's text item delimiters to "/"
set posixParentFolderName to text items 1 thru 3 of posixFileName as string
set posixChildFolderAndOrFileName to text items 4 thru -1 of posixFileName as string
set AppleScript's text item delimiters to ""
-- # Uncomment the next line of code for the working script, but use the
-- # sample further below just to test the example posixFileName above.
-- set mountOutput to do shell script "mount | grep '" & posixParentFolderName & "'; exit 0"
-- # This is just sample output for demonstration purposes and not used when
-- # the above line is uncommented and then this line of code gets removed.
set mountOutput to "//me@192.168.2.103/Temp%20Folder on /Volumes/Temp Folder (smbfs, nodev, nosuid, mounted by me)"
if mountOutput is not equal to "" then
-- # Use AppleScript's text item delimiters to parse mountOutput
-- # to get e.g. '192.168.2.103/Temp%20Folder'
set AppleScript's text item delimiters to " on "
set serverPathname to first text item of mountOutput
set AppleScript's text item delimiters to "@"
set serverPathname to second text item of serverPathname
set AppleScript's text item delimiters to ""
-- # Combine elements to get the SMB URL.
set smbPathname to "smb://" & serverPathname & "/" & posixChildFolderAndOrFileName
-- # Replace any spaces ' ' with '%20'.
set AppleScript's text item delimiters to " "
set smbPathname to text items of smbPathname
set AppleScript's text item delimiters to "%20"
set smbPathname to smbPathname as string
set AppleScript's text item delimiters to ""
return smbPathname
-- # Result: "smb://192.168.2.103/Temp%20Folder/Some%20Stuff/foobar.txt"
else
return
end if
推荐阅读
- javascript - 尽管添加了课程,但动画不起作用
- c# - Entity Framework Core 中 TimeSpan 的总和
- java - 声明一个静态 Java lambda 以便重用更有效吗?
- reactjs - 使用 Inertia 从 URL 获取数据
- java - Alfresco 6.2 社区无法启动
- angular - 为什么我将 ng-template 放入手风琴选项卡中获得这种奇怪的渲染行为?
- python - 使用包含“:”的用户配置 sqlalchemy 引擎
- css - 如何在反应中在同一行显示项目?
- javascript - 如何将js中的jquery ajax函数发送到节点js文件
- docker - Snakemake --use-singularity 等待下载图片