Can apple script change the way Safari opens websites?
For example, I frequently use Wikipedia and after I open a page in Wikipedia, I have to click on “Printable version” to save it or print it.
Can I have Safari always open Wikipedia web page directly in printable version?
try this, it works only with Safari.
You need a tool to be able to run the script with a shortcut.
Move the mouse over a link and press the shortcut
tell application "System Events" to set frontProc to name of 1st process whose frontmost is true
if frontProc is "Safari" then
try
tell application "System Events"
tell process "Safari"
set StatusItem to 1st menu item of menu 1 of menu bar item 5 of menu bar 1 whose title contains "Status"
tell StatusItem to set StatusBarOff to its title contains "Show" or its title contains "ein"
if StatusBarOff then display dialog "Enable status bar in Safari and run script again" buttons {"Cancel"} default button 1
set theLink to value of static text 1 of group 1 of window 1
end tell
end tell
if theLink does not contain "wikipedia" then error
on error
return
end try
set TID to text item delimiters
try
set text item delimiters to ASCII character 210
set theLink to text item 2 of theLink
set text item delimiters to ASCII character 211
set theLink to text item 1 of theLink
set text item delimiters to "//"
set wikiLanguage to text item 2 of theLink
set text item delimiters to "/"
set title to last text item of theLink
set text item delimiters to "."
set wikiLanguage to text item 1 of wikiLanguage
set text item delimiters to TID
open location "http://" & wikiLanguage & ".wikipedia.org/w/index.php?title=" & title & "&printable=yes"
on error
set text item delimiters to TID
end try
end if
.
set wikiLanguage to text item 1 of wikiLanguage
set text item delimiters to TID
tell application "Safari"
tell window 1
make new tab with properties {URL:"http://" & wikiLanguage & ".wikipedia.org/w/index.php?title=" & title & "&printable=yes"}
end tell
end tell
on error
.
You can’t make Safari navigate to a different URL from the one it’s given. You can give it a doctored URL, but you have to get the original URL into the script first.
-- Get the URL to the main page, perhaps in one of these three ways:
-- tell application "Safari" to set givenURL to URL of front document
-- set givenURL to "http://en.wikipedia.org/wiki/Extensible_Firmware_Interface"
-- set givenURL to (the clipboard)
-- Doctor the URL to fetch the printable page instead.
if (givenURL contains ".wikipedia.org/wiki/") then
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/wiki/"
set givenURL to givenURL's text items
set AppleScript's text item delimiters to "/w/index.php?title="
set newURL to (givenURL as text) & "&printable=yes"
set AppleScript's text item delimiters to astid
tell application "Safari" to set URL of front document to newURL
end if
If you’ve already opened the main page, it’s probably possible to use JavaScript to read the printable-page link from it, which would be more reliable if the link format were to change in the future.