1

我正在使用一个调用这两行代码的 shell 脚本:

iname=$(ls -d -1 $PWD/*jpg) 
osascript -e 'tell app "Finder" to set desktop picture to POSIX file \"$iname\"'

其中 iname 是一个变量,它是我想要的图片的绝对路径。根据我的阅读,这是将变量传递给 osascripts 的方法。但是在尝试运行这两行时出现此错误

55:56: syntax error: Expected expression, property or key form, etc. but found unknown token. (-2741)

有人可以解释我如何解决这个问题吗?

4

2 回答 2

2

您正在尝试用单引号扩展变量。尝试这个:

osascript -e 'tell app "Finder" to set desktop picture to POSIX file "'"$iname"\"
于 2013-11-18T20:34:13.333 回答
0

您还可以使用显式运行处理程序来获取命令行参数:

osascript -e'on run {a}
    tell app "finder" to set desktop picture to posix file a
end' "$iname"`

如果路径可以是相对路径,则可以使用 GNUreadlink将其转换为绝对路径:

osascript -e'on run {a}
    tell app "finder" to set desktop picture to posix file a
end' "$(greadlink -f "$iname")"

如果您需要将绝对路径列表传递给脚本,您可以执行以下操作:

osascript -e'on run a
    set l to {}
    repeat with f in a
        set end of l to posix file f
    end
    l
end' "$@"

如果您需要将路径列表传递给脚本并将相对路径转换为绝对路径,您可以执行以下操作:

osascript -e'on run {a}
    set l to {}
    repeat with f in (get paragraphs of a)
        set end of l to posix file f
    end
    l
end' "$(greadlink -f "$@")"
于 2015-09-12T10:40:58.213 回答