这个 Bash 脚本包含了一些 AppleScript,以便打开一个浏览器窗口,其中包含脚本可以跟踪的引用并继续以正在进行的 URL 请求为目标。
您应该能够将其复制粘贴到文本编辑器中并将其保存为您希望调用此替换open
函数的任何内容。我将其另存为url
, 在我的$PATH
变量中列出的目录之一中。这样,我可以简单地url dropbox.com
从命令行键入,它就会运行。
您必须先使其可执行,然后才能执行此操作。因此,保存后,运行以下命令:
chmod +x /path/to/file
那么你应该很高兴。如果您遇到任何错误,请告诉我,我会修复它们。
#!/bin/bash
#
# Usage: url %file% | %url%
#
# %file%: relative or absolute POSIX path to a local .html file
# %url%: [http[s]://]domain[/path/etc...]
IFS=''
# Determine whether argument is file or web address
[[ -f "$1" ]] && \
_URL=file://$( cd "$( dirname "$1" )"; pwd )/$( basename "$1" ) || \
{ [[ $1 == http* ]] && _URL=$1 || _URL=http://$1; };
# Read the value on the last line of this script
_W=$( tail -n 1 "$0" )
# Open a Safari window and store its AppleScript object id reference
_W=$( osascript \
-e "use S : app \"safari\"" \
-e "try" \
-e " set S's window id $_W's document's url to \"$_URL\"" \
-e " return $_W" \
-e "on error" \
-e " S's (make new document with properties {url:\"$_URL\"})" \
-e " return id of S's front window" \
-e "end try" )
_THIS=$( sed \$d "$0" ) # All but the last line of this script
echo "$_THIS" > "$0" # Overwrite this file
echo -n "$_W" >> "$0" # Appened the object id value as final line
exit
2934