使用 applescript 获取倒数第二个 window 索引

get next-to-last window index with applescript

我无法核对这些案例。 3和4完全一样,只是应用不同。 5 和 6 也一样。我也用 brave 代替了 safari。他们似乎工作相同。 textedit 似乎也像其他人一样工作。 finder 是特例还是有其他问题?

# Case 1) Give tell a string, get last window index: succeeds.
tell application "finder"
    get index of last window
end tell

# Case 2) Give tell a variable, get last window index: succeeds.
set the_application to "finder"
function_a(the_application)
on function_a(the_application)
    tell application the_application
        get index of last window
    end tell
end function_a

# Case 3) Give tell a variable, get next-to-last window index for finder: fails.
# 651:656: execution error: Finder got an error: Can’t get window before Finder window id 63457. (-1728)
set the_application to "finder"
function_b(the_application)
on function_b(the_application)
    tell application the_application
        get index of window before last window
    end tell
end function_b


# Case 4) Give tell a variable, get next-to-last window index for safari: succeeds.
set the_application to "safari"
function_c(the_application)
on function_c(the_application)
    tell application the_application
        get index of window before last window
    end tell
end function_c

# Case 5) Give tell a string, get next-to-last window index: succeeds.
tell application "safari"
    get index of window before last window
end tell

# Case 6) Give tell a string, get next-to-last window index: fails.
1420:1425: execution error: Finder got an error: Can’t get window before Finder window id 63457. (-1728)
tell application "finder"
    get index of window before last window
end tell

# Case 7) Give tell a string, get next-to-last finder window index: succeeds.
tell application "finder"
    get index of finder window before last window
end tell

任何可编写脚本的应用程序都有一个独特的 AppleScript 字典。

当然,应用程序可以有一些共同点,例如 windows 和文档,但是 windows 元素的实现和索引的处理是不同的。没有通用的约定如何做到这一点。即使具有相同名称的属性在不同的应用程序中也可以有不同的四字符代码(内部术语标识符)。

例如 NSPositionalSpecifier 必须在目标应用程序中显式实现才能使用 before/after 语法。

在我看来,为多个应用程序编写可重用脚本的努力是浪费时间。

我将以 Safari 为例向您展示解决方案。

情况一(可能是您的情况):您的 Safari 设置是 "open new windows as windows":

set the_application to "Safari"
function_c(the_application)

on function_c(the_application)
    run script "tell application \"" & the_application & "\"
get index of window before last window
    end tell"
end function_c

案例二(如果您的 Safari 设置为 “打开新的 windows 作为标签页”):

set the_application to "Safari"
function_c(the_application)

on function_c(the_application)
    run script "tell application \"" & the_application & "\"
get index of tab before last tab of window 1
    end tell"
end function_c

与上面的代码相同,但对于多个应用程序:

set the_application to choose from list {"Safari", "Finder", "TextEdit"}
if the_application is false then return

set the_application to item 1 of the_application
function_c(the_application)

on function_c(the_application)
    run script "tell application \"" & the_application & "\"
get index of last window
end tell"
end function_c

其他漂亮例子:

return {|Safari|:function_c("Safari"), |Finder|:function_c("Finder"), |TextEdit|:function_c("TextEdit")}

on function_c(the_application)
    try
        run script "tell application \"" & the_application & "\"
get index of last window
end tell"
    on error
        return missing value
    end try
end function_c