| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | Safariで |
| 006 | 表示中の画面をPDFで保存します |
| 007 | UIスクリプトなので実行中は操作無用 |
| 008 |
|
| 009 | こちらのスクリプトの改良版 |
| 010 | https://www.macscripter.net/t/gui-scripting-safari-export-as-pdf-dialog |
| 011 |
|
| 012 | マルチリンガル対応しています |
| 013 |
|
| 014 | 縦方向にスクロール入れて |
| 015 | レイジーな画像の読み込みに対応してみたテスト版 |
| 016 |
|
| 017 |
|
| 018 | com.cocolog-nifty.quicktimer.icefloe *) |
| 019 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- |
| 020 | use AppleScript version "2.8" |
| 021 | use scripting additions |
| 022 |
|
| 023 | #起動確認 |
| 024 | set boolRunning to application "Safari" is running |
| 025 | if boolRunning is false then |
| 026 | return "Safariが起動していません" |
| 027 | end if |
| 028 | #WINDOW確認 |
| 029 | tell application "Safari" |
| 030 | set numCntWindow to (count of every window) as integer |
| 031 | end tell |
| 032 | if numCntWindow = 0 then |
| 033 | return "Windowがありません" |
| 034 | end if |
| 035 | #Document確認 |
| 036 | tell application "Safari" |
| 037 | set numCntWindow to (count of every document) as integer |
| 038 | set strPageName to (name of front document) as text |
| 039 | end tell |
| 040 | if numCntWindow = 0 then |
| 041 | return "documentがありません" |
| 042 | else if strPageName contains "スタートページ" then |
| 043 | return "スタートページは書き出せません" |
| 044 | end if |
| 045 |
|
| 046 | #サイズを指定サイズに変更する |
| 047 | tell application "Safari" |
| 048 | tell front window |
| 049 | set bounds to {0, -13, 1280, 7200} |
| 050 | end tell |
| 051 | end tell |
| 052 |
|
| 053 | set refStat to (missing value) |
| 054 | set strJS to ("document.readyState") |
| 055 | repeat 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 |
| 076 | end repeat |
| 077 |
|
| 078 |
|
| 079 | #[ADD]Retrieve the localized names used in the UI |
| 080 | tell 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 |
| 083 | end tell |
| 084 |
|
| 085 | #set filename |
| 086 | set {year:y, month:m, day:d, hours:h, minutes:min, seconds:s} to (current date) |
| 087 | set theDate to ((y * 10000) + ((m as integer) * 100) + d) as text |
| 088 | set theTime to text 2 thru -1 of ((1000000 + (h * 10000) + (min * 100) + s) as text) |
| 089 | set fileName to "Safari (" & theDate & " " & theTime & ")" |
| 090 |
|
| 091 | #confirm Safari is open with URL |
| 092 | tell application "Safari" to set theURL to URL of front document |
| 093 | if 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 |
| 097 | tell application "System Events" to tell process "Safari" |
| 098 | set frontmost to true |
| 099 | delay 1 |
| 100 | end tell |
| 101 |
|
| 102 | #confirm "Export as "PDF dialog is available then display |
| 103 | set repeatNumber to 6 |
| 104 | tell 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 |
| 122 | end tell |
| 123 |
|