【问题标题】:AppleScript for getting the frontmost url from Safari and open it in Google Chrome IncognitoAppleScript 用于从 Safari 获取最前面的 url 并在 Google Chrome Incognito 中打开它
【发布时间】:2021-03-26 10:00:13
【问题描述】:

我正在尝试编写一个 applescript,它将从 Safari 获取当前最前面的 url,并在 Google Chrome Incognito 中打开相同的 url。

到目前为止,我知道如何从 Safari 获取最前面的 url,但我不确定如何在 Chrome 隐身模式中打开该 url。

MWE

tell application "Safari" to return URL of front document
tell application "Safari" to return name of front document

参考资料:

【问题讨论】:

    标签: applescript


    【解决方案1】:

    以下示例 AppleScript 代码假设Safari打开到一个带有URL 并且系统上存在 Google Chrome,但是,如果存在,将按照您的要求执行。

    注意:不包含错误处理以确保Safari打开到具有URL的页面或者系统上是否存在 Google Chrome。那是你添加的。

    tell application "Safari" to set theURL to the URL of the front document
    
    do shell script "open -na 'Google Chrome' --args -incognito " & theURL's quoted form
    

    代码Safari行也可以写成:

    tell application "Safari" to set theURL to the URL of the current tab of the front window
    

    这是一个 示例,其中包含一些 错误处理,它使用 UI 脚本系统事件 而不是 @ 987654328@ commandGoogle Chrome 中打开隐身窗口

    示例 AppleScript 代码

    if running of application "Safari" then
        
        tell application "Safari"
            if the URL of ¬
                the current tab of ¬
                    the front window ¬
                        does not contain missing value then
                set theURL to ¬
                    the URL of ¬
                        the current tab of ¬
                            the front window
            else
                return
            end if
        end tell
        
        tell application "Google Chrome" to activate
        
        delay 1
        
        tell application "System Events" to ¬
            keystroke "n" using ¬
                {shift down, command down}
        
        delay 1
        
        tell application "Google Chrome" to ¬
            set the URL of ¬
                the active tab of ¬
                    the front window to theURL
        
    end if
    


    更新地址评论:

    ...如果已经打开了一个 incongito 窗口,有没有办法在新标签页中打开新网址?

    示例 AppleScript 代码

    if running of application "Safari" then
        
        tell application "Safari"
            if the URL of ¬
                the current tab of ¬
                    the front window ¬
                        does not contain missing value then
                set theURL to ¬
                    the URL of ¬
                        the current tab of ¬
                            the front window
            else
                return
            end if
        end tell
        
        if running of application "Google Chrome" then
            tell application "Google Chrome"
                if mode of front window is "incognito" then
                    make new tab at end of ¬
                        front window with properties {URL:theURL}
                end if
            end tell
        end if
    
    end if
    


    更新以解决其他 cmets:

    以下示例 AppleScript 代码包含必要的错误处理以适应正常可能的状态场景Google Chrome 关于在 Google 的隐身窗口/标签中打开 Safari 前窗口的当前标签的 URL

    示例 AppleScript 代码

    if running of application "Safari" then
        
        tell application "Safari"
            if exists front window then
                if the URL of ¬
                    the current tab of ¬
                        the front window ¬
                            does not contain missing value then
                    set theURL to ¬
                        the URL of ¬
                            the current tab of ¬
                                the front window
                else
                    return
                end if
            else
                return
            end if
        end tell
        
        if running of application "Google Chrome" then
            
            tell application "Google Chrome"
                set incognitoWindowIDs to ¬
                    the id of every window ¬
                        whose mode is "incognito"
                if incognitoWindowIDs is {} then
                    activate
                    my openNewIncognitoWindow()
                    delay 1
                    set URL of ¬
                        the active tab of ¬
                            the front window to theURL
                else
                    make new tab at end of ¬
                        window id (first item of incognitoWindowIDs) ¬
                            with properties {URL:theURL}
                end if
            end tell
        else
            tell application "Google Chrome"
                activate
                delay 1
                my openNewIncognitoWindow()
                delay 1
                set URL of ¬
                    the active tab of ¬
                        the front window to theURL
            end tell
        end if
        
    end if
    
    on openNewIncognitoWindow()
        tell application "System Events" to ¬
            keystroke "n" using ¬
                {shift down, command down}
    end openNewIncognitoWindow
    

    注意:示例 AppleScript 代码 就是这样,并且没有任何包含的错误处理不包含任何适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statementerror statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5延迟设置得当。

    【讨论】:

    • 这很棒。我有个小问题,每次都会打开一个新的 incog 窗口,有没有办法如果已经打开了一个 incongito 窗口,然后在新标签中打开新的 url?
    • @MilkyWay001,RE:“如果已经打开了一个 incongito 窗口,有没有办法在新标签页中打开新 url?” -- 请参阅更新答案的更新地址评论:部分。
    • 我处理了第二个代码并得到了这个错误:$ osascript a.applescript a.applescript:558:569: execution error: Google Chrome got an error: Can’t get window 1. Invalid index. (-1719)
    • 另外,当正常的谷歌浏览器打开时,代码运行但什么也不做。更新前的代码有效,但每次都会打开新的 chrome。
    • @MilkyWay001,我总是在发布之前测试 example AppleScript code 我发布它,并且写下来它可以工作对我来说,Script Editor 或保存 script 并使用 osascript 执行它没有问题。
    【解决方案2】:

    如果您不介意使用 JavaScript,只需几行代码:

    const chrome = Application('Google Chrome')
    const safari_url = Application('Safari').documents[0].url()
    chrome.Window({ mode: 'incognito' }).make()
    chrome.windows[0].activeTab.url = safari_url
    

    【讨论】:

      猜你喜欢
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多