Save document as PDF with current months date

I have a script that goes to a web page and logs in. Then navigates to another web page and clicks a link that prints the page. After which I hit print to "Save As PDF".

On the page in the top right corner there a right aligned table with the date. I want to save the date just as it appears on the page. Some advice if anyone has the time...

  • Should I attempt to find it and select it or would it be better to have Keyboard Maestro just print the current date as it's formatted on the page?

Period:
04/01/17 thru 04/30/17

  • I have Hazel filing this by using "01/17 thru " as the trigger.

  • Next I need to get the mouse from the Name field in the save dialogue box down to the "Title" field which I usually just repeat the date.

  • Tabbing doesn't seem reliable so is there a way for me to get Keyboard Maestro to 'select title' or 'set title' as "$FirstDayCurrentMonth&" "&" "thru $LastDayCurrentMonth" (Assuming I'll need to write the variable in a way Keyboard Maestro dictates).

  • I also set keywords which include the current year, an acronym, and keywords about what it is. So I'd either need to tab down or set the keyword field.

Comments welcome.

Hey John,

I don't think you've made yourself clear here.

Finding that date should be easy enough. Here's how to get the text of the page you're going to save:

------------------------------------------------------------------------------
tell application "Safari"
   tell front document
      set pageText to its text
   end tell
end tell
------------------------------------------------------------------------------

If you have the Satimage.osax AppleScript Extension installed then the task is easy.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/05/04 00:30
# dMod: 2017/05/04 00:35 
# Appl: Safari
# Task: Extract Text from the front page and search for a regular expression.
# Libs: None
# Osax: Satimage.osax
# Tags: @Applescript, @Script, @Safari, @Satimage.osax, @Extract, @Text, @Front, @Page, @Search, @Regular, @Expression, @RegEx
------------------------------------------------------------------------------

tell application "Safari"
   tell front document
      set pageText to its text
   end tell
end tell

set docDate to fndUsing("Period:.*\\n(\\d{2}/\\d{2}/\\d{2} thru \\d{2}/\\d{2}/\\d{2})", "\\1", pageText, false, true) of me

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on fndUsing(_find, _capture, _data, _all, strRslt)
   try
      set findResult to find text _find in _data using _capture all occurrences _all ¬
         string result strRslt with regexp without case sensitive
   on error
      false
   end try
end fndUsing
------------------------------------------------------------------------------

Getting around the Save as PDF sheet is not nearly so simple, but it's doable.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/05/03 23:00
# dMod: 2017/05/04 22:03 
# Appl: Safari
# Task: Set focus to various fields in Save as PDF Sheet and set their values.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @System_Events, @Set, @Focus, @Fields, @Save_As, @PDF, @Sheet, @Value
------------------------------------------------------------------------------

tell application "System Events"
   tell application process "Safari"
      tell (first window whose subrole is "AXStandardWindow")
         tell sheet 1
            tell sheet 1
               
               tell text field 1 -- Save As field
                  set focused to true
                  set value to "Goofy File Name.pdf"
               end tell
               
               tell group 1
                  
                  tell text field 3 -- Title field
                     set focused to true
                     set value to "Goofy PDF Title"
                  end tell
                  
                  tell text field 4 -- Author field
                     set focused to true
                     set value to "Goofy Author's Name"
                  end tell
                  
                  tell text field 2 -- Subject field
                     set focused to true
                     set value to "Goofy Subject"
                  end tell
                  
                  tell text field 1 -- Keywords field
                     set focused to true
                     set value to "Keyword01,Keyword02,Keyword03"
                  end tell
                  
               end tell
               
            end tell
         end tell
      end tell
   end tell
end tell

------------------------------------------------------------------------------

-Chris