当我尝试检索桌面上文件的路径时,我得到:
/Users/MyName/Desktop/file.txt但我需要完整的路径(以提供bash脚本):
/Volumes/MyDrive/Users/MyName/Desktop/file.txt我花了相当多的时间寻找解决方案,但没有找到任何解决方案。我对任何可以通过Automator、bash或applescript运行的东西都持开放态度。
这让我抓狂,因为如果我简单地将我的file.txt放在Coda中,它将立即输出我想要的完整的绝对路径,而终端不会。
有什么想法吗?
发布于 2014-11-09 06:28:08
所以你需要找出/Volumes下的哪个卷等同于/,对吗?这很简单-只需查找/的符号链接即可。
for vol in /Volumes/*; do
if [ "$(readlink "$vol")" = / ]; then
root_vol=$vol
fi
done 然后你可以这样做
echo "$root_vol$HOME/Desktop/file.txt"或者别的什么。
发布于 2014-11-10 02:40:30
对于任何可能需要它的人:如果您正在运行OSX Automator工作流/应用程序/服务,并且需要输入文件的绝对路径,只需添加一个"Run shell script“操作,将输入设置为"as arguments”并使用以下命令:
#!/bin/bash
## Check if the input is an absolute path including the volume name
if [[ "$1" = /Volumes/* ]]
## If TRUE, the path is already correct
then
echo "$1"
## If FALSE, needs fixing
else
## Resolve the volume name
## by looping through all the volumes to find the one symlinked to "/"
for vol in /Volumes/*
do
if [ "$(readlink "$vol")" = / ]
then root_vol=$vol
fi
done
## Pass the path back to Automator for further use
echo "$root_vol$1"
fihttps://stackoverflow.com/questions/26634800
复制相似问题