20260513

[AppleScript] SafariでWebページのPDF保存(UIスクリプト)

[AppleScript] SafariでWebページのPDF保存(UIスクリプト)

NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

WebページのPDF保存.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005Safariで
006表示中の画面をPDFで保存します
007UIスクリプトなので実行中は操作無用
008
009こちらのスクリプトの改良版
010https://www.macscripter.net/t/gui-scripting-safari-export-as-pdf-dialog
011
012マルチリンガル対応しています
013
014縦方向にスクロール入れて
015レイジーな画像の読み込みに対応してみたテスト版
016
017
018com.cocolog-nifty.quicktimer.icefloe *)
019----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
020use AppleScript version "2.8"
021use scripting additions
022
023#起動確認
024set boolRunning to application "Safari" is running
025if boolRunning is false then
026   return "Safariが起動していません"
027end if
028#WINDOW確認
029tell application "Safari"
030   set numCntWindow to (count of every window) as integer
031end tell
032if numCntWindow = 0 then
033   return "Windowがありません"
034end if
035#Document確認
036tell application "Safari"
037   set numCntWindow to (count of every document) as integer
038   set strPageName to (name of front document) as text
039end tell
040if numCntWindow = 0 then
041   return "documentがありません"
042else if strPageName contains "スタートページ" then
043   return "スタートページは書き出せません"
044end if
045
046#サイズを指定サイズに変更する
047tell application "Safari"
048   tell front window
049      set bounds to {0, -13, 1280, 7200}
050   end tell
051end tell
052
053set refStat to (missing value)
054set strJS to ("document.readyState")
055repeat 10 times
056   tell application "Safari"
057      tell front document
058         set refStat to (do JavaScript strJS) as text
059      end tell
060   end tell
061   tell application "Safari"
062      activate
063      tell application "System Events"
064         tell application process "Safari"
065            repeat 10 times
066               key code 125 using {control down}
067               delay 0.25
068            end repeat
069         end tell
070      end tell
071   end tell
072   if refStat is "complete" then
073      exit repeat
074   end if
075   delay 0.25
076end repeat
077
078
079#[ADD]Retrieve the localized names used in the UI
080tell application "Safari"
081   set strFile to (localized string ("83.title") from table ("MainMenu")) as text
082   set strExportPDF to (localized string ("1059.title") from table ("MainMenu")) as text
083end tell
084
085#set filename
086set {year:y, month:m, day:d, hours:h, minutes:min, seconds:s} to (current date)
087set theDate to ((y * 10000) + ((m as integer) * 100) + d) as text
088set theTime to text 2 thru -1 of ((1000000 + (h * 10000) + (min * 100) + s) as text)
089set fileName to "Safari (" & theDate & " " & theTime & ")"
090
091#confirm Safari is open with URL
092tell application "Safari" to set theURL to URL of front document
093if theURL is missing value then display dialog "The frontmost Safari window could not be found or did not return a URL." buttons {"OK"} cancel button 1 default button 1
094
095#I normally run this script by way of FastScripts with Safari active
096#as a result, the following is normally not necessary and is for testing only
097tell application "System Events" to tell process "Safari"
098   set frontmost to true
099   delay 1
100end tell
101
102#confirm "Export as "PDF dialog is available then display
103set repeatNumber to 6
104tell application "System Events" to tell process "Safari"
105   repeat with i from 1 to repeatNumber
106      #[ADD] localized names
107      set menuAvailable to enabled of menu item strExportPDF of menu strFile of menu bar 1
108      if menuAvailable is true then
109         exit repeat
110      else if i = repeatNumber then
111         display dialog "The \"Export as PDF\" menu item was not available." buttons {"OK"} cancel button 1 default button 1
112      else
113         delay 0.5
114      end if
115   end repeat
116   click menu item strExportPDF of menu strFile of menu bar 1
117   delay 1
118   keystroke fileName
119   #[ADD] using localized imput method return is required here.
120   keystroke return
121   delay 0.5
122end tell
123
AppleScriptで生成しました