如何使用 AppleScript 在 Firefox 另存为对话框中输入文件名?

How to use AppleScript to enter a filename in Firefox Save As dialog?

我对 AppleScript 比较陌生,我正在尝试寻找一种方法,以编程方式将文件名输入到 Firefox 中的“另存为 PDF...”对话框中。

我已经远离搜索网络了。我在 Xcode 中打开了辅助功能检查器,我可以检查各种表单元素。

on run {input, parameters}

tell application "Firefox" to activate
tell application "System Events"
    tell process "Firefox"
        click menu item "Print…" of menu "File" of menu bar 1
        click menu button "PDF" of window "Print"
        keystroke "S"
        keystroke return
    end tell
end tell

return input
end run

我的情况是这样的:我打开了 Firefox,其中有 20 个选项卡,我想使用 AppleScript 将每个选项卡另存为带有 "prefix"+"number" 文件名的 pdf 文件,例如"foo001.pdf"、"foo002.pdf" 等

  1. 如何在 AppleScript 中设置可以递增的变量?
  2. 如何在“另存为 PDF...”对话框的相应字段中输入可编程文件名?
  3. 假设脚本从 Firefox 中当前活动的选项卡开始,我如何测试到达最后一个选项卡? (我想我可以让它 select 在 "next" 选项卡上直到出现错误为止)

提前致谢。

您需要知道 Firefox 根本无法编写脚本。但是,Safari(部分)是可编写脚本的。我希望这对你来说不是问题。我已经添加了评论来帮助你。

回答你的三个问题:

  1. 您可以 运行 每次重复都会增加变量的重复。
  2. 有问题的变量可以添加到文件名变量中。
  3. 我使用 'count every tab' 函数来获取选项卡的总数。结合前两个答案,您可以让重复循环 运行 获取每个选项卡所需的确切次数。

    set theDestination to ((path to desktop) as text)
    set theFileName to "SafariTabPDF" as text
    set i to 1
    
    tell application "Safari"
        activate
        tell window 1
    
            -- Get amount of open tabs
            set allTabs to count every tab
    
            -- Repeat with the total amount of open tabs
            repeat with i from 1 to allTabs
    
                set current tab to tab i
                set tempFileName to (theFileName & i) as text
    
                tell application "System Events" to tell process "Safari"
    
                    -- Work through the clicking process
                    click menu item "Export as PDF…" of menu "File" of menu bar 1
                    delay 1
                    keystroke tempFileName
                    keystroke return
                    delay 1
                end tell
    
                -- Add to i to get next tab and save as corresponding file name
                set i to i + 1
            end repeat
        end tell
    end tell
    

在 Safari 中打开带有四个选项卡的脚本后 运行我得到以下结果。

How do I set up a variable that I can increment in AppleScript?

set counter to 0 # setup counter
set counter to counter + 1 # increment counter

How do I enter a programmable filename into the appropriate field in the Save as PDF... dialog?

查看脚本keystroke "newFileName"

Assuming that the script starts with the currently active tab in Firefox, how can I test when I've reached the last tab? (I suppose I could just let it select the "next" tab until it gets an error)

也许您可以找到一种方法在切换到下一个选项卡之前检查下一个选项卡是否存在(在右侧),以便您知道何时停止脚本。最后它只是转到第一个选项卡(没有错误)。


要查看一些有用的代码片段,请按住 Ctrl 键并单击进入脚本。它还具有重复例程,可以帮助您归档循环。

我帮不上忙,但这里有一个可用的样板:


第 1 部分将当前页面存储为 pdf,名称为 "newFileName",第二部分转到下一个选项卡。

tell application "Firefox" to activate
delay 0.25

tell application "System Events"

    tell process "Firefox"
        set frontmost to true

        click menu item "Print…" of menu "File" of menu bar 1
        delay 0.25

        click menu button "PDF" of window "Print"
        delay 0.1
        keystroke "S"
        delay 0.1
        keystroke return

        repeat until window "Print" exists
            delay 0.5
        end repeat

        keystroke "newFileName"
        keystroke return

    end tell

end tell


tell application "System Events"
    tell process "Firefox"
        set frontmost to true
        # let's go to the next tab
        # (# 123 goes back) 
        key code 124 using {command down, option down}
    end tell
end tell