Sorry for the late reply and thank you all for the suggestions.
In the meantime i think i found i better solution to the “overwrite” problem and avoid duplicate files all along. I also got rid of “show reader” passage as i didn’t like the format.
set SaveFolderPath to POSIX path of (choose folder with prompt "Select Folder to Save PDF Files")
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
tell application "Safari"
tell window 1
set i to 1
set ctabs to (count tabs)
repeat with i from 1 to ctabs
tell application "Safari" to activate
tell application "Safari" to get the URL of the current tab of window 1
set taburl to the URL of the current tab -- now every pdf generated has the name as the url where it's from, much better
tell application "System Events"
tell process "Safari"
try
click menu item "Export as PDF…" of menu "File" of menu bar 1
on error -- sometimes i get error from safari "unable to export" since the file is too long and it doesn't have the necessary time to load the long text before "export as pdf" command, so i tried this so with no effect unfortunately
keystroke space -- the intention was to to tell safari to keep going with the cycle because when there's that error the script stops, anyway it doesn't work, it's like this passage gets completely ignored when the script runs
end try
repeat until exists sheet 1 of window 1
delay 1
end repeat
tell sheet 1 of window 1
set value of text field 1 to (taburl & ".pdf") as string -- now every pdf generated has the name as the url where it's from
click button "Save"
if exists then
keystroke space -- with this i'm able to overwrite a possible duplicate file, mind you that i had to activate complete "tab" keyboard function in system preferences (keyboard options)
end if
end tell
click menu item "Close tab" of menu "File" of menu bar 1
set resultDialogReply to display dialog "Close after 1 second..." giving up after 1
end tell
set i to i + 1
end tell
end repeat
end tell
end tell
end tell
end tell
this way seems to be better but there are still few issues:
the problem with a very long text in a page, which can’t be exported as pdf as it shows error and stops the script all along. the ideal solution would be to tell safari to wait for complete load of the page (but only when i get this kind error, so that there’s no need to put too much delay. you know like: when you get that error, then reload the page, wait for complete load and then export as pdf)
sometimes the destination folder is stuck with the first i choose, even if it asks for a destination folder every time i run the script (that’s a minor issue anyway)
Finally it would be cool before all of this accomplishment, to extract all the links (filtered by a word given by the user) from a webpage and open each one in a different tab, and then run the above script.
I found on this forum (god bless this forum) something helpful but it opens every possible link it finds on the webpage, whereas i would need to tell him to only open the links containing “whatever” in the url, this is the script i’m referring to:
--Script to open page links in Safari tabs
-- John Maisey 16/5/5
--
set myList to {}
tell application "Safari"
set myURL to URL of front document
set URLsource to source of front document
end tell
set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "href=\""}
set myChunks to (text items 2 thru -1 of URLsource)
set AppleScript's text item delimiters to myTID
repeat with myChunk in myChunks
set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "\""}
set myList to myList & (text item 1 of myChunk)
set AppleScript's text item delimiters to myTID
end repeat
repeat with myListNo from 1 to length of myList
set myLink to item myListNo of myList
if myLink does not contain "://" then
set myCount to -2
repeat
if (myLink starts with "../") or (myLink starts with "./") then
set myLink to (characters ((offset of "/" in myLink) + 1) thru -1 of myLink) as text
set myCount to myCount - 1
else
exit repeat
end if
end repeat
set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set myBase to (text items 1 thru myCount of myURL) as text
set AppleScript's text item delimiters to myTID
set myLink to (myBase & "/" & myLink) as text
set (item myListNo of myList) to myLink
end if
end repeat
tell application "Safari"
repeat with theURL in myList
my new_tab()
set the URL of document 1 to theURL
end repeat
end tell
--credit http://www.macosxhints.com/article.php?story=20030414185226343
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
end new_tab
Thanks again, have a great day