Quickly find a group in any open DB [+LaunchBar operations]

With more than one open (and fairly complex) database, finding and opening a particular group can be slower and a bit more mouse-infested than it should be :neutral_face:

Edit > Find > In Database is a workable route, but unless you have already selected the target database you won’t get a hit from your search-string at all, and you still have to dbl-click the group if it does show up in the match list (even, in fact, if it’s the only match).

Do tell me if I am missing a quicker route. In the meanwhile, I have attached a key-stroke to the script below, which:

  • Needs no mice,
  • Searches across all open databases,
  • Jumps straight to the target folder if there is a unique match for the string you have typed,
  • Offers a full-path list of matching groups if there is more than one.
  • Can get its search string from LaunchBar
  • [EDIT - as of Ver 0.4, see posts below, can use LaunchBar’s Instant Send mode to copy files from the Finder straight to a group in DT]
  • [EDIT2 - as of Ver 0.5, if selected text picked up by LaunchBar Instant Send is a URL rather than part of a group name, the user is prompted for a group, and a bookmark for the URL is created in the target group]


property pVer : "0.6"
property pTitle : "Go to DT group" & "    " & pVer

property pstrDelim : "    "

-- Ver 0.6		Small fixes to standardize alert when no match is found.
-- Ver 0.5		If text selected by LaunchBar Instant Send matches no group and appears to be a URL, 
--				then make a BookMark record for this URL in the target group
-- Ver 0.4		Can use LaunchBar 'Instant Send' to copy files selected in Finder straight to target DT group
-- Ver 0.3		Avoids opening additional windows for the same group - checks for existing windows for target group before creating a new one
-- Ver 0.2		Can get its search string from Launchbar

-- SEARCH FOR DT GROUP BY STRING PASSED TO LAUNCHBAR 
on handle_string(strText)
	if strText ≠ "" then
		set lstMatches to GetMatches(strText)
		if lstMatches ≠ {} then
			OpenGroup(lstMatches)
		else if isURL(strText) then -- IF THERE IS NO MATCHING GROUP, AND THE STRING LOOKS LIKE A URL, PROMPT FOR GROUP AND MAKE A BOOKMARK
			set strFind to GetSearchString()
			if strFind ≠ "" then
				set lstMatches to GetMatches(strFind)
				if lstMatches ≠ {} then
					set oTarget to OpenGroup(lstMatches)
					if oTarget is not missing value then
						tell application id "com.devon-technologies.thinkpro2"
							set oRec to (create record with {name:strText, type:bookmark, URL:strText} in oTarget)
						end tell
					end if
				else
					WarnNotFound(strFind)
				end if
			end if
		else
			WarnNotFound(strText)
		end if
	end if
end handle_string

-- COPY FILES SELECTED WITH LAUNCHBAR INSTANT SEND TO  TARGET GROUP
on open lstArgv
	if class of lstArgv is list and lstArgv ≠ {} then
		
		-- PROMPT USER FOR SEARCH STRING (TO IDENTIFY TARGET GROUP)
		set strFind to GetSearchString()
		
		-- COPY FILES TO THE USER'S CHOICE OF TARGET GROUP
		if strFind ≠ "" then
			set lstMatches to GetMatches(strFind)
			if lstMatches ≠ {} then
				set oTarget to OpenGroup(lstMatches)
				if oTarget is not missing value then
					tell application id "com.devon-technologies.thinkpro2"
						repeat with oFile in lstArgv
							import (POSIX path of oFile) to oTarget
						end repeat
						activate
					end tell
				end if
			else
				WarnNotFound(strFind)
			end if
		end if
	end if
end open

-- SIMPLE USE (WITHOUT LAUNCHBAR) PROMPT USER FOR SEARCH STRING AND OPEN CHOSEN GROUP
on run
	set strFind to GetSearchString()
	if strFind ≠ "" then
		set lstMatches to GetMatches(strFind)
		if lstMatches ≠ {} then
			OpenGroup(lstMatches)
		else
			WarnNotFound(strFind)
		end if
	end if
end run

on GetMatches(strFind)
	tell application id "com.devon-technologies.thinkpro2"
		-- GET FILTERED SET OF POSSIBLE DESTINATIONS
		-- BOTH OPEN DATABASES WITH MATCHING NAMES
		set lstMatches to (root of databases where name contains strFind)
		
		-- AND FOLDERS WITH MATCHING NAMES
		repeat with oDb in databases
			set lstMatches to lstMatches & (parents of oDb where name contains strFind)
		end repeat
	end tell
	lstMatches
end GetMatches

