Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何使用AppleScript中的窗口ID将Safari窗口带到前台?_Macos_Scripting_Window_Applescript - Fatal编程技术网

Macos 如何使用AppleScript中的窗口ID将Safari窗口带到前台?

Macos 如何使用AppleScript中的窗口ID将Safari窗口带到前台?,macos,scripting,window,applescript,Macos,Scripting,Window,Applescript,我有一个脚本,它记录了启动时激活的浏览器窗口。事情发生在中间,然后脚本需要回到原来的窗口和标签它被调用。 问题是,用户可能会在脚本运行期间更改活动窗口或选项卡。我想返回调用脚本时使用的窗口和选项卡 以下是我如何尝试(但失败)做到这一点: tell application "Safari" if (id of front window) is not (windowID of browserInfo) display dialog "Made it in the block

我有一个脚本,它记录了启动时激活的浏览器窗口。事情发生在中间,然后脚本需要回到原来的窗口和标签它被调用。 问题是,用户可能会在脚本运行期间更改活动窗口或选项卡。我想返回调用脚本时使用的窗口和选项卡

以下是我如何尝试(但失败)做到这一点:

tell application "Safari"
    if (id of front window) is not (windowID of browserInfo)
        display dialog "Made it in the block!"
        display dialog (get index of window 1)
        display dialog (get index of window (windowID of browserInfo))
-- ...
当然,这些对话框都是用于调试的。 现在,
browserInfo
是一个对象,其
windowID
属性对应于调用脚本的Safari窗口。这通常类似于“889”或“1195”或其他数字

现在,有趣的是,当模拟一个用户在一个窗口中启动,然后激活另一个窗口时,前四行正确地触发。第四行按预期返回“1”。但是第五行给出了一个错误:
Safari得到了一个错误:无法获取windows809。无效索引。

当我只能使用ID时,如何获取Safari窗口的索引


(是的,URL和窗口标题都很好,但它们超出了我的应用程序的范围。用户可能会打开多个具有相同URL和窗口标题的窗口。因此我需要特定的窗口ID。)

我相信这就是您想要的

on run
    tell application "Safari"
        set myID to id of window 1
        set myTab to current tab of window 1
    end tell

    do shell script "sleep 8" -- simulates time where user may change tabs

    tell application "Safari"
        set index of window id myID to 1
        set current tab of window 1 to myTab
    end tell
end run

我相信这就是你想要的

on run
    tell application "Safari"
        set myID to id of window 1
        set myTab to current tab of window 1
    end tell

    do shell script "sleep 8" -- simulates time where user may change tabs

    tell application "Safari"
        set index of window id myID to 1
        set current tab of window 1 to myTab
    end tell
end run

杰出的我缺少的部分包括
id
在窗口之后但在变量之前。即使尝试设置id为的窗口的索引(browserInfo的windowID)也没有成功。太棒了!我缺少的部分包括
id
在窗口之后但在变量之前。即使尝试设置id为的窗口的索引(browserInfo的windowID)也没有成功。