在 applescript 和 shell 脚本之间传递参数
passing parameters between applescript and shell script
我需要使用 Apple 选择文件对话框来 select 一个要在 bash 脚本中使用的文件。我相信做到这一点的唯一方法是在 AppleScript 中。我想从 bash 中调用 AppleScript,并将 return selected 文件的位置作为要在 shell 脚本中使用的变量。到目前为止我有:
osascript <<EOF
tell Application "Finder"
set strPath to "/my/default/location/"
set thePDF to file (choose file with prompt "Choose a PDF: " of type { " com.adobe.pdf" , "dyn.agk8ywvdegy" } without invisibles default location strPath) as alias
set PDFName to name of file thePDF
end tell
EOF
我现在如何将 PDF 的位置 - AppleScript 变量 PDFName
- 传递回 Shell?
您可以将 osascript 中生成的文本发送到标准输出并捕获它,例如,在变量中。像这样:
#!/bin/bash
PDFNAME=$( osascript <<EOF
tell Application "Finder"
set strPath to "/your/pdf/path/"
set thePDF to file (choose file with prompt "Choose a PDF: " without invisibles default location strPath) as alias
set PDFName to name of file thePDF
end tell
copy PDFName to stdout
EOF )
echo "From bash: $PDFNAME"
这里,整个 osascript 位被执行为 "command substitution"(参见 bash 手册页),其中 $( ... ) 之间的表达式被执行结果替换表达式。
这里的关键当然是上面的 AppleScript 行 "copy ... to stdout"。
或者,您可以通过
将 osascript 的输出通过管道传输到下一个命令
osascript <<EOF
(your code here)
copy output to stdout
EOF | next_command
这是您的脚本的修改版本:
thePDF=$(osascript <<EOF
set strPath to "/my/default/location/"
set thePDF to (choose file with prompt ("Choose a PDF: ") ¬
of type {"com.adobe.pdf", "dyn.agk8ywvdegy"} ¬
default location strPath ¬
without invisibles)
set PDFName to the POSIX path of thePDF
EOF
)
需要注意的变化是:
- 删除不必要的
tell application
...end tell
语句;
- 因此删除
file
对象说明符和对 alias
的强制转换,作为 choose file
命令 returns 默认情况下一个文件 alias
对象;
- 消除
" com.adobe.pdf"
中的 space 以允许选择 PDF 文件;
- 将 AppleScript 代码的倒数第二行更改为:
set PDFName to the POSIX path of thePDF
;
- 使用
thePDF=$(...)
. 将 osascript
的输出分配给 bash 变量
osascript
returns 文件的完整 posix 路径,例如/Users/CK/Documents/somefile.pdf
,现在分配给 bash 变量 $thePDF
。
如果您碰巧收到关于 /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit
的警告,可以通过进行以下小修改来忽略和消除警告:osascript 2>/dev/null <<EOF
.
我需要使用 Apple 选择文件对话框来 select 一个要在 bash 脚本中使用的文件。我相信做到这一点的唯一方法是在 AppleScript 中。我想从 bash 中调用 AppleScript,并将 return selected 文件的位置作为要在 shell 脚本中使用的变量。到目前为止我有:
osascript <<EOF
tell Application "Finder"
set strPath to "/my/default/location/"
set thePDF to file (choose file with prompt "Choose a PDF: " of type { " com.adobe.pdf" , "dyn.agk8ywvdegy" } without invisibles default location strPath) as alias
set PDFName to name of file thePDF
end tell
EOF
我现在如何将 PDF 的位置 - AppleScript 变量 PDFName
- 传递回 Shell?
您可以将 osascript 中生成的文本发送到标准输出并捕获它,例如,在变量中。像这样:
#!/bin/bash
PDFNAME=$( osascript <<EOF
tell Application "Finder"
set strPath to "/your/pdf/path/"
set thePDF to file (choose file with prompt "Choose a PDF: " without invisibles default location strPath) as alias
set PDFName to name of file thePDF
end tell
copy PDFName to stdout
EOF )
echo "From bash: $PDFNAME"
这里,整个 osascript 位被执行为 "command substitution"(参见 bash 手册页),其中 $( ... ) 之间的表达式被执行结果替换表达式。
这里的关键当然是上面的 AppleScript 行 "copy ... to stdout"。
或者,您可以通过
将 osascript 的输出通过管道传输到下一个命令osascript <<EOF
(your code here)
copy output to stdout
EOF | next_command
这是您的脚本的修改版本:
thePDF=$(osascript <<EOF
set strPath to "/my/default/location/"
set thePDF to (choose file with prompt ("Choose a PDF: ") ¬
of type {"com.adobe.pdf", "dyn.agk8ywvdegy"} ¬
default location strPath ¬
without invisibles)
set PDFName to the POSIX path of thePDF
EOF
)
需要注意的变化是:
- 删除不必要的
tell application
...end tell
语句; - 因此删除
file
对象说明符和对alias
的强制转换,作为choose file
命令 returns 默认情况下一个文件alias
对象; - 消除
" com.adobe.pdf"
中的 space 以允许选择 PDF 文件; - 将 AppleScript 代码的倒数第二行更改为:
set PDFName to the POSIX path of thePDF
; - 使用
thePDF=$(...)
. 将
osascript
的输出分配给 bash 变量
osascript
returns 文件的完整 posix 路径,例如/Users/CK/Documents/somefile.pdf
,现在分配给 bash 变量 $thePDF
。
如果您碰巧收到关于 /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit
的警告,可以通过进行以下小修改来忽略和消除警告:osascript 2>/dev/null <<EOF
.