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

804 views Asked by At

I am relatively new to AppleScript and I'm trying to find a way to programmatically enter a filename into a Save as PDF... dialog in Firefox.

I've gotten this far from searching the web. I've turned on the Accessibility Inspector in Xcode and I can inspect the various form elements.

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

My situation is this: I have Firefox open with 20 tabs and I want to use AppleScript to save each tab as a pdf file with a "prefix"+"number" filename, e.g. "foo001.pdf", "foo002.pdf", etc.

  1. How do I set up a variable that I can increment in AppleScript?
  2. How do I enter a programmable filename into the appropriate field in the Save as PDF... dialog?
  3. 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)

Thanks in advance.

2

There are 2 answers

1
AudioBubble On BEST ANSWER

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?

See script where it does that with 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)

Maybe you can find a way to check if the next tab exists (on the right side) before switching to it so you know when to stop the script. At the end it just goes to the first tab (no error).


To see some useful code snippets, ctrl-click into a script. It also has repeat routines which may help you to archive the looping.

I can't help more, but here a working boiler plate:


Part 1 stores the current page as pdf with "newFileName" as name and the second part goes to the next tab.

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
3
Nick Groeneveld On

You need to know that Firefox is not scriptable at all. However, Safari (partially) is scriptable. I hope this isn't an issue for you. I have added comments to help you out.

To answer your three questions:

  1. You can run a repeat that increases a variable every time it repeats.
  2. The variable in question one can be added to a filename-variable.
  3. I use the 'count every tab' function to get the total number of tabs. In combination with the previous two answers you can let the repeat loop run the exact amount of times necessary to get 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
    

After running the script with four tabs open in Safari, I get the following result.

Script result