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

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

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

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

我想通过使用window idif 来解决这个问题,但是需要一些脚本方面的帮助。

为此,我创建了以下内容,但我再次认为可能存在问题。我宁愿不使用 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

答案1

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

下列例子 苹果脚本 代码保证列表网址s 在目标中打开窗户无论其位置在里面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

笔记:根据需要滚动查看所有代码


笔记:

  • 更新的例子 苹果脚本 代码包括一些错误处理,如果临时文件(价值tmpFileName 多变的)没有创建脚本中止而不显示任何消息。可以通过转换if not my writeToFile ... 陈述充分if 堵塞并包括适当的display alertdisplay dialogdisplay notification 命令,正如所希望的那样,然后是return 命令

  • 按照代码,这将返回--> document "Untitled"make new document相关的内容,因为包含repeat 环形它等待,直到HTML 文件已加载并可以通过实际姓名文档定义如下<title>" & windowName & "</title> 标签并确保z 顺序无关紧要。

  • repeat 环形代码中写着等待 3 秒钟HTML 文件加载,时间应该足够。如有必要,请进行调整。

  • 我选择放弃使用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 # 部分代码

此外,如果您更喜欢do shell script 命令删除临时文件, 这价值tmpFileName 多变的, 然后:

代替:

tell application "System Events" to delete file tmpFileName

和:

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


原始答案

下列例子 苹果脚本 代码是一个如何打开的示例列表网址在相同的窗户苹果浏览器,无论其窗户命令。

笔记:

  • 请勿使用open location,因为它是标准添加, 不是苹果浏览器。 使用URL 财产设置网址文档或者标签
  • 使用list网址指数项目清单网址) 在里面列表
  • 什么时候苹果浏览器被指示make new document新文档成为front window苹果浏览器. 获取window id紧接着front windowmake new document 命令
  • 对于后续网址在里面列表,启动repeat 环形2并使用window id在之后直接确定的make 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

答案2

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

这样可以让以下命令避免使用“前窗口”,以防在其余命令开始时创建的窗口不再位于最前面 - 虽然值得怀疑,但有可能。

我不确定在创建窗口时是否有办法为新窗口赋予除“无标题”之外的其他名称。

## 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"}

相关内容