AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1580130
Accepted
John
John
Asked: 2020-08-24 13:24:57 +0800 CST2020-08-24 13:24:57 +0800 CST 2020-08-24 13:24:57 +0800 CST

AppleScript 使用 Safari 窗口 ID 创建新标签

  • 772

我有一个 AppleScript,它创建一个新的 Safari 文档,然后使用重复循环在该窗口中打开多个 URL。我希望脚本继续使用同一个窗口,即使它不是最前面的窗口。

(问题是如果用户将焦点转移到另一个 Safari 窗口,此脚本将在最前面的窗口中打开 URL,而不是之前创建的窗口。)

如果可能的话,我想通过使用解决这个问题,window id但需要一些关于脚本的帮助。

为此,我创建了以下内容,但我再次认为可能存在问题。我宁愿不使用 id,front window因为用户可能会在不合时宜的时间更改前窗,并且脚本会提取错误的窗口 ID。

tell application "Safari"
    make new document -- after this what if user changes focus
    set win_ID to id of front window of application "Safari"
end tell

我宁愿使用类似的东西

set win_ID to window id of (make new document)

即使使用上述方法,我也无法通过使用它的窗口 ID 在窗口中打开 URL,并且还需要有关该脚本的帮助。

它的功能是这样的:

tell application "Safari"
    open location "https://apple.com" in a new tab in window id xxxx
end tell
safari applescript
  • 2 2 个回答
  • 680 Views

