Sunday, December 4, 2022

#1 2022-11-18 05:56:31 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Safari reeder to RTF document

In this example I like to show a way to use GUI Scripting make ex. Safari to perform actions.
There the user only need to know the menu item.

With the try block it will not perform error, if the menu item do change.

This example will give you RTF document in TextEdit of content of front window Safari.
3 handlers to perform the actions and 1 tell block to make RTF document.

Ps. Try to run the script on Macscripter.net smile

Applescript:

its clickMenuItem:"Show Reader" inProcess:"Safari"
its selectAllInProcess:"Safari"
its copySelectedTextInProcess:"Safari"

tell application "TextEdit"
   make new document with properties {text:(the clipboard as «class RTF »)}
   activate
end tell

on clickMenuItem:menuItem inProcess:processName
   tell application "System Events" to tell process processName
       set frontmost to true
       repeat with anItem in (menu bar item of menu bar 1)
           tell menu bar 1 to tell menu bar item (name of anItem)
               try
                   click menu item menuItem of menu 1
               end try
           end tell
       end repeat
   end tell
end clickMenuItem:inProcess:

on selectAllInProcess:processName
   tell application "System Events" to tell process processName
       set frontmost to true
       key code "0" using {command down}
   end tell
end selectAllInProcess:

on copySelectedTextInProcess:processName
   tell application "System Events" to tell process processName
       set frontmost to true
       key code "8" using {command down}
   end tell
end copySelectedTextInProcess:

Last edited by Fredrik71 (2022-11-18 06:00:11 am)


Node-RED makes it easy to automate IoT

Offline

 

#2 2022-11-22 03:15:35 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

Re: Safari reeder to RTF document

Simpler approach, without using GUI scripting.

Applescript:


tell application "Safari"
   activate
   (do JavaScript "document.execCommand('selectAll')" in document 1) -- select all
   (do JavaScript "document.execCommand('copy')" in document 1) -- copy selection
end tell

tell application "TextEdit"
   make new document with properties {text:(the clipboard as «class RTF »)}
   activate
end tell

Last edited by KniazidisR (2022-11-22 05:45:21 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#3 2022-11-22 07:17:40 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Safari reeder to RTF document

@KniazidisR
Sure, but your copy is not exactly the same as mine, you forgot "Show Reader" that display a more clean text view of a page. One of the main reason I use reader in Safari is to hide ads.
You could read more here: https://support.apple.com/en-gb/guide/s … i32632/mac

The limitation of this approach to use reader on macscripter.net is it only display specific portion of the site. On the other hand you could addjust the font and size in reader to look more nice before copy. In the end I get a more clean approach to the content I like to make a RTF of.

Challenge for you:
Make a reader in JavaScript, I have done that when I did a research how Safari reader works.

Last edited by Fredrik71 (2022-11-22 07:47:35 am)


Node-RED makes it easy to automate IoT

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)