I have a BBEdit script that needs some help. It works as expected if I have the BBEdit file named “WebCopy” opened – the file can be Minimized to the Dock, or hidden (Hide App, from the Menu) and it works just fine. But If the file is not open, the script does not create the new file. Here’s the script…
tell application “Safari” to set theText to (do JavaScript “getSelection()” in document 1) as string
tell application “Finder”
if (exists file “WebCopy”) is false then
tell application “BBEdit” to make new text window with properties {contents :theText , name :“WebCopy”, soft wrap text :true } & return & return – this goofs, it does not make a new file
else if (exists file “WebCopy”) is true then
tell application “BBEdit”
set moreText to theText
select insertion point after last character of text window 1
set selection of window 1 to moreText & return & return
end tell
end if
end tell
How can I make it create the new BBEdit “WebCopy” file?
The basic idea is to be able to copy some text from one source then append the “WebCopy” doc with text copied from another source.
jonn8
July 14, 2003, 4:57am
#3
You can also add this line in the write_to_file handler after it has closed access to the file:
Jon
The original script does not create a new file because it’s never saved.
There is a difference between a text window and a text document on disk.
While you can create a new text window with the text from the web page, you need to tell BBEdit to save it before the disk file will be created.
Alternatively, as already demonstrated, there’s no need to use BBEdit at all since you can use AppleScript’s file writing commands to append the text.
jonn8
July 14, 2003, 5:01am
#5
If you don’t want to keep the file, change the path to temporary items:
set the_path to (path to temporary items as string ) & “WebCopy.txt”
tell application “Safari” to set theText to (do JavaScript “getSelection()” in document 1) as string
my write_to_file (the_path , theText )
tell application “BBEdit”
activate
open (the_path as alias )
find theText searching in text of text window 1 with selecting match
end tell
on write_to_file (the_path , theText )
try
open for access file the_path with write permission
write (theText & return ) to file the_path starting at eof
close access file the_path
tell application “Finder” to set creator type of (the_path as alias ) to “R*ch”
on error
try
close access file the_path
end try
end try
end write_to_file
Jon
Wowzers Jonn8, that’s pretty close. I was wanting to have a double carriage return between the first slection and the subsequent selections, but this is a great help. My main problem was getting the document there from the start. I was able to do so only if I had the “WebCopy” doc opened. Many thanks…
jonn8
July 14, 2003, 5:22am
#7
Here’s some more code that does what I think you originally wanted:
tell application “Safari” to set theText to (do JavaScript “getSelection()” in document 1) as string
tell application “BBEdit”
activate
if (exists of window “WebCopy”) = false then
set doc_ref to make new text window
tell doc_ref to set name to “WebCopy” as Unicode text
else
set doc_ref to (a reference to window “WebCopy”)
end if
tell doc_ref
set soft wrap text to true
set contents to (((text of contents ) & return & theText ) as text )
set s_options to {search mode :literal , starting at top :false , wrap around :false , reverse :true , case sensitive :true , match words :false , extend selection :false }
find theText searching in text options s_options with selecting match
end tell
end tell
Jon
Thanks Camelot, saving the document was something else I wasn’t aware of. I was trying to create a file with selections from different sources into one readable file. e.g., If I visit webpage A, I can copy the selected text into a file, then append that file with text selected from webpage B, C, D, … with a double carriage return between the subsequent selections.
This seems to be working quite well…
tell application “Safari” to set theText to (do JavaScript “getSelection()” in document 1) as string
tell application “BBEdit”
activate
if (exists of window “WebCopy”) = false then
set doc_ref to make new text window
tell doc_ref to set name to (“WebCopy” as Unicode text )
else
set doc_ref to (a reference to window “WebCopy”)
end if
tell doc_ref
set soft wrap text to true
set contents to (((text of contents ) & return & theText ) as text ) & return & return – double returns between selections
set s_options to {search mode :literal , starting at top :false , wrap around :false , reverse :true , case sensitive :true , match words :false , extend selection :false }
find theText searching in text options s_options with selecting match
end tell
end tell
Many thanks to jonn8 and Camelot.