我正在尝试制作一个显示文本对话的 applescript 脚本,我可以在 URL 中输入它并在我的默认浏览器中打开它。

I'm trying to make an applescript script that displays a text dialogue which I can enter in a URL and it opens it in my default browser.

我正在尝试制作一个显示文本对话的 applescript 脚本,我可以在 URL 中输入它并在我的默认浏览器中打开它。另外,我正在努力使 url 之前有一个 "http://",这样我就可以键入 www.website.com。这是我到目前为止的代码。

display dialog "Website name" default answer ""
set x to text returned
open location "http:// x"
set dialogresult to display dialog "Website name" default answer ""
set x to text returned of dialogresult
open location "http://" & x

以后尽量让你的问题更具体,即为什么

set x to "xyz"
log "http:// x"

不显示"http:// xyz"?

但您可能仍会收到一些负面反馈。

假设你有

open location "http://www.excersise.com/x"

(只是为了争论),你怎么能指望计算机知道一个 x 代表一个变量而另一个不是?

如果您还有其他问题,请尽管提问。

我假设您希望 Safari 成为默认浏览器。如果您想要不同的浏览器,只需将 "Safari" 更改为浏览器应用程序的名称即可; "Chrome" 例如。您快完成了,只需要修复一些 coding/scripting。所以这是您要查找的脚本。 :)

display dialog "Website name" default answer ""
set x to the text returned of the result
tell application "Safari"
open location "http://" & x
end tell

您需要将 "http://" 与变量 x 分开,否则 AppleScript 将无法区分。通过不在 x 周围放置“”,它告诉 AS 那是一个变量而不是常规文本。并添加 & 告诉 AS 向其添加另一条信息。这在处理像本例中的变量和对话框时很有用。您还必须输入:

set x to the text returned of the result

不是:

set x to text returned

这正是 AS 希望您表达的方式。希望这有很大帮助。抱歉迟到了 1 个月...

如果您不想针对 Safari 或 Chrome 等特定浏览器,而只想在系统默认浏览器中打开 URL,请让 Finder 打开位置:

tell application "Finder"
    open location "http://google.com/"
end tell

... 因为它会将 URL 传递给系统默认浏览器而不是自己打开它。