Hello.
Those scripts from 2003 used to work until recently, (Snow Leopard or Lion) , but haven’t in a while, because the do javascript command of Safari, has become more picky with returning values.
Here they are, I hope I haven’t done anything wrong with publishing them for general consumption, since they came from here in the first place. By the way, don’t try them here, because you’ll get better results if you log in, and hit the link anyways. But they will work all right on other pages, that displays applescripts, in code blocks, or as just text.
Safari Selection to Script Editor
-- http://files.macscripter.net/scriptbuilders/Safari/Safari_SelectionToEditor.sit
-- Version 1.0.1
-- Version 1.0.2 McUsr fixed the javascript. and it works as it should! (Safari 7.x )
(*
-- http://www.macupdate.com/app/mac/12312/safari-selection-to-script-editor-debugger
-- From that page:
Safari Selection to Script Editor/Debugger allows users to quickly and easily transfer AppleScript code from a Web page in Safari to a supported script editor (Script Editor 2 or Script Debugger). They take the selection of the front Safari window and generate a new script document in the target script editor. The code will be compiled if possible.
What's New
Version 1.0.1 adds the following:
Add the source code's page title and URL to the script's comments. Thanks Jonathan Nathan!
More thorough error checking.
Requirements
Safari 1.0 (v85) or later and Script Editor 2.0 or Script Debugger.
*)
try
-- If Safari isn't running, notify and terminate.
tell application "System Events"
if (exists process "Safari") is false then
display dialog "Safari isn't running. The script will now terminate." buttons ¬
{"OK"} default button 1 with icon 0
return
end if
end tell
tell application "Safari"
-- If no document, notify and terminate.
if (exists document 1) is false then
display dialog "No Safari document was found. The script will now terminate." buttons ¬
{"OK"} default button 1 with icon 0
return
end if
-- Get window title if possible, insert filler text if no window.
try
set title_ to name of window 1
on error
set title_ to "No page title was obtained by Safari. The script will now terminate."
end try
-- Get URL if possible, insert filler text if no URL.
try
set url_ to URL of document 1
on error
set url_ to "No URL was obtained by Safari."
end try
-- Attempt to get the selected text. Notify and terminate if no selection.
try
set selected_ to (do JavaScript "x=(function() { self.focus() ; b= self.getSelection().toString(); return b ; })();x" in document 1) as string
if selected_ is "" then
display dialog "No content selected. The script will now terminate." buttons ¬
{"OK"} default button 1 with icon 0
return
end if
on error
-- Advise user to copy and paste text into new script document 'cause somethin' failed.
display dialog "Safari was unable to obtain the selected text. Copy and paste " & ¬
"it into the new document which has been created in Script Editor." buttons ¬
{"OK"} default button 1 with icon 1
tell application "AppleScript Editor" to make new document at end with properties ¬
{contents:("-- Code found at:" & return & "-- Page Title: " & title_ & ¬
return & "-- URL: <" & url_ & ">" & return & return)}
return
end try
end tell
-- Convert line endings to returns.
set data_ to ""
repeat with i in paragraphs of selected_
set data_ to data_ & i & return
end repeat
-- Shake and bake.
set data_ to ("-- Code found at:" & return & "-- Page Title: " & title_ & return & ¬
"-- URL: <" & url_ & ">" & return & return) & data_
-- Serve for consumption.
tell application "AppleScript Editor"
activate
set doc_ to make at end new document with properties {contents:data_}
try
check syntax doc_
end try
end tell
on error e
display dialog e buttons {"OK"} default button 1 with icon 0
return
end try
Safari Selection to Script Debugger
-- http://files.macscripter.net/scriptbuilders/Safari/Safari_SelectionToEditor.sit
-- Version 1.0.1
-- Version 1.0.2 McUsr fixed the javascript. and it works as it should!
(*
-- http://www.macupdate.com/app/mac/12312/safari-selection-to-script-editor-debugger
-- From that page:
Safari Selection to Script Editor/Debugger allows users to quickly and easily transfer AppleScript code from a Web page in Safari to a supported script editor (Script Editor 2 or Script Debugger). They take the selection of the front Safari window and generate a new script document in the target script editor. The code will be compiled if possible.
What's New
Version 1.0.1 adds the following:
Add the source code's page title and URL to the script's comments. Thanks Jonathan Nathan!
More thorough error checking.
Requirements
Safari 1.0 (v85) or later and Script Editor 2.0 or Script Debugger.
*)
try
-- If Safari isn't running, notify and terminate.
tell application "System Events"
if (exists process "Safari") is false then
display dialog "Safari isn't running. The script will now terminate." buttons ¬
{"OK"} default button 1 with icon 0
return
end if
end tell
tell application "Safari"
-- If no document, notify and terminate.
if (exists document 1) is false then
display dialog "No Safari document was found. The script will now terminate." buttons ¬
{"OK"} default button 1 with icon 0
return
end if
-- Get window title if possible, insert filler text if no window.
try
set title_ to name of window 1
on error
set title_ to "No page title was obtained by Safari."
end try
-- Get URL if possible, insert filler text if no URL.
try
set url_ to URL of document 1
on error
set url_ to "No URL was obtained by Safari."
end try
-- Attempt to get the selected text. Notify and terminate if no selection.
try
set selected_ to (do JavaScript "x=(function() { self.focus() ; b= self.getSelection().toString(); return b ; })();x" in document 1) as string
if selected_ is "" then
display dialog "No content selected. The script will now terminate." buttons ¬
{"OK"} default button 1 with icon 0
return
end if
on error
-- Advise user to copy and paste text into new script document 'cause somethin' failed.
display dialog "Safari was unable to obtain the selected text. Copy and paste " & ¬
"it into the new document which has been created in Script Debugger." buttons ¬
{"OK"} default button 1 with icon 1
tell application "Script Debugger 4.5" to make new document with properties ¬
{contents:("-- Code found at:" & return & "-- Page Title: " & title_ & ¬
return & "-- URL: <" & url_ & ">" & return & return)}
return
end try
end tell
-- Convert line endings to returns.
set data_ to ""
repeat with i in paragraphs of selected_
set data_ to data_ & i & return
end repeat
-- Shake and bake.
set data_ to ("-- Code found at:" & return & "-- Page Title: " & title_ & return & ¬
"-- URL: <" & url_ & ">" & return & return) & data_
-- Serve for consumption.
tell application "Script Debugger 4.5"
-- activate
set doc_ to make new document at front with properties {script source:data_}
try
compile doc_
end try
end tell
on error e
display dialog e buttons {"OK"} default button 1 with icon 0
return
end try