如何在没有cURL的情况下将变量设置为AppleScript中的网站源?

时间:2014-04-07 01:14:40

标签: curl applescript

我需要在浏览器中加载网页,并将源指定给AppleScript中的变量。

我过去曾使用cURL来完成此任务,但这对于这个特定的脚本是不可接受的。如果没有cURL,请帮我完成同样的工作。

以下是cURL的工作示例:

set myIP to (do shell script "curl -sL http://icanhazip.com")

如果没有cURL,建议如何做同样的事情?

1 个答案:

答案 0 :(得分:1)

这是基于Safari 7.0.3的解决方案。 同步加载URL - 访问source属性的先决条件 - 令人惊讶地痛苦 - openUrlSync()中的评论告诉了可悲的故事。

# Specify URL and loading behavior:
set targetUrl to "http://example.com/"
set asNewWin to true
set activateNew to true

# Load the URL *synchronously* in Safari, using the helper
# function below.
set newTab to my openUrlSync(targetUrl, asNewWin, activateNew)

# Now that we know that the page has finished loading,
# we can access its `source` property to obtain the HTML
# source.
tell application "Safari" to set pageSource to source of newTab

# ---- HELPER FUNCTION
# SYNOPSIS
#   my openUrlSync(targetUrl, asNewWin, activateNew)
# DESCRIPTION
#   Opens a URL in Safari *synchronously* and returns the new tab.
#   CAVEAT: To be safe, access the properties of the returned tab object in the Safari context.
#     targetUrl ... the URL to open
#     asNewWin ... true: open the URL in a new window; false: open it in a new tab in the front window, at the end
#     activateNew ... true: activate the new window/tab; false: do NOT activate the new window/tab;
#        Note: Safari itself is NEVER activated.
# COMMENTS
#   Loading synchronously allows safe access to the properties of the tab returned, notably the `source`
#   property.
#   Tested on Safari 7.0.3.
#  CAVEATS: 
#    - Safari can get into states where this function HANGS.
#    - Creating a new window without activating it makes the interactive
#      window management behave oddly; e.g., `File > Close All Windows`
#      doesn't include the windows created here.
# EXAMPLE:
#   set newTab to my openUrlSync("http://example.com", true, true)
#   tell application "Safari" to set pageSource to source of newTab
on openUrlSync(targetUrl, asNewWin, activateNew)

    tell application "Safari"

        local newTab

        if asNewWin or not (exists front window) then # Make new window.
            # !! The document returned by `make new document` is NOT directly   usable - 
            # !! it seems to change its identity as the page is being loaded.
            # Note: This DOES make the new window the active one in Safari.
            # !! Trying to keep the window invisible with `{URL:targetUrl, visible: false}` is IGNORED.
            make new document at end of documents with properties {URL:targetUrl}
            # !! The following does not make the window INVISIBLE, but it places it LAST among
            # !! the open windows.
            if not activateNew then set visible of front window to false
            # !! We CAN get a persistent reference to the new tab, but only via `current tab` and `front window`.
            set newTab to current tab of front window
        else # Make new tab in front window (at the end).
            # Note: This does NOT activate the new tab.
            # !! Trying to keep the tab invisible with `{URL:targetUrl, visible: false}` BREAKS the statement.
            # !! Ditto for a separate `set visible of newTab to false` statement.
            tell front window to set newTab to make new tab with properties {URL:targetUrl}
            if activateNew then set current tab of front window to newTab
        end if

        # Wait until the page has finished loading.
        # !! Checking for `"complete" == (do JavaScript "document.readyState")` 
        # !! does NOT work as expected, as properties such as `source` are NOT yet available
        # !! at that time. 
        # !! Seemingly, the page is only fully loaded (from an AppleScript perspective),
        # !! once the `URL` property no longer contains `missing value`.
        tell newTab
            repeat while not (exists URL)
                delay 0.5
            end repeat
        end tell

        return newTab

    end tell

end openUrlSync