2 个回答

  • Voted
  1. Best Answer
    user3439894
    2020-08-24T15:09:33+08:002020-08-24T15:09:33+08:00

    更新以解决使用以下问题的问题:front window

    以下示例 AppleScript 代码保证URL列表在目标窗口中打开,无论其在windows的z 顺序中的位置如何。

    set myURLs to {¬
        "https://apple.com", ¬
        "https://google.com", ¬
        "https://superuser.com", ¬
        "https://example.com"}
    
    set windowName to random number from 1000000 to 9999999
    set tmpFileName to "/private/tmp/" & windowName & ".html"
    set tmpFileContent to "<html><head><title>" & windowName & "</title></head></html>"
    
    if not my writeToFile(tmpFileContent, tmpFileName, true) then return
    
    tell application "Safari"
        
        make new document with properties {URL:"file://" & tmpFileName}
        set i to 0
        repeat while not (exists (windows whose name is windowName))
            delay 0.1
            set i to i + 1
            if i = 30 then return
        end repeat
        set winID to (id of windows whose name is windowName) as number
        
        make new tab at end of tabs of window id winID with properties {URL:item 1 of myURLs}
        delete first tab of window id winID
        repeat with i from 2 to (length of myURLs)
            make new tab at end of tabs of window id winID with properties {URL:item i of myURLs}
            delay 1
        end repeat
        
    end tell
    
    tell application "System Events" to delete file tmpFileName
    
    
    --  # Handler #
    
    on writeToFile(theData, theFile, overwriteExistingContent)
        try
            set theFile to theFile as string
            if theFile contains "/" then
                set theOpenedFile to open for access theFile with write permission
            else
                set theOpenedFile to open for access file theFile with write permission
            end if
            if overwriteExistingContent is true then set eof of theOpenedFile to 0
            write theData to theOpenedFile starting at eof
            close access theOpenedFile
            return true
        on error
            try
                close access file theFile
            end try
            return false
        end try
    end writeToFile
    

    注意:根据需要滚动查看所有代码。


    笔记:

    • 更新后的AppleScript示例 代码包含一些错误处理,如果未创建tmp 文件(变量的值),则脚本会中止且不显示任何消息。这可以通过将语句转换为完整块并根据需要包括适当的或命令来更改,然后是命令。 tmpFileName if not my writeToFile ... if display alertdisplay dialogdisplay notification return

    • 按照编码,这会--> document "Untitled"从make new document无关项返回,因为使用包含的repeat 循环,它会等待HTML 文件被加载,并且可以通过文档中的标签定义的文档的实际名称进行查询,并确保z 顺序是无关的。<title>" & windowName & "</title>

    • 编码的repeat 循环被编写为等待HTML 文件加载最多 3 秒,并且应该有足够的时间。必要时进行调整。

    • 我选择放弃使用您的答案中使用的do shell script 命令,但是,如果您更喜欢使用它,那么:

    代替:

    if not my writeToFile(tmpFileContent, tmpFileName, true) then return
    

    和:

    set shellCMD to {"echo '", tmpFileContent, "' > '", tmpFileName, "'"} as string
    do shell script shellCMD
    

    然后从代码部分中删除on writeToFile(theData, theFile, overwriteExistingContent) 处理程序。-- # Handler #

    此外,如果您更喜欢删除tmp 文件、变量值的do shell script 命令,则:tmpFileName

    代替:

    tell application "System Events" to delete file tmpFileName
    

    和:

    do shell script "rm " & tmpFileName's quoted form
    


    原始答案

    以下示例 AppleScript 代码是一个示例,说明如何在Safari的同一窗口中打开URL列表,而不管其窗口顺序如何。

    笔记:

    • 不要使用open location,因为它是Standard Additions的一部分,而不是Safari。使用该URL 属性设置文档或选项卡的URL。
    • 使用一个listURL和列表中列表项( URL )的索引。
    • 当Safari被指示时make new document,新文档成为Safarifront window的。在命令之后立即获取。window idfront windowmake new document
    • 对于list中的后续URL,开始循环并使用在执行命令后直接确定的。repeat 2window idmake new document
    set myURLs to {¬
        "https://apple.com", ¬
        "https://google.com", ¬
        "https://superuser.com", ¬
        "https://example.com"}
    
    tell application "Safari"
        
        make new document with properties {URL:item 1 of myURLs}
        set winID to id of front window
        
        repeat with i from 2 to (count myURLs)
            make new tab at end of tabs of window id winID with properties {URL:item i of myURLs}
            delay 1
        end repeat
        
    end tell
    
    • 1
  2. John
    2021-04-14T15:36:02+08:002021-04-14T15:36:02+08:00

    @user3439894 我接受了你的回答——它很好地回答了这个问题。还有另一种方法 - 一种为 Safari 窗口创建 ID 的 hackie 方法。

    这允许以下命令避免使用“前窗口”,以防在其余命令开始时创建的窗口不再是最前面的 - 值得怀疑,但可能。

    我不确定在创建窗口时是否可以为新窗口提供除“无标题”之外的任何名称。

    ## you could use: set x to(time/date combo) instead, and then use variable x in the touch command and "make new document with properties"
    do shell script "touch '/private/tmp/uniquename.txt'"
    tell application "Safari"
        make new document with properties {URL:"file:///private/tmp/uniquename.txt"}
        set winID to (id of window {name:"uniquename.txt"}) -- this guarantees that the window we just created is the window we are going to add tabs to
        set x to properties of window id winID -- let's take a peek at it's properties
    end tell
    x
    

    回复:

    tell current application
        do shell script "touch '/private/tmp/uniquename.txt'"
            --> ""
    end tell
    tell application "Safari"
        make new document with properties {URL:"file:///private/tmp/uniquename.txt"}
            --> document "Untitled"
        get id of window {name:"uniquename.txt"}
            --> 30049
        get properties of window id 30049
            --> {zoomable:true, closeable:true, zoomed:true, class:window, index:1, visible:true, name:"Untitled", miniaturizable:true, id:30049, miniaturized:false, resizable:true, bounds:{0, 23, 800, 423}, current tab:tab 1 of window id 30049, document:document "Untitled"}
    end tell
    Result:
    {zoomable:true, closeable:true, zoomed:true, class:window, index:1, visible:true, name:"Untitled", miniaturizable:true, id:30049, miniaturized:false, resizable:true, bounds:{0, 23, 800, 423}, current tab:tab 1 of window id 30049 of application "Safari", document:document "Untitled" of application "Safari"}
    
    • 0

相关问题

  • 如何使用 AppleScript 以 root 身份运行任何应用程序?

  • 什么是 UtilityParze?

  • 将 favicon 添加到还没有它们的网站 Safari

  • Safari 浏览器跟踪

  • Applescript - 为什么 killall 没有杀死我的应用程序

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve