The shared bits

Add To Yojimbo with AppleScript

I’ve started using Yojimbo more as an Anything Bucket and I wanted an easy way to get stuff into it. I found several AppleScripts and they were all good, but they were for individual applications. I try to minimize how much I have to think about routine stuff – I’d much rather have code that just “does the right thing” so I set out to meld the scripts into a single one.

I’m quite happy with the results. I’ve got a single shortcut setup (⌘⌥Y) in FastScripts and I can use it in any of the apps that I need to capture from (Mail, Safari, NetNewsWire).

Direct download link

(*

Add To Yojimbo Script

Matt Vanderpol
1.0 - 2010-02-18

Collects tag info and creates a new web archive or note in Yojimbo. Can cancel from tag
collection screen to cancel action.

Currently works from:
  NetNewsWire - creates web archive
  Mail - creates note
  Safari - creates web archive
 
Notifies via growsl on success or failure.

Based on work by:
    John Gruber (Create Yojimbo Bookmark From NetNewsWire - http://daringfireball.net/misc/2007/05/nnw-to-yojimbo-bookmark)
    Jom Correia (Create Yojimbo Note From Apple Mail - http://www.listsearch.com/Yojimbo/Thread/index.lasso?342#2169)
    Jim DeVona (Bookmark in Yojimbo - http://anoved.net/software/bookmark-in-yojimbo/)
   

*)


global gScript, gApplication

-- From: http://daringfireball.net/2009/01/applescripts_targetting_safari_or_webkit
on GetCurrentApp()
    tell application "System Events"
        set _app to item 1 of (every process whose frontmost is true)
        return name of _app
    end tell
end GetCurrentApp

on GetTags()
    -- prompt for tags
    set _tags to {}
    set _dlog2 to display dialog "Set tags (if any) for web archive:" default answer ¬
        "" default button 2 cancel button 1
    set _action2 to the button returned of _dlog2
    if _action2 = "" or _action2 = "Cancel" then
        tell application "GrowlHelperApp"
            notify with name ¬
                "Failure Notification" title ¬
                "Cancel Add" description "User cancelled adding item to Yojimbo" application name gScript
        end tell
        error number -128
    else
        set _answer to text returned of _dlog2
        if _answer is not "" then
            set AppleScript's text item delimiters to ", "
            set _tags to text items of _answer
        end if
    end if
    return _tags
end GetTags

on GenerateMessageText(m)
    tell application "Mail"
        set _sender to sender of m
        set _subject to subject of m
        set _date to date received of m as string
        set _contents to content of m
        set _messageString to "From:" & tab & _sender & return
        set _messageString to _messageString & "Subject:" & tab & _subject & return
        set _messageString to _messageString & "Date:" & tab & _date & return
        set _messageString to _messageString & return & return & _contents
    end tell
end GenerateMessageText

on AddWebArchive(h_URL, _tags, _title)
    tell application "Yojimbo"
        set _new_item to (make new web archive item with contents h_URL)
        add tags _tags to _new_item
    end tell
   
    tell application "GrowlHelperApp"
        notify with name ¬
            "Success Notification" title ¬
            "Import Success" description "Successfully imported \"" & _title & ¬
            "\" to Yojimbo" application name gScript
    end tell
end AddWebArchive

on AddNote(_contents, _name, _tags)
    -- create the new note
    tell application "Yojimbo"
        set _new_item to (make new note item with properties {contents:_contents, name:_name})
        add tags _tags to _new_item
    end tell
   
    tell application "GrowlHelperApp"
        notify with name ¬
            "Success Notification" title ¬
            "Note Created" description ¬
            "" & _name & "" application name gScript
    end tell
end AddNote

on run
    set gApplication to GetCurrentApp()
    set gScript to "Add To Yojimbo Script"
   
    tell application "GrowlHelperApp"
        set the allNotificationsList to {"Success Notification", "Failure Notification"}
        set the enabledNotificationsList to {"Success Notification", "Failure Notification"}
       
        register as application ¬
            gScript all notifications allNotificationsList ¬
            default notifications enabledNotificationsList ¬
            icon of application gApplication
    end tell
   
    if gApplication is "NetNewsWire" then
        set _tags to GetTags()
        tell application "NetNewsWire"
            try
                if (index of selected tab is not 0) then
                    set _tabNum to index of selected tab + 1
                    set _tabURLs to URLs of tabs
                    set h_URL to (get item _tabNum of _tabURLs)
                    set _tabTitles to titles of tabs
                    set _newItemTitle to (get item _tabNum of _tabTitles)
                    my AddWebArchive(h_URL, _tags, _newItemTitle)
                   
                else if exists selectedHeadline then
                    set h_URL to URL of selectedHeadline
                    set _newItemTitle to title of selectedHeadline
                    my AddWebArchive(h_URL, _tags, _newItemTitle)
                   
                else
                    error "No headline is selected."
                end if
            on error error_message number error_number
                tell application "GrowlHelperApp"
                    notify with name ¬
                        "Failure Notification" title ¬
                        "Import Failure" description "Failed to import \"" & newItemTitle & ¬
                        "\" to Yojimbo.  Error: " & error_message application name gScript
                end tell
            end try
        end tell
    else if gApplication is "Mail" then
        set _tags to GetTags()
        tell application "Mail"
            tell message viewer 1
                set _messageList to selected messages
                set _name to subject of item 1 of _messageList
                set _contents to ""
                repeat with m in _messageList
                    set _contents to _contents & my GenerateMessageText(m)
                end repeat
               
                my AddNote(_contents, _name, _tags)
               
            end tell
        end tell
    else if gApplication is "Safari" then
        set _tags to GetTags()
        tell application "Safari"
           
            -- identify the page to create web archive for, if any 
            try
                set h_URL to the URL of document 1
                set _pageTitle to the name of document 1
            on error
                return
            end try
           
            my AddWebArchive(h_URL, _tags, _pageTitle)
        end tell
    else
        tell application "GrowlHelperApp"
            notify with name ¬
                "Failure Notification" title ¬
                "Unknown Application" description "Don't know how to add to Yojimbo from " & gApplication ¬
                application name gScript
        end tell
    end if
   
end run

Categories