The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > OmniFocus > OmniFocus Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
A 'home page' toolbar button with a memory Thread Tools Search this Thread Display Modes
The code below (and the attached script version with icon) is a simple illustration of OF 1.8's new ability to conserve memory of state between successive runs of a script on the OF toolbar.

This example does two things:
  1. It acts as a 'Home Page' button, combining Perspectives > All Items with View > Show All Projects allowing you to jump back with one click to a window where all your data is in view (creating the window if necessary).
  2. It remembers the View Mode, Focus, Filter Settings, Selections, and Search Term of where you came from. If you click it a second time it foregrounds a window displaying that view (creating a new window if necessary).
In short, a sort of toggle between 'Home' and 'Away', using Applescript 'global' variables, which retain their value between runs, to remember the 'Away' settings.

(Intended to ease some of the friction involved in alternating between a focused view and a broader view of what you are up to.)

Code:
-- HOME page TOGGLE: a home button with memory (Clears focus and returns to default view, or returns to last remembered NON-HOME view)
-- Rob Trew ver 0.12

-- If the front window is does NOT show a broad "home page view",
-- jump to the first window that does (or create one if necessary).

-- If the front window DOES show a broad "home page view" then,
-- do we remember the last NARROWED view in which this button was used ?
-- if we do, jump to the first window that shows that narrowed view, or
-- create a new window in which to show that view.

-- ver 0.12 : fix for bug involving loss of reference to a window whose visibility is changed

property pblnRestoreSearch : true -- Restore any search term when returning to an 'Away' view ?

-- DEFINE THE HOME VIEW
--property pstrDefaultPerspective : ""  -- There is a BUG (OF 1.8) in the reading and clearing of the
-- 'perspective name' property, so we have to bypass the perspective system in this script entirely
property pstrDefaultViewMode : "project"
property plstDefaultFilters : {"remaining", "none", "none", "incomplete", "all", "any"}

-- DECLARE SOME VARIABLES WHOSE VALUES SHOULD PERSIST ACROSS RUNS OF THE SCRIPT
--global gstrPerspective
global gstrViewMode
global glstFocus
global glstFilters
global glstSideSeln
global glstContentSeln
global gstrSearchTerm

on run
    -- DO THE GLOBALS ALL RETAIN A VALUE FROM A PREVIOUS RUN ?
    try
        --gstrPerspective
        gstrViewMode
        glstFocus
        glstFilters
        glstSideSeln
        glstContentSeln
        gstrSearchTerm
    on error -- IF NOT, THEN SET THEM ALL TO DEFAULTS
        --set gstrPerspective to pstrDefaultPerspective
        set gstrViewMode to pstrDefaultViewMode
        set glstFocus to {}
        set glstFilters to plstDefaultFilters
        set glstSideSeln to {}
        set glstContentSeln to {}
        set gstrSearchTerm to ""
    end try
    
    -- GET A REFERENCE TO THE FRONT WINDOW (CREATING IT IF NOTHING THERE)
    tell application id "com.omnigroup.omnifocus"
        set oDoc to default document
        if (count of document windows of oDoc) < 1 then
            tell oDoc
                set oWin to make new document window
            end tell
        else
            set oWin to front document window of oDoc
        end if
        
        tell oWin
            if not visible then set visible to true
        end tell
        
        -- We need to get the reference again because switching visibility tends to lose it.
        set oWin to front document window of oDoc
        
        -- GET THE SETTINGS OF THE FRONT WINDOW
        tell oWin
            --set strPerspective to my GetPerspective(oWin)
            set strViewMode to selected view mode identifier
            set lstFocus to {}
            if my IsNarrowed(focus) then
                repeat with oObject in focus
                    set {cClass, strID} to {class, id} of oObject
                    set end of lstFocus to {cClass, strID}
                end repeat
            end if
            set lstFilters to my GetFilters(oWin)
            
            set blnSearchClear to true
            try
                set blnSearchClear to (search term = "")
            end try
        end tell
        
        -- IS THIS A HOME VIEW (AS DEFINED BY THE SCRIPT PROPERTIES) ?
        set blnHomeView to (strViewMode = pstrDefaultViewMode)
        --set blnHomePerspective to (strPerspective = pstrDefaultPerspective)
        set blnHomeFocus to (lstFocus = {})
        set blnHomeFilter to (lstFilters = plstDefaultFilters)
        set blnAtHome to (blnHomeView and blnHomeFocus and blnHomeFilter and blnSearchClear)
        
        -- IF THIS A HOME VIEW ...
        if blnAtHome then
            -- DO WE REMEMBER ANY NARROWER VIEW ?
            if (gstrViewMode ≠ pstrDefaultViewMode) or (glstFocus ≠ {}) or ¬
                (glstFilters ≠ plstDefaultFilters) or (not blnSearchClear) then
                
                -- IF SO, TRY TO RESTORE THE REMEMBERED NARROWER VIEW,
                -- EITHER BY FINDING A WINDOW WHICH DISPLAYS IT,
                set lstDocWins to document windows of oDoc
                set blnFound to false
                repeat with oWin in lstDocWins
                    if my matchesview(oWin, {gstrViewMode, glstFocus, glstFilters}) then
                        set oWin to my BringToFront(oWin)
                        set blnFound to true
                    end if
                end repeat
                
                -- OR BY RE-CREATING THE NARROWER VIEW IN A NEW WINDOW.
                if not blnFound then
                    tell oDoc
                        set oWin to make new document window
                    end tell
                    if selected view mode identifier of oWin is not gstrViewMode then ¬
                        set selected view mode identifier of oWin to gstrViewMode
                    my RestoreSelns(oWin, glstSideSeln, glstContentSeln)
                    my RestoreFocus(oWin, glstFocus)
                    my SetFilters(oWin, glstFilters, (gstrViewMode = "context"), "")
                    if pblnRestoreSearch then set search term of oWin to gstrSearchTerm
                end if
                
                if pblnRestoreSearch then set search term of oWin to gstrSearchTerm
            end if
        else -- IF THIS IS NOT A HOME VIEW
            -- RECORD THE SETTINGS WHICH WE READ EARLIER
            --set gstrPerspective to strPerspective
            set gstrViewMode to strViewMode
            set glstFocus to lstFocus
            set glstFilters to lstFilters
            -- AND A COUPLE OF ADDITIONAL ONES
            set oWin to front document window of oDoc
            tell oWin
                set glstSideSeln to {id, name} of selected trees of sidebar
                set glstContentSeln to {id, name} of selected trees of content
                set gstrSearchTerm to ""
                try
                    set gstrSearchTerm to search term
                end try
            end tell
            -- FOREGROUND ANY EXISTING HOME VIEW WINDOW
            set lstDocWins to document windows of oDoc
            repeat with oWin in lstDocWins
                if my matchesview(oWin, {pstrDefaultViewMode, {}, plstDefaultFilters}) then
                    set oWin to my BringToFront(oWin)
                    return
                end if
            end repeat
            -- OR MAKE A NEW HOME VIEW WINDOW IF NECESSARY
            tell oDoc
                set oWin to make new document window
                if selected view mode identifier of oWin is not pstrDefaultViewMode then ¬
                    set selected view mode identifier of oWin to pstrDefaultViewMode
                my SetFilters(oWin, plstDefaultFilters, (pstrDefaultViewMode = "context"), "")
                tell oWin
                    set focus to {}
                    tell sidebar
                        select tree 2
                    end tell
                end tell
            end tell
        end if
        
    end tell
end run

on RestoreFocus(oWin, glstFocus)
    using terms from application "OmniFocus"
        try -- glstFocus may not be defined
            set lstOldFocus to {}
            repeat with oObject in glstFocus
                set {cClass, strID} to oObject
                try -- (they might have been deleted in the meanwhile)
                    if cClass is project then
                        set oObj to project id strID of oDoc
                    else
                        set oObj to folder id strID of oDoc
                    end if
                    set end of lstOldFocus to oObj
                end try
            end repeat
            set focus to lstOldFocus
        end try
    end using terms from
end RestoreFocus

on SetFilters(oWin, {strSmartGroup, strGrouping, strSorting, strState, strFlagged, strDurn}, blnContextView, strComment)
    if blnContextView then
        set strClass to "-contexts"
    else
        set strClass to "-projects"
    end if
    tell application id "com.omnigroup.omnifocus"
        tell oWin
            if strSmartGroup is not "" then
                if strSmartGroup is not "-" then if selected smart group identifier of sidebar is not strSmartGroup then ¬
                    set selected smart group identifier of sidebar to strSmartGroup & strClass
            else
                set selected smart group identifier of sidebar to "all" & strClass
            end if
            tell content
                try
                    if strGrouping is not "" then
                        if strGrouping is not "-" then if selected grouping identifier is not strGrouping then ¬
                            set selected grouping identifier to strGrouping
                    else
                        set selected grouping identifier to "none"
                    end if
                on error
                    my CheckIdentifier("grouping", strGrouping, strComment)
                end try
                
                try
                    if strSorting is not "" then
                        if strSorting is not "-" then if selected sorting identifier is not strSorting then ¬
                            set selected sorting identifier to strSorting
                    else
                        if blnContextView then
                            set selected sorting identifier to "context"
                        else
                            set selected sorting identifier to "none"
                        end if
                    end if
                on error
                    my CheckIdentifier("sorting", strSorting, strComment)
                end try
                
                try
                    if strState is not "" then
                        if strState is not "-" then if selected task state filter identifier is not strState then ¬
                            set selected task state filter identifier to strState
                    else
                        set selected task state filter identifier to "all"
                    end if
                on error
                    my CheckIdentifier("task state filter", strState, strComment)
                end try
                
                try
                    if strFlagged is not "" then
                        if strFlagged is not "-" then if selected task flagged filter identifier is not strFlagged then ¬
                            set selected task flagged filter identifier to strFlagged
                    else
                        set selected task flagged filter identifier to "all"
                    end if
                on error
                    my CheckIdentifier("task flagged filter", strFlagged, strComment)
                end try
                
                try
                    if strDurn is not "" then
                        if strDurn is not "-" then if selected task duration filter identifier is not strDurn then ¬
                            set selected task duration filter identifier to strDurn
                    else
                        set selected task duration filter identifier to "any"
                    end if
                on error
                    my CheckIdentifier("task duration filter", strDurn, strComment)
                end try
            end tell
        end tell
    end tell
end SetFilters

on CheckIdentifier(strIdentifier, strSetting, strComments)
    display dialog "Unknown " & strIdentifier & " identifier: " & return & return & quote & strSetting & quote & ¬
        return & return & "Correct and try again." buttons {"OK"}
end CheckIdentifier

on matchesview(oWin, {strViewMode, lstFocus, lstFilters})
    using terms from application "OmniFocus"
        tell oWin
            if selected view mode identifier = strViewMode then
                
                if my GetFilters(oWin) = lstFilters then
                    set blnNarrowed to my IsNarrowed(focus of oWin)
                    set lngFocus to length of lstFocus
                    if lngFocus > 0 then
                        set lstWinFocus to {}
                        set iObj to 0
                        repeat with oObject in focus
                            set iObj to iObj + 1
                            if iObj > lngFocus then return false
                            set end of lstWinFocus to {class, id} of oObject
                        end repeat
                        return (lstFocus = lstWinFocus)
                    else
                        return not blnNarrowed
                    end if
                end if
                
            end if
            return false
        end tell
    end using terms from
end matchesview

on IsNarrowed(oFocus)
    using terms from application "OmniFocus"
        repeat with oObj in oFocus
            return true
        end repeat
        return false
    end using terms from
end IsNarrowed

on GetFilters(oWin)
    tell application "OmniFocus"
        set strSmartGroup to selected smart group identifier of sidebar of oWin
        set my text item delimiters to "-"
        set lstParts to text items of strSmartGroup
        set strSmartGroup to (items 1 thru ((length of lstParts) - 1) of lstParts) as string
        set my text item delimiters to space
        tell content of oWin
            {strSmartGroup, ¬
                selected grouping identifier, selected sorting identifier, ¬
                selected task state filter identifier, selected task flagged filter identifier, ¬
                selected task duration filter identifier}
        end tell
    end tell
end GetFilters

on BringToFront(oWin)
    tell application "OmniFocus"
        set lngWin to id of oWin
        set visible of oWin to false
        set oWin to window id lngWin
        set visible of oWin to true
        return window id lngWin
    end tell
end BringToFront

on RestoreSelns(oWin, glstSideSeln, glstContentSeln)
    tell application "OmniFocus"
        tell oWin
            -- Try to restore the remembered sidebar selection
            try
                tell sidebar
                    select {}
                    set {lstID, lstName} to glstSideSeln
                    repeat with iSeln from 1 to length of lstID
                        set strID to item iSeln of lstID
                        if strID is missing value then
                            set strTreeOne to "Library"
                            if gstrViewMode is "context" then set strTreeOne to "Contexts"
                            if item iSeln of lstName is strTreeOne then
                                select tree 2 with extending
                            else
                                select tree 1 with extending
                            end if
                        else
                            try
                                select descendant tree id strID with extending
                            end try
                        end if
                    end repeat
                end tell
            end try
            
            -- Try to restore the remembered content selection
            try
                tell content
                    select {}
                    set {lstID, lstName} to glstContentSeln
                    repeat with iSeln from 1 to length of lstID
                        set strID to item iSeln of lstID
                        if strID is missing value then
                            set strNAme to item iSeln of lstName
                            if name is not missing value then
                                set oTree to (first descendant tree where name is strNAme)
                                select oTree with extending
                            end if
                        else
                            try
                                select descendant tree id strID with extending
                            end try
                        end if
                    end repeat
                end tell
            end try
        end tell
    end tell
end RestoreSelns

Last edited by RobTrew; 2011-07-05 at 07:46 AM..
 
Well, storing and restoring perspectives should be easy, except that there seems to be a bug :-)

The perspective name of window property, nominally readable and writable, turns out to be unreadable in the current build (r137984) :-(

Code:
tell application id "com.omnigroup.omnifocus"
	set oDoc to front document
	set oWin to front document window of oDoc
	
	-- BUG: PERSPECTIVE NAME OF WINDOW
	-- This is meant to be a read/write property, and
	
	
	-- *writing* to the the property does work ...
	set perspective name of oWin to "flagged"
	
	-- but *reading* it does not ...
	set strName to perspective name of oWin
	
	-- (always yields missing value)
end tell
(Something for OF > Help Send Feedback)


--

Last edited by RobTrew; 2010-09-19 at 03:10 AM.. Reason: Suggested work-around deleted: only works in particular circumstances
 
Amended the above to correct restoration of filters. The script now also aims to restore selections, where possible.
 
This comes sooo close to something I've hankered for: a Search All toggle. I find the current Search implementation lacking because:
- it requires the toolbar to show icons, when I prefer the "text only" view. After activating Search, I have to reset the toolbar to text view.
- Along with seven perspectives, I've got several Rob Trew scripts in my toolbar, which is OK when I'm in text view, but when I activate icon mode the Search icon falls off the end of the toolbar.
- I have to use a perspective to get Search to Search All, and then return to my working perspectives after I've finished.

It's all rather clumsy. OF really needs a separate Search window. If we're going to stick our lives in this program, shouldn't we be able to get our lives back more straightforwardly?

In the interim, the only thing this script doesn't do to make Search a one-button process is to change the toolbar view mode to icon view.
I have no idea if this is possible: AppleScript is all Greek to me, or maybe Mandarin or Esperanto. If it is possible, any chance of inserting a line of code somewhere?

Thanks for all the applescripting. You've made OmniFocus a much more capable beast already.
 
Changing the toolbar state to activate icon display turns out to be difficult - scripts which which use System Events seem to work fine if launched from the Scripts menu, but freeze if they are launched from the toolbar.

Another approach, however, (see another post for a first draft of a one-click search), is to bypass the search bar and get the script to throw up a dialog into which you can type your search string directly, before it clears focus and filters and selects the Inbox and Library trees.

--

Last edited by RobTrew; 2010-09-12 at 09:40 AM..
 
The script in the first post has now been updated to restore Perspectives (as well as View Mode, Focus, Filter Settings, Selections, and Search Term) on the second click.

(The first click jumps to a 'Home Page' view in which all data is visible)
 
Now updated to Ver 0.11 (script and zip above - the zipped version has the icon).

Now faster, and perhaps more practical - toggles between two windows (home view and remembered narrow view) rather than just transforming the front window. (If either window is missing, it will create them).

Usage: install in toolbar and click it from a narrow view to return to one in which all is visible. (It will remember the narrow view, and return to it on the next click of the script).
 
Woah. Just.... woah. That's... woah... I didn't know you could... DO THAT via applescript.

(Thank you, in other words!)
 
:-) I'm glad it seems useful.

(the magic, however, is provided not by me but by the excellent enhancements in Applescript support in OF 1.8)

(I do hope that OF 2 has something like this built-in.
I think it would help new users a lot to have something simple and foregrounded which clears any narrowed view and reveals any hidden data).
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Group button on toolbar does nothing? zamzmith OmniPlan General 3 2009-08-28 11:37 AM
Projects button toolbar rogbar OmniFocus 1 for Mac 1 2009-07-11 11:54 AM
Launching OmniWeb brings up recent page, not designated Home Page jwthomas OmniWeb General 14 2008-05-29 09:55 PM
Launching OmniWeb brings up recent page, not designated Home Page jwthomas OmniWeb Bug Reports 0 2008-05-26 11:36 AM
Home button Davelabutte OmniWeb General 2 2007-07-26 11:42 AM


All times are GMT -8. The time now is 07:04 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.