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

时间:2015-06-11 04:26:29

标签: firefox applescript

我对AppleScript相对较新,我正试图找到一种方法,以编程方式在Firefox中的“另存为... ...”对话框中输入文件名。

我已经远远没有在网上搜索了。我在Xcode中打开了Accessibility Inspector,我可以检查各种表单元素。

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将每个标签保存为带有“前缀”+“数字”文件名的pdf文件,例如“foo001.pdf”,“foo002.pdf”等。

  1. 如何设置可以在AppleScript中增加的变量?
  2. 如何在“另存为PDF ...”对话框的相应字段中输入可编程文件名?
  3. 假设脚本以Firefox中当前活动的选项卡开头,我如何测试何时到达最后一个选项卡? (我想我可以让它选择“下一个”标签,直到出现错误)
  4. 提前致谢。

2 个答案:

答案 0 :(得分:0)

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

回答你的三个问题:

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

    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
    
  4. 在Safari中打开四个选项卡运行脚本后,我得到以下结果。

    Script result

答案 1 :(得分:0)

  

如何设置一个可以在AppleScript中增加的变量?

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

如何在可用的字段中输入可编程文件名   另存为PDF ...对话框?

使用keystroke "newFileName"查看脚本的位置。

  

假设脚本以当前活动的选项卡开头   Firefox,我如何在到达最后一个标签时进行测试? (我想我   可以让它选择“下一个”选项卡,直到它出现错误)

也许你可以找到一种方法来检查下一个标签是否存在(在右侧),然后再切换到它,这样你就知道何时停止脚本。最后它只是进入第一个标签(没有错误)。


要查看一些有用的代码段,请按住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
相关问题