on OpenGroup(lstParent)
	set oTarget to missing value
	tell application id "com.devon-technologies.thinkpro2"
		-- BUILD NUMBERED MENU OF DESTINATION NAMES
		set lngParents to length of lstParent
		if lngParents > 0 then
			if lngParents > 1 then
				set lngDigits to length of (lngParents as string)
				set lstMenu to {}
				repeat with i from 1 to lngParents
					tell item i of lstParent
						set strName to (name of its database) & its location & its name
					end tell
					if strName = "Inbox/Inbox" then set strName to "Global Inbox"
					set end of lstMenu to my PadNum(i, lngDigits) & pstrDelim & strName
				end repeat
				
				-- CHOOSE DESTINATION
				tell application id "com.apple.systemevents"
					activate
					set varTargets to choose from list lstMenu with title pTitle with prompt "Go to:" default items {first item of lstMenu} without multiple selections allowed
				end tell
				if varTargets is not false then
					
					-- 	RETRIEVE CHOSEN DESTINATION BY NUMERIC INDEX
					set my text item delimiters to space
					set oTarget to item ((first text item of (first item of varTargets)) as integer) of lstParent
					
					-- OPEN DT WINDOW FOR TARGET
					set lstWins to viewer windows where root is oTarget
					if length of lstWins > 1 then
						open first item of lstWins
					else
						open window for record oTarget
					end if
				end if
			else
				set oTarget to (first item of lstParent)
				set lstWins to viewer windows where root is oTarget
				if length of lstWins > 1 then
					open first item of lstWins
				else
					open window for record oTarget
				end if
			end if
			activate
		end if
	end tell
	return oTarget
end OpenGroup

on WarnNotFound(strText)
	tell application id "com.apple.systemevents"
		activate
		display dialog "No open Devonthink groups contain the string:" & "
	
		" & strText buttons {"OK"} default button "OK" with title pTitle
	end tell
end WarnNotFound

on GetSearchString()
	set strFind to ""
	-- GET  SEARCH STRING FROM USER
	tell application id "com.apple.systemevents"
		activate
		set strFind to text returned of (display dialog "Enter search string to find target group:" default answer "" with title pTitle)
	end tell
	return strFind
end GetSearchString

on isURL(str)
	return (str contains "://")
end isURL

-- GET A DIGIT STRING OF MINIMUM WIDTH (LEFT-PADDING WITH ZEROS WHERE NEEDED)
on PadNum(lngNum, lngDigits)
	set strNum to lngNum as string
	set lngGap to (lngDigits - (length of strNum))
	repeat while lngGap > 0
		set strNum to "0" & strNum
		set lngGap to lngGap - 1
	end repeat
	strNum
end PadNum

Wow, what a useful script! I’ve only played with it a couple of minutes now, but it looks like it could be one of my favorite DT additions. Rob, thanks for posting.

Good - I’m glad it seems useful.

I’ve just edited the code (above) to ver 0.2, which can get its string from LaunchBar.

(Get the script by its abbreviation/name with LaunchBar, then tap the space-bar to enter a search string).

That works, and that change also makes it work with Instant Send in LaunchBar.

Well spotted - that hadn’t occurred to me.

When I have a bit more time I’ll experiment with a LaunchBar route to moving selected files straight to a target group in DT - it should, I think, be possible as an edit to this script.

Ver 0.5 (at the start of this thread), can now use LaunchBar’s Instant Send to:

  • copy files selected in the Finder straight to the target DT group,
  • or create a bookmark in the target group from a URL selected in a browser or text editor.

http://www.obdev.at/resources/launchbar/help/InstantSend.html

Simple use: * Select the file(s) in the Finder,

  • hold down your LaunchBar hotkey a little longer than usual (till it switches to ‘instant send’ mode),
  • type your abbreviation for launching this script.

Do you mean copy?

Haven’t tried the script yet to answer that myself, but it looks interesting and a reason to practice using LB Instant Send more. :slight_smile:

Copy is indeed the word - not move :slight_smile:

(I’ll amend it above)

Thanks, houthakker. Obviously the difference between copy and move usually matters, probably more when anticipating the result will be the former and instead getting the latter. :slight_smile:

Thanks houthakker!

houthakker, your scripts have revolutionized the way I use DTPO, I can’t remember my work-flow before them, they are absolutely essential. I always wanted to be able to import/move/replicate items into/within DTPO via Launchbar, and now, thanks to this and other scrips of yours, I can. DEVONtechnologies should incorporate your scripts into the scripts they package with the application, they should be standard fare and are truly essential for LaunchBar users. Many, many thanks! ~ Jeremy

Hello houthakker,

This is the reason I am happy that I subscribe to the RSS feed for these forums. I saw jeremy’s post of gratitude ~ so even though your script was posted over 2 years ago ~ I was fortunate enough to find it. I agree with Jeremy ~ and I’ve not had the scripts for more than one hour but I can see they are amazing!

Sincerely,

Hi

Thank you for this great script. It helps a lot.

Hello there!
it looks fantastic! Do you think it could be adapted to work with Alfred instead of LB?

This might be of some use for Alfred users:

Thanks @lavaggio!