【问题标题】:Applescript "Google Chrome got an error: Can’t make |tabs| of window id 1 into type specifier."Applescript“Google Chrome 出现错误:无法将窗口 id 1 的 |tabs| 设置为类型说明符。”
【发布时间】:2020-05-13 04:37:00
【问题描述】:

Applescript 新手。

尝试制作一个脚本,循环浏览我的标签,将每个标签打印为 PDF。

从这里开始……

Example of working Google Chrome Applescript

...但试图概括以适用于所有 Google Chrome、Firefox 和 Safari。为了开始这个修改,我首先查看了herehere

我马上就被绊倒了,我注意到一个小小的改变可以产生很大的不同......

没有错误:

tell application "Google Chrome"
    set myWindow to front window
    set myTabs to tabs in myWindow
    --Do more stuff here...
end tell

错误:

set webBrowser to "Google Chrome"
tell application webBrowser
    set myWindow to front window
    set myTabs to tabs in myWindow
    --Do more stuff here...
end tell

我得到的错误是:

“Google Chrome 出现错误:无法将窗口 ID 1 的 |tabs| 设置为类型说明符。”此错误中的“违规对象”是“窗口 id 1 的选项卡”,它告诉我预期的类型是“引用”。

任何想法如何进行?感谢您的帮助。

贾斯汀

编辑:

set webBrowser to "Firefox" -- or Safari
set pdfMenuItemTitle to "Save as PDF…" -- the title of the menu item you want
set myDelay to 0.2

tell application webBrowser to activate
tell application "System Events"
    tell process webBrowser
        -- open the print dialog
        click menu item "Print…" of menu 1 of menu bar item "File" of menu bar 1
        delay myDelay

        --Do more stuff here...

    end tell
end tell

【问题讨论】:

  • 变量不能用于tell语句的目标 - 任何给定应用程序的脚本术语都需要在编译时声明。
  • @red_menace ...感谢您的回复:)。快速的问题,不过...查看我的编辑...为什么 EDIT 部分代码有效?
  • 之所以有效,是因为您没有使用应用程序的脚本术语,而是通过系统事件(在编译时声明)为用户界面编写脚本。

标签: applescript


【解决方案1】:

不,关键不是您不能在应用程序的 tell 块中使用变量。可以做到,只是从编程的角度来看效率不高。因为你需要告诉 AppleScript 从哪里获取 terms,比如在 tell 块中使用的 tabs

纯粹出于演示目的:

set webBrowser to (choose from list {"Google Chrome", "Safari", "FireFox"})
if webBrowser is false then return
set webBrowser to item 1 of webBrowser

tell application webBrowser
    if webBrowser is "Google Chrome" then
        using terms from application "Google Chrome"
            set myTabs to tabs in front window
        end using terms from
    else if webBrowser is "Safari" then
        using terms from application "Safari"
            set myTabs to tabs in front window
        end using terms from
    else -- it is Firefox. It has only windows, not the tabs.
        set myTabs to front window
    end if
end tell

在实践中,最好不要变态,而是这样做:

set webBrowser to (choose from list {"Google Chrome", "Safari", "FireFox"})
if webBrowser is false then return
set webBrowser to item 1 of webBrowser

if webBrowser is "Google Chrome" then
    tell application "Google Chrome" to set myTabs to tabs in front window
else if webBrowser is "Safari" then
    tell application "Safari" to set myTabs to tabs in front window
else -- it is Firefox. It has only windows, not the tabs.
    tell application "Firefox" to set myTabs to front window
end if

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多