hello there. i’ve created a task that automatically print a pdf located in a specific folder.
now i want to open a webpage (Sudoku - New York Times Number Puzzles — The New York Times), print as a pdf in a specific folder (eventually replace older versions).
is it possible?
thks
In brief, yes, but you are missing a lot of detail.
You haven’t noted what the task is create in, what device or OS you are using, or where the folder is, but for example on an iPhone you could use an Apple Shortcuts shortcut like this to do that sort of thing.
https://www.icloud.com/shortcuts/a7912cc2f1c24d08b21d40ece295a725
So sorry i was so focused…
So i m on OSX Sequoia 15.4
MacMini M4
I want to save the pdf in a folder named SUDOKU in the desktop
Thanks
Okay I have done some digging and testing. While you should in theory be able to use that shortcut, you can’t on the Mac. Shortcuts on the Mac isn’t quite as good with web requests (amongst other things). Furthermore the page being retrieved is built using JavaScript by the looks of it - so using direct access, the puzzle is not rendered. Using the Safari actions doesn’t help either. It really needs a full on browser to build the page.
The best way to do this would probably be to use a headless browser, except there are some things you have to okay through to even get the site to load and they can pop up extra pop ups like when they have new features to tout.
Because of this latter point, which I imagine also affects the previous shortcut, you start getting into some more serious web automation tools. E.g. using a headless browser plus an integration to allow user interaction, and then a tool to dynamically interact with and remove/close any new pop ups.
Some of this could be done in the foreground too using Safari instead, but the tooling available is perhaps more limited and could create other issues.
Perhaps an intermediate approach would be to use some AppleScript that you trigger and have it just step through the process for you, opening the URL, pressing buttons, and also giving you chance to manually intervene to confirm/close pop ups? Such an AppleScript could even be embedded in a shortcut.
Here’s an example to hopefully get you up and running.
-- Delete the existing file if it exists
set pdfPath to POSIX path of (path to desktop folder) & "today_sudoku_hard.pdf"
do shell script "rm -f " & quoted form of pdfPath
tell application "Safari"
activate
make new document with properties {URL:"https://www.nytimes.com/puzzles/sudoku/hard"}
end tell
delay 8 -- Wait for Safari to load the page
tell application "System Events"
tell process "Safari"
set frontmost to true
-- Open Export as PDF… from File menu
click menu item "Export as PDF…" of menu "File" of menu bar 1
delay 2
-- Open the "Go to the folder" dialog with ⇧⌘G
keystroke "G" using {shift down, command down}
delay 2
-- Type the folder path only (no trailing slash)
keystroke "~/Desktop"
delay 2
keystroke return
delay 2
-- Set the filename (no file extension)
set fileName to "today_sudoku_hard"
keystroke fileName
delay 2
-- Confirm Save
keystroke return
delay 2
end tell
end tell
-- Check file existence and modification time
set fileCheckCommand to "if [ -f " & quoted form of pdfPath & " ]; then " & ¬
"now=$(date +%s); " & ¬
"mod=$(stat -f %m " & quoted form of pdfPath & "); " & ¬
"delta=$((now - mod)); " & ¬
"if [ $delta -le 10 ]; then exit 0; else exit 1; fi; " & ¬
"else exit 1; fi"
try
do shell script fileCheckCommand
set saveSucceeded to true
on error
set saveSucceeded to false
end try
-- Close the Safari window only if save succeeded
if saveSucceeded then
tell application "Safari"
if (count of windows) > 0 then close front window
end tell
else
display dialog "PDF save failed or timed out." buttons {"OK"} default button 1
end if
Hope that helps.
many thks for your help…i’m gonna try this out in a couple of days.