AppleScript使用Safari窗口ID创建新选项卡

约翰

我有一个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
用户名

更新以解决对以下用途的关注: front window

下面的例子 的AppleScript 代码保证名单网址就会越来越目标打开窗口,无论其位置Z顺序窗口

set myURLs to {¬
    "https://apple.com", ¬
    "https://google.com", ¬
    "https://superuser.com", ¬
    "https://example.com"}

set randomNumber to random number from 1000000 to 9999999
set tmpFileName to "/private/tmp/" & randomNumber & ".html"
set tmpFileContent to "<html><head><title>" & randomNumber & "</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 randomNumber))
        delay 0.1
        set i to i + 1
        if i = 30 then return
    end repeat
    set winID to (id of windows whose name is randomNumber) 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 命令,如想要的。

  • 按照编码方式,此渲染--> document "Untitled"make new document无关的返回,因为在包含repeat 循环的情况下,它会一直等到HTML 文件加载完毕并可以由文档中的实际名称(由文档中的<title>" & randomNumber & "</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 命令删除tmp文件变量,则:tmpFileName

代替:

tell application "System Events" to delete file tmpFileName

和:

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


原始答案

下面的例子 AppleScript的 代码是如何打开一个例子列表的网址在同一窗口中Safari浏览器,不管它的窗口秩序。

笔记:

  • 不要使用open location它,因为它是Standard Additions的一部分,而不是Safari的一部分使用URL 属性来设置URL中的文档标签
  • 使用list网址指数的的列表项URL中)列表
  • Safari浏览器的指示make new document,在新的文件将成为front windowSafari浏览器获取window id的的front window中后,立即make new document 命令
  • 对于后续的URL列表中,启动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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Applescript:从显示对话框中打开一个新的 Safari 选项卡

来自分类Dev

使Safari在现有窗口中以选项卡(而不是新窗口)打开新链接

来自分类Dev

AppleScript:如何获取无法加载的Safari选项卡的URL?

来自分类Dev

如何使用applescript从网页表单窗口中的选项卡中移动?

来自分类Dev

创建新的选项卡或窗口时,防止gnome-terminal更改目录

来自分类Dev

创建新的选项卡或窗口时,防止gnome-terminal更改目录

来自分类Dev

使用选项卡从列表创建片段

来自分类Dev

使用指令创建导航选项卡?

来自分类Dev

使用foreach创建的HTML选项卡

来自分类Dev

使用ASP.NET进行二进制写入新的选项卡/窗口

来自分类Dev

鸣叫按钮打开新窗口和新选项卡

来自分类Dev

如何使用AppleScript关闭“终端”选项卡?

来自分类Dev

如何获取使用'chrome.tabs.create()创建的选项卡的窗口对象?

来自分类Dev

如何获取使用'chrome.tabs.create()创建的选项卡的窗口对象?

来自分类Dev

如何防止Firefox从选项卡之外创建窗口?

来自分类Dev

以编程方式在新选项卡中创建新的QTextEdit

来自分类Dev

如何双击选项卡栏创建新的选项卡?

来自分类Dev

在案例屏幕中使用新用户表中的网格创建新选项卡

来自分类Dev

无法在 MAC 上使用 JAVA 在 Selenium Webdriver 中创建或切换到新选项卡

来自分类Dev

如何使用用户输入创建 html 并将其显示在新选项卡中?

来自分类Dev

创建新的jQuery ui选项卡并填充Ajax数据

来自分类Dev

创建新的jQuery ui选项卡并填充Ajax数据

来自分类Dev

使用jQuery检测窗口未聚焦或选项卡开关

来自分类Dev

使用vba创建一个新的Ribbon选项卡,该选项卡仅在选择某些形状时出现

来自分类Dev

使用Angular UI Bootstrap在动态创建的选项卡上设置活动选项卡

来自分类Dev

MacOSX 10.9.1 Safari选项卡如何用于合并两个单个选项卡式窗口?

来自分类Dev

Javascript 选项卡使用数据属性而不是 ID 来链接按钮和选项卡

来自分类Dev

如何设置AppleScript以打开新的iTerm2选项卡并更改目录?

来自分类Dev

如何防止Webbrowser控件中的弹出窗口,新选项卡,新窗口和消息框?

Related 相关文章

  1. 1

    Applescript:从显示对话框中打开一个新的 Safari 选项卡

  2. 2

    使Safari在现有窗口中以选项卡(而不是新窗口)打开新链接

  3. 3

    AppleScript:如何获取无法加载的Safari选项卡的URL?

  4. 4

    如何使用applescript从网页表单窗口中的选项卡中移动?

  5. 5

    创建新的选项卡或窗口时,防止gnome-terminal更改目录

  6. 6

    创建新的选项卡或窗口时,防止gnome-terminal更改目录

  7. 7

    使用选项卡从列表创建片段

  8. 8

    使用指令创建导航选项卡?

  9. 9

    使用foreach创建的HTML选项卡

  10. 10

    使用ASP.NET进行二进制写入新的选项卡/窗口

  11. 11

    鸣叫按钮打开新窗口和新选项卡

  12. 12

    如何使用AppleScript关闭“终端”选项卡?

  13. 13

    如何获取使用'chrome.tabs.create()创建的选项卡的窗口对象?

  14. 14

    如何获取使用'chrome.tabs.create()创建的选项卡的窗口对象?

  15. 15

    如何防止Firefox从选项卡之外创建窗口?

  16. 16

    以编程方式在新选项卡中创建新的QTextEdit

  17. 17

    如何双击选项卡栏创建新的选项卡?

  18. 18

    在案例屏幕中使用新用户表中的网格创建新选项卡

  19. 19

    无法在 MAC 上使用 JAVA 在 Selenium Webdriver 中创建或切换到新选项卡

  20. 20

    如何使用用户输入创建 html 并将其显示在新选项卡中?

  21. 21

    创建新的jQuery ui选项卡并填充Ajax数据

  22. 22

    创建新的jQuery ui选项卡并填充Ajax数据

  23. 23

    使用jQuery检测窗口未聚焦或选项卡开关

  24. 24

    使用vba创建一个新的Ribbon选项卡,该选项卡仅在选择某些形状时出现

  25. 25

    使用Angular UI Bootstrap在动态创建的选项卡上设置活动选项卡

  26. 26

    MacOSX 10.9.1 Safari选项卡如何用于合并两个单个选项卡式窗口?

  27. 27

    Javascript 选项卡使用数据属性而不是 ID 来链接按钮和选项卡

  28. 28

    如何设置AppleScript以打开新的iTerm2选项卡并更改目录?

  29. 29

    如何防止Webbrowser控件中的弹出窗口,新选项卡,新窗口和消息框?

热门标签

归档