• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Safari scripting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Safari scripting


  • Subject: Re: Safari scripting
  • From: Jeremy Sellors <email@hidden>
  • Date: Sat, 29 Aug 2009 16:31:56 -0700


On Aug 27, 2009, at 3:45 AM, Timofey Danshin wrote:

Hello.
I want to create a button in Safari that would, when pressed, copy the text of the web-page open in Safari into a new Pages document.


I have a script that I use several times a day that copies the selected article including the heading, any embedded graphics and creates a new pages document. The script also saves the document with the name using the first paragraph of the selection. If the first paragraph happens to have no text the user is asked to name the document. A footnote is created of the current date and the page’s web address.
The applescript and the java script have the disadvantage that the selected text loses all its formatting and the images are lost so I use the UI Browser over at PreFab Software
to help build the script.
The finished document usually has to be cleaned up a bit to get rid of unnecessary stuff and double spaces etc.
If the article spans several web pages then another script is used to append the additions to the article.
The script is a bit of a slow clunker on my aging G4 machine so I have some delays.
For convenience I call the scripts from the contextual menu using OnMyCommand from Abracode
Script:
if checkProcess(“Pages”) = false then -- Checks to see if Pages is open
try
tell application “Finder”
open application “Pages”
delay 4
end tell
end try
end if
tell application “Safari”
set pageURL to “”
set theSelection to “”
set docTextName to “”
set the clipboard to “error” --to check if text is actually copied latter
activate
set pageURL to URL of document 1 of application “Safari” as string
set footnote to ((current date) & return & pageURL) as string
end tell
tell application “System Events”
get system attribute “sysv”
if result is greater than or equal to 4144 then -- Mac OS X 10.3.0
--if UI elements enabled then this does’nt work anymore.
tell application process “Safari”
click menu item “Copy” of menu 1 of menu bar item “Edit” of menu bar 1
--keystroke “c” using command down did not work for some reason.
delay 4 --some copies are long and include images and graphs might not be needed on faster machines.
end tell
set theSelection to the clipboard --this is to get get the text for the title
else
beep
display dialog “GUI Scripting is not enabled” & return & return & “Open System Preferences and check Enable Access for Assistive Devices in the Universal Access preference pane, then run this script again.” with icon stop
if button returned of result is “OK” then
tell application “System Preferences”
activate
set current pane to pane “com.apple.preference.universalaccess”
end tell
end if

(* else
beep
display dialog “This computer cannot run this script” & return & return & “The script uses GUI Scripting technology, which requires an upgrade to Mac OS X 10.3 Panther or newer.” with icon caution buttons {“Quit”} default button “Quit” *)
end if
end tell
if (the clipboard) is not “error” then
set docTextName to every character of paragraph 1 of theSelection as string
else
activate me
display dialog “The clipboard is empty!
Select some text first and then re-run this script.”
return
end if
if docTextName is equal to “” then -- some headings are gifs
set docTextNameTemp to every character of paragraph 2 of theSelection as string
tell me to activate
set docTextName to text returned of (display dialog “What is the document name?” default answer docTextNameTemp)
end if
set CharCnt to the count of characters in docTextName
if CharCnt > 100 then
set docTextName to characters 1 thru 100 of docTextName as string -- could be set to less
end if
if docTextName contains “:” or “—” then -- : not allowed in OS X file name
set docTextNameList to every character of docTextName as list
set docTextName to “”
repeat with i in docTextNameList
if i contains “:” or “—” then
set docTextName to docTextName & “_”
else
set docTextName to docTextName & i
end if
end repeat
end if
if docTextName contains “—” then -- : not allowed in OS X file name
set docTextNameList to every character of docTextName as list
repeat with i in docTextNameList
if i contains “—” then
set docTextName to docTextName & “_”
else
set docTextName to docTextName & i
end if
end repeat
end if
tell application “Pages”
activate
if ((count of documents) = 0) then
make new document at beginning
else if name of document 1 is not “untitled” then
make new document at beginning
end if
tell application “System Events”
get system attribute “sysv”
if result is greater than or equal to 4144 then -- Mac OS X 10.3.0
--if UI elements enabled then
tell application process “Pages”
keystroke “v” using {command down}
delay 3
keystroke return
click menu item “Footnote” of menu 1 of menu bar item “Insert” of menu bar 1
set the clipboard to footnote
click menu item “Paste and Match Style” of menu 1 of menu bar item “Edit” of menu bar 1
delay 3
end tell
else
beep
display dialog “GUI Scripting is not enabled” & return & return & “Open System Preferences and check Enable Access for Assistive Devices in the Universal Access preference pane, then run this script again.” with icon stop
if button returned of result is “OK” then
tell application “System Preferences”
activate
set current pane to pane “com.apple.preference.universalaccess”
end tell
end if
end if
(* else
beep
display dialog “This computer cannot run this script” & return & return & “The script uses GUI Scripting technology, which requires an upgrade to Mac OS X 10.3 Panther or newer.” with icon caution buttons {“Quit”} default button “Quit”
end if *)
end tell
set Documents_path to (path to documents folder as string)
set Documents_path to Documents_path & “Articles:”
set filepath to Documents_path & docTextName & “.pages” as text
try
if exists filepath as alias then
set filepath to Documents_path & docTextName & “1.pages” as text
end if
end try
save document 1 in filepath as alias
end tell
on checkProcess(processName)
tell application “System Events”
set isRunning to ((application processes whose (name is equal to processName)) count)
end tell
if isRunning is greater than 0 then
return true
else
return false
end if
end checkProcess
End Script.
This script is a bit rough and I would be interested in any improvements.
Hope this helps, ——Jeremy


_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden
  • Prev by Date: Re: Creating a list
  • Next by Date: Re: Finder scripting bugs
  • Previous by thread: Re: Safari scripting
  • Next by thread: SOLVED: Unable to send user input string to Webster Dictionary Application using variable
  • Index(es):
    • Date
    • Thread