generated from mhucka/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Save PDFs of All Tabs.applescript
47 lines (40 loc) · 1.88 KB
/
Save PDFs of All Tabs.applescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# =============================================================================
# @file Save PDFs of All Tabs
# @brief Convert open tabs to PDF and save them in a destination folder
# @author Michael Hucka <[email protected]>
# @license Please see the file LICENSE in the parent directory
# @repo https://github.com/mhucka/devonagent-hacks
# =============================================================================
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
# Utility functions.
# .............................................................................
on sanitizedFileName(name)
# Return a name with most non-ASCII characters replaced with "-" and leading
# and trailing whitespace removed. This is more strict than necessary; macOS
# allows many more characters in file names, but this function makes the
# result more compatible with Linux/Unix file systems.
if length of name > 250 then
set name to text 1 thru 250 of name
end if
return do shell script "echo " & quoted form of name & " | sed 's|[^a-zA-Z0-9_ ]|-|g;s|^[ ]*||;s|[ ]*$||'"
end sanitizedFileName
# Main body.
# .............................................................................
tell application id "DNag"
activate
set destinationFolder to (choose folder with prompt "Choose destination folder for PDF files")
set numTabs to count of (tabs of browser 1)
show progress indicator "Generating PDF files ..." steps numTabs
repeat with theTab in (tabs of browser 1)
set theTitle to name of theTab
set contentAsPDF to PDF of theTab
set fileName to my sanitizedFileName(theTitle) & ".pdf"
set filePath to (the POSIX path of destinationFolder) & "/" & fileName
set theFile to open for access filePath with write permission
step progress indicator theTitle
write contentAsPDF to theFile
close access theFile
end repeat
hide progress indicator
end tell