Export Web Page to Mori

Tags ( | )

Hi,

I discovered Mori a day ago and I'm very impressed with the app, the thoughtfulness of all of the users and Jesse Grosjean. It's a truly interesting experiment in development and made me want to learn more and get involved. Thanks!

I wanted a little project to get aquainted with extending Mori. I noticed some discussions about keeping Mori focussed on text (as opposed to images, pdfs etc). I can understand that, but must admit that sometimes it might be nice to have web pages, too, with images and live links. Since TextEdit style views can do a semblance of HTML rendering (as has been noted in the Forums), I reworked the example plugin (Import Safari Webpage), which imports the page text, to a version that tries to import the whole page.

You can put this in the system-wide scripts menu and call while you're browsing in Safari.

It definitely has some significant rough edges (documented in comments) but I hope it might be useful to someone.

Thanks so much!

PS. I'd truly appreciate any comments and improvements. There were several things I didn't know how to do. (I've been more of a obj-C kind of guy to now, but this opened my eyes to the utility and fun of applescripting and UI scripting! Thanks for giving me an opportunity to try something new).

[attached file incorporates changes discussed in thread below]

Version:
AttachmentSize
Export Web Page to Mori.scpt_.zip5.65 KB

John, welcome and thanks for

John, welcome and thanks for the contribution. I haven't had time to do much more then unzip and run, but I'm getting a crash every-time I run it. This might very well be a problem on Mori's end, but I'm curious if you ever see a crash problem when you run this script or if it just works for you. Thanks.

Hi Jesse, No it's not been a

Hi Jesse,

No it's not been a problem for me. That sounds frustrating.

Mori did crash two or three times (out a hundred plus runs), but I didn't think much of it. Unfortunately I didn't do anything with the crash logs (I don't suppose they are saved somewhere even if I choose not to submit?) but I recall it had to do with clipboard manipulation within Mori.

As the script is merely manipulating keyboard events, do you get the same crash if you cmd-a cmd-c in Safari, switch to mori, select the last entry in the entries view and then hit cmd-v?

My setup is Mori 1.3(64), OSX 10.4.7, Safari 2.0.4 on last generation TiBook for what its worth.

John

A few ideas

Hi, I had a few other thougths:

The way the script is set up now it expects a "Safari Webpages" folder, as in your original code. It works by pasting into the 'entries view' (so the paste goes in the "Safari Webpages"). The way it ensures that particular view has focus is to select the last entry in the "Safari Webpages" folder--of course if there are no entries in that folder it fails, so you might try creating the folder and putting one entry in it for testing. I don't know if this could be part of the problem.

Also, running it from the script menu within Mori has problems of its own (see the source) so I run it directly or thru the systemwide script menu.

This is just a hack--it scripts keyboard events to copy and paste. The catch with scripting keyboard events is that it matters which UI view has focus--a paste into each different view in mori does a different thing. It has the same issue in Safari--the focus needs to be in the root level, not in any text boxes. I don't know if there's a direct way to ensure a given view has focus from within applescript, so there clearly could be better ways to do this.

John

is there some way to make script menu spawn a different process

I've found a lot of scripts don't run well from there. My impression is that the applescript thread and the mori application thread don't play nice together.

like your script!

I didn't realize its so easy to simulate key events. So it seems that you get all the html and none of the css. A few notes:

1) If you don't have a Safari Webpages folder it creates one, however your script at present actually requires that folder to have an entry or it fails.
2)It is, as you might expect a bit unstable in that if you click another application while its running your web page gets pasted in there.
3) I don't know how to fix the css problem. If TextEdit has the css file embedded in the html will it render it?
4) Mori also crashed once for me.

what if you create a new entry and then select it

then it won't fail

Joe, Thanks for your

Joe, Thanks for your comments.

--I had realized the problem w/ an empty webpages folder. Your solution is right, but also cleaned up the code a bit so you could do something more robust (see below).

--I agree that having to flip application focus to Mori then back to Safari is problematic. If there was a way to paste into Mori without having to activate it it would be better. It may be possible by calling its methods directly as opposed to scripting UI events? I'm on the learning curve here.

--good question about css--does it work?

--I had a crash last night. Is this the same?


Thread 0 Crashed:
0 libobjc.A.dylib 0x90a3e0f8 objc_msgSend + 24
1 ...baysoftware.mori.MIDocument 0x002a59fc -[MINote setNoteTextStorage:] + 252
2 ...baysoftware.mori.MIDocument 0x0029be1c -[MIPasteboardController readEntryCopiesFromPasteboard:document:error:] + 2492
3 ...ftware.mori.MIUserInterface 0x01254918 -[MIEntryTreeController paste:] + 224
4 ...ftware.mori.MIUserInterface 0x0126403c -[MIEntryTreeControllerOutlineViewAdapter paste:] + 104
5 com.apple.AppKit 0x9378ac4c -[NSApplication sendAction:to:from:] + 108
6 com.apple.AppKit 0x937e54b8 -[NSMenu performActionForItemAtIndex:] + 392
7 com.apple.AppKit 0x937e523c -[NSCarbonMenuImpl performActionWithHighlightingForItemAtIndex:] + 104
8 com.apple.AppKit 0x937e4ce4 -[NSMenu performKeyEquivalent:] + 272
9 com.apple.AppKit 0x937e48c4 -[NSApplication _handleKeyEquivalent:] + 220
10 com.apple.AppKit 0x936ee408 -[NSApplication sendEvent:] + 2944
11 com.hogbaysoftware.Mori 0x00003670 -[MIMoriApplication sendEvent:] + 52
...


tell application "Mori"
activate
tell front document
if (exists entry named "Safari Webpages") then
set web_folder to entry named "Safari Webpages"
else
set web_folder to make entry with properties {name:"Safari Webpages", kind:"public.folder"}
set index of web_folder to 0 -- move to front of list
end if

set current entry to web_folder --puts focus of paste in source view

--paste (can we do this w/o activating Mori first?)
tell application "System Events"
tell process "Mori"
keystroke "v" using command down
--alternative:
--click menu item "Paste" of menu 1 of menu bar item "Edit" of menu bar 1
end tell
end tell

--if for whatever reason a new entry was not made (so current entry is still web_folder), skip the rest
if current entry ≠ web_folder then

--new entry was made at same level as web folder, so move it into web folder
move current entry to end of entries of web_folder

--label new entry with details of web page
set name of current entry to page_name
set comment of current entry to page_URL

else
beep 2
end if

end tell
end tell

as far as i know

you can't pass rich text through applescript methods. I have a feeling you are somehow using the clipboard to convert htlmt -> rich text. but i don't really know all that much either

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.