【问题标题】:Open a new tab in a private window in Safari using applescript使用 applescript 在 Safari 的私人窗口中打开一个新选项卡
【发布时间】:2021-01-13 03:02:51
【问题描述】:

实际上,我为 Alfred 开发了一个工作流程。它假设您搜索的任何内容都可以在 Safari 的新私人窗口中打开,并且它按预期工作.. 但问题是,它会在新的私人窗口中打开每个搜索,我希望第一次搜索打开一个新的私人窗口,然后从下一个开始,只是为了在该窗口中打开新标签。希望我足够清楚,我有点新,我会尽力学习,所以非常欢迎任何建议。这是我到目前为止得到的:

tell application "System Events" to ¬
       click menu item "New Private Window" of ¬
        menu "File" of menu bar 1 of ¬
        application process "Safari"
tell application "Safari" to ¬
            set URL of current tab of ¬
        front window to "https://duckduckgo.com/?q={query}&t=h_&ia=web"


tell application "System Events" to ¬
       click menu item "New Tab" of ¬
        menu "File" of menu bar 1 of ¬
        application process "Safari"
tell application "Safari" to ¬
            set URL of current tab of ¬
        front window to "https://duckduckgo.com/?q={query}&t=h_&ia=web"

P.S 代码的第 2 部分只是我的草稿,从技术上讲,该部分在窗口内打开了一个新选项卡,但只是初始搜索的副本。

【问题讨论】:

    标签: safari applescript workflow alfred


    【解决方案1】:

    将私有窗口存储在一个变量中,以便以后可以访问它:

    property privyWindow : missing value
    
    tell application "Safari"
        -- a set of tests to make sure the window is present and valid
        set noWindow to (privyWindow is missing value) or not (exists privyWindow) or (current tab of privyWindow is missing value)
    end tell
    if noWindow is true then
        -- make new private window, and get its first tab
        tell application "System Events"
            tell process "Safari"
                click menu item "New Private Window" of menu "File" of menu bar 1
            end tell
        end tell
        tell application "Safari"
            set privyWindow to first window
            set activeTab to first tab of privyWindow
        end tell
    else
        -- retrieve stored window and add a new tab to it
        tell application "Safari"
            set activeTab to make new tab in privyWindow
        end tell
    end if
    
    -- open the url in the specified tab
    tell application "Safari"
        set URL of activeTab to "https://duckduckgo.com/?q={query}&t=h_&ia=web"
    end tell
    

    【讨论】:

    • 那么当私人窗口已经关闭并且脚本再次运行时,你将如何处理呢?按照编码和表达的条件,它会出错,例如:Safari 出错:无法获取窗口 id 5226。
    • @user3439894:啊,没考虑过这个问题。在答案中修改了代码。
    • 您好泰德,非常感谢您的帮助。但不幸的是,它仍然每次都会在一个新的私人窗口中打开。
    • @BogdanDuduia:property 变量将在您每次重新编译脚本时自行重置(或者有时如果您正在执行其他操作,例如通过 osascript 或从应用程序运行此脚本)。如果它每次都打开一个窗口,那意味着您正在做一些您在问题中没有指定的另类事情。我只能测试你告诉我的内容。
    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 2014-02-02
    相关资源
    最近更新 更多