Alfred Workflow for DT 3.x

Awesome! Thanks so much. Just a couple of issues:

  • Can’t get search-by-tag working.
  • Some notes are opened as HTML files in my web browser rather than in DT3.

Hi. I downloaded that workflow thanks but for some reason the tag searches are not working. When I type dnm and a tag nothing happens. If I press Command it just says open with devonthink.

Hello. I wrote the Alfred workflow you’ve mentioned. I think you have been running an old version of the package. At some point, I made the changes for DT3+Alfred 4. The version in GitHub should now work correctly out of the box.

2 Likes

Hello, I’ve installed the workflow as per the instructions. Works really well with files, but when I click on or hit enter on a folder nothing happens. I am on DT3 and Alfred 4?

I am on Alfred 4.6.5 and DEVONthink Search Workflow version is 7.4.

For me dnm command is not working. In Alfred workflow debug mode I see error message /usr/bin/python: No such file or directory.

167258103-bd3bfad4-24d2-4574-98f3-e5c788920837

To further add…
I followed Alfred python 2 monterey help link to install Python 2.7.18 and made sure it is available in $PATH.

Below terminal commands show my Python version and install location:

% ls -ld /usr/local/bin/python
lrwxr-xr-x  1 root  admin  48 Mar 28 12:29 /usr/local/bin/python -> /Users/mars/.pyenv/versions/2.7.18/bin/python2.7

% ls -ld /Users/mars/.pyenv/versions/2.7.18/bin/python2.7
-rwxr-xr-x  1 mars  staff  2112256 May  7 11:56 /Users/mars/.pyenv/versions/2.7.18/bin/python2.7

% python -V
Python 2.7.18

% which python
/usr/local/bin/python

My other Alfred workflows that are Python dependent seem to work fine.

When you’re running code that explicitly starts Python in a location where it’s not installed, this is what must happen. That you have Python installed somewhere else has nothing to do with it.

Simply modify the workflow so that it runs Python from where it’s installed.

1 Like

Thank you for this suggestion.

After modifying path to /usr/local/bin/python the workflow works as expected.

1 Like

JFTR: Apple is discontinuing support for scripting languages (Python, Perl, PHP). So they have to be installed manually.

OTOH, I do have a python3 in /usr/bin. I suppose that it came with XCode, perhaps the command line tools or something like that. In the case of the Alfred workflow, that doesn’t help either because the workflow uses /usr/bin/python, not /usr/bin/python3. Always a good idea to not rely on external tools being installed, I guess.

I too have /usr/bin/python3 but no /usr/bin/python.

I had initially thought of adding /usr/bin/python as symbolic link to /usr/local/bin/python but because of macOS’s System Integrity Protection feature /usr/ is off limits.

According to the code on github and what Alfred shows me, the workflow should use python3 on macOS > 12.3.

But that’s neither here nor there – you should take up the problems with this workflow with its author. They’re on github, so you could raise an issue there. The question if and where Python is or isn’t installed is not related to DT, so it’s off-topic here.

1 Like

Indeed and this is why we avoid scripting additions (osaxen), etc., as much as possible. Dependencies are one more thing that can break, may have to be maintained, and may not be present on all machines. We work with what’s built in.

@chrillek and @BLUEFROG make good points about dependencies. Relying on workflows that connect two applications that progress through major versions separately, maintained by a single dev, is bound to eventually cause problems and disappointment. In this case, @mpco retired from further development in 2022. My recent upgrade to DT4b2 broke this Alfred workflow. Over the years, I greatly benefitted from it, and I was able to get it going again (under DT4b2 and Alfred 5.6.2.) with some trivial modifications. I eventually might run out of luck with that, but for now I can maintain my flow. I describe the fix in the next comment.

1 Like

TLDR

  • make this workflow work again under DT4b2 and Alfred 5.6.2.

Background info

  • I find this Alfred workflow very handy in a multi-desktop environment.
    • On whatever desktop I work, I can bring up the Alfred prompt and conduct a DT search, say dnt -name "jackson 2019",
      see the search results in Alfred, and open the file (in Previw/the system-wide preference, or, if holding down CMD, in the DT viewer).
    • This allows me to bypass switching to the full DT machinery for cases where I pretty much know what I’m looking for.
    • Assigning a hot key in Alfred speeds this up further.

Problem

  • Unfortunately, development ceased officially in 2022.
  • Did not work under DT4 anymore (tested under DT4b2).
  • The app name used in the workflow files is Devonthink 3.

Fix

  • If Devonthink 3 is replaced in all files (including the plist) by Devonthink, the workflow functions again (but see below for possible caveats).
  • I modified my own copy of the workflow script, in the directory where Alfred installed it (I believe that originally, a couple of years ago, I dragged the .alfredworkflow package onto Alfred prefs).
    • In Alfred prefs, go to the workflow panel, right click on this workflow and choose open in Finder.
    • Make a safety backup of the directory somewhere else
    • Replace Devonthink 3 by Devonthink in all files.
  • Ideally, one would put a DT4 compatible version on Github, but that would be up to the author, who professes to no longer having a Mac.

Caveats

  • I essentially only use dnt + keywords to search DT for a file via Alfred and bring it directly up, bypassing DT. This seems to work fine under DT4 now.
  • On the other hand, dnts + keywords (which starts a search in a DT window) seems to have an issue:
    • Works fine in an existing DT window (no modifier pressed); but that mode is not useful to me because I don’t want this search to change the state of another, open, DT window;
    • but using the CMD modifier to search in a new DT window, the keywords are not passed into that window;
    • using the Alfred debugger output, this seems to be an issue of using Applescript to navigate the DT GUI to put the keywords into the correct input field for a search — something for another day.
  • While we’re at caveats: dnt + keywords + CMD does not bring up the selected file in a new DT viewer window, if by accident an already open DT window lists that file. In that case one might think that the script is broken, but it isn’t.

I imagine someone will be along with a new version, the internet is full of nice people.

I use Alfred but I don’t use the dtns shortcut and I’m not on DT4 yet, so I’m not sure I will be much help. However, I wasn’t certain I understood the issue, please can you confirm if my understanding is right? (Just in case anyone else is unclear as well!)

When searching dnts + keywords + cmd, the intended behaviour is that it should be opening a new DT window and searching for your keywords. However, your experience is that it’s just opening a new DT window (good) and not passing the keywords over to the DT search (bad!)? (So the new window part of the command is working, but the keyword search isn’t?)

You’ve mentioned that the search is working fine if you don’t instruct it to open a new window first, so on the face of it (having not looked at it yet) it seems like it will be a minor error because we know it can work.

Correct. The workflow uses Applescript to issue cmd-opt n, which opens a new DT window. That works. But then the keywords don’t find their way into the search field.

I think I have figured it out now. Upon using dnts + keywords + cmd, Alfred generates and runs the script shown below. The debugger returns the error:
Can’t get group 3 of toolbar 1 of window 1 of process "DEVONthink". Invalid index. (-1719)
Well, in an utterly unscientific manner, I tried group 2 instead. Lo and behold this works. I’m not familiar with how elements in DT are numbered and how one finds out about it. But hey. Trying it several times, once it did not work. So I put a delay 0.5 between window creation and filling the search field. Have not had it fail since.

So in addition to replacing Devonthink 3 with Devonthink in all files of the workflow, one has to change group 3 to group 2 in the plist file. Note that this only affects the code for searching in new windows. Searching in an exisiting window does not use a mechanism like this. I have not bothered to find how/where that is accomplished in the workflow.

Anyway, scripts are meant to be quick and dirty, so as long as this works, I am content.

on run argv
	set theQuery to item 1 of argv
	set theNewQuery to (do shell script "echo '" & theQuery & "' | perl -CIOED -p -e 's/(\\p{Block=CJK_Unified_Ideographs}+)/ ~\\1 /g'")
	tell application "DEVONthink" to activate
	tell application "System Events" to tell process "DEVONthink"
		keystroke "n" using {command down, option down} -- open new window
		set focused of text field 1 of group 3 of toolbar 1 of window 1 to true -- focus on search field
		set value of text field 1 of group 3 of toolbar 1 of window 1 to theNewQuery
		delay 0.1
		keystroke return
	end tell
end run

Yes to the first, no to the second :wink:

It’s a long time that I looked at that workflow, but I don’t think that UI scripting is required to search for keywords in DT. DT has a search scripting command.
And unless you’re in a CJK environment there’s no need to call perl as the script does.

there’s no need to call perl as the script does

Yeah, I noted that, too. The workflow combines Javascript, Python, Applescript, Perl, shell (and maybe more). Personally, I’d like to see more Prolog in there. :rofl:

But hey, I’m not complaining. If I did, I should write my own. Don’t have the time, as I would have to get up to speed with a lot of things first. If my kludges take me through DT4 and Alfred 5, I might make it to retirement. After that, no need for shortcuts anymore, I will have all the time in the world.

Instructions/files for running the AlfredWorkflow-DEVONthink-Search workflow under DT4 and Alfred 5.

I post a zip archive with relevant files that go into the workflow. I am not sure how Alfred installs a workflow, so likely it won’t work to just move my modified files into the Alfred workflow location if you have not already installed the original one.

  1. Install workflow from Github, if not already done.
  2. In Alfred settings, go to Workflows and right click “open in Finder”.
  3. Swap out the files from the attached archive (not all of them were modified).
  4. Trust that these files contain no backdoor. Otherwise, make the changes described in the posts above on your own. In short: replace Devonthink 3 in all files with Devonthink and replace in the plist file group 3 with group 2.
  5. No claim of fitness, no liability, do not use for the operation of nuclear power plants.

Archive.zip (69.7 KB)

I don’t know anything about Alfred workflows, but the UI scripting seems unneccesary.

I haven’t looked at DT4’s scripting dictionary, but DT3 viewer windows have a search query property. Setting it performs a search in the window (which is different from the search command). Besides that you have the commands open window for and open tab for.

I would guess something like this is still possible in DT4:

-- Open document window for a record:
set theRecord to -- however you get the record
open tab for record theRecord

-- Open record in new viewer window, even if it's already open:
set newWindow to open window for record (root of current database) with force
open tab for record theRecord in newWindow

-- Perform search in new window:
set theQuery to -- however you get the query
set newWindow to open window for record (root of current database) with force
set search query of newWindow to theQuery
1 Like