Applescript "Google Chrome got an error: Can’t make |tabs| of window id 1 into type specifier."



Applescript的新功能。

尝试制作一个脚本,该脚本将循环浏览我的选项卡,将每个选项卡打印为 PDF。

从这里开始...

工作谷歌浏览器苹果脚本的例子

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

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

无错误:

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

我得到的错误是:

"谷歌浏览器出现错误:无法制作 |标签|将窗口 ID 1 转换为类型说明符。此错误中的"违规对象"是"窗口 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块中使用变量。这是可以做到的,只是从编程的角度来看效率不高。因为那时你需要告诉AppleScript从哪里得到术语,如告诉块中使用的标签

纯粹出于演示目的:

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

最新更新