How to input text into Applescript evoked dialog


#1

In Devonthink Pro Office I want to select a record and move it to a specific Group.

An Applescript has been written that produces this dialog box

(obviously with text from my selected record) however when I evoke this script from within DTPO I can not use an insert text by pasting or typing action into the search string textbox even tho there is a flashing cursor there. Somehow KM cannot see the input text field.

Any ideas?

Here is the Applescript:

property pMove : "Move"
property pDup : "Duplicate"
property pRep : "Replicate"

property pDefaultVerb : pMove -- Default verb: may be switched to Duplicate (pDup) or Replicate (pRep) at run-time

-- ver 1.0      Allows double dot (..) as short-hand for parent group 
--            (to facilitate moving selected records up one level in the group hierarchy)
-- ver 0.9      Fixed a big involving search strings which include a space
-- ver 0.8      Clarified the Growl message: bold header: [Verb]d [N] records to:, body:[Destination]/n[List of records]
-- ver 0.7      Enhanced the "Group not Found" message for replication to remind user that replication
--            is only possible within a single database.
-- ver 0.6      The default verb (Move) can be overridden by prefixing the search string with "d " or "r "
--            (for "Duplicate" or "Replicate") in LaunchBar or the standard dialog
--            (a single verb letter followed by a single space before the search string itself)
-- ver 0.5      Can get the search string from LaunchBar
-- ver 0.4      Allows target databases (where their names match) as well as target folders
--            (the default incoming group of the database is used as the destination)

property pVer : "1.0"
property pTitle : "  to target group" & "    " & pVer

property pstrDelim : "    "

on run
   set lstSelns to GetDevnSelns()
   if lstSelns ≠ {} then
      set strNames to GetNameList(lstSelns)
      set strCmd to GetSearchString(pDefaultVerb, strNames)
      if strCmd ≠ "" then
         set {strVerb, strFind} to ParseCmd(strCmd)
         if strFind ≠ "" then ProcessSelns(lstSelns, strNames, strVerb, strFind)
      end if
   end if
end run

-- GET THE TARGET GROUP SEARCH STRING FROM LAUNCHBAR
on handle_string(strCmd)
   if strCmd ≠ "" then
      set lstSelns to GetDevnSelns()
      if lstSelns ≠ {} then
         set {strVerb, strFind} to ParseCmd(strCmd)
         if strFind ≠ "" then
            set strNames to GetNameList(lstSelns)
            ProcessSelns(lstSelns, strNames, strVerb, strFind)
         end if
      end if
   end if
end handle_string

on ParseCmd(strCmd)
   set {strVerb, strFind} to {pDefaultVerb, ""}
   set text item delimiters to space
   set lstParts to text items of strCmd
   if length of lstParts > 1 then
      set strFirst to first item of lstParts
      ignoring case
         if strFirst is in {"m", "d", "r"} then
            if strFirst = "m" then
               set strVerb to pMove
            else if strFirst = "d" then
               set strVerb to pDup
            else if strFirst = "r" then
               set strVerb to pRep
            end if
            set strFind to (items 2 thru end of lstParts) as string
         else
            set strFind to strCmd
         end if
      end ignoring
   else
      set strFind to strCmd
   end if
   {strVerb, strFind}
end ParseCmd

on ProcessSelns(lstSelns, strNames, strVerb, strFind)
   -- CHECK THAT A KNOWN VERB IS SPECIFIED AT THE TOP OF THE SCRIPT
   if strVerb is not in {pMove, pDup, pRep} then
      tell application id "com.apple.systemevents"
         activate
         display dialog "\"" & strVerb & "\": unknown value for pDefaultVerb" & return & return & "Expected Move | Duplicate | Replicate"
      end tell
      return
   end if
   
   tell application id "com.devon-technologies.thinkpro2"
      -- GET FILTERED SET OF POSSIBLE DESTINATIONS
      if strVerb ≠ pRep then
         -- BOTH OPEN DATABASES WITH MATCHING NAMES
         set lstParent to (incoming group of databases where name contains strFind)
         
         -- AND FOLDERS WITH MATCHING NAMES
         repeat with oDb in databases
            set lstParent to lstParent & (parents of oDb where name contains strFind)
         end repeat
      else -- OR, IN THE CASE OF REPLICATION, IN CURRENT DATABASE 
         set lstParent to (parents of current database where name contains strFind)
      end if
      
      -- ALLOW FOR DOUBLE DOT (..) AS REFERENCE TO PARENT DIRECTORY OF SELECTED ITEMS
      if strFind = ".." then set end of lstParent to first item of parents of (first item of lstSelns)
      
      -- BUILD NUMBERED MENU OF DESTINATION NAMES
      set lngParents to length of lstParent
      if lngParents > 0 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 strVerb & pTitle with prompt strVerb & ":
         
" & strNames & "

to target group:" 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
            
            -- MOVE | DUPLICATE | REPLICATE SELECTED ITEMS TO DESTINATION
            if strVerb = pMove then
               repeat with oRec in lstSelns
                  move record oRec to oTarget
               end repeat
            else if strVerb = pDup then
               repeat with oRec in lstSelns
                  duplicate record oRec to oTarget
               end repeat
            else if strVerb = pRep then
               repeat with oRec in lstSelns
                  replicate record oRec to oTarget
               end repeat
            end if
            
            -- NOTIFY RESULT BY GROWL (IF INSTALLED) OR WITH DIALOG BOX
            my Announce(oTarget, strVerb, (length of lstSelns), strNames)
            open window for record oTarget
            activate
         end if
      else
         if strVerb ≠ pRep then
            set strRepNote to ""
         else
            set strRepNote to "
            
(If you were attempting to replicate to another open database, note that DevonThink only supports replication within a single database)"
         end if
         tell application id "com.apple.systemevents"
            activate
            display dialog "No open groups contain the string:" & "
   
      " & strFind & strRepNote buttons {"OK"} default button "OK" with title strVerb & pTitle
         end tell
      end if
   end tell
end ProcessSelns

on GetDevnSelns()
   tell application id "com.devon-technologies.thinkpro2"
      return selection
   end tell
end GetDevnSelns

on GetSearchString(strVerb, strNames)
   set strFind to ""
   -- GET  SUB-STRING TO FILTER LIST OF TARGETS
   tell application id "com.apple.systemevents"
      activate
      set strFind to text returned of (display dialog strVerb & ":" & return & return & strNames & return & ¬
         "Enter search string to list target groups:" default answer "" buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel" with title strVerb & pTitle)
   end tell
   return strFind
end GetSearchString

on GetNameList(lstSelns)
   -- BUILD BULLETED LIST OF SELECTION NAMES
   set strNames to ""
   repeat with i from 1 to count of lstSelns
      set strNames to strNames & "• " & name of (item i of lstSelns) & return
   end repeat
   return strNames
end GetNameList

-- REPORT RESULT
(*
on Announce(oGroup, strVerb, lngRecords, strNames)
   tell application id "com.devon-technologies.thinkpro2" to set {strDb, strPath, strFolder} to {name of database, location, name} of oGroup
   set strGrowlTitle to strVerb & "d " & (lngRecords as string) & " records to:"
   set strGrowlBody to strDb & strPath & strFolder & "
   
" & strNames
   
   tell application id "com.apple.systemevents"
      -- USE GROWL IF IT'S RUNNING
      if (count of (every process whose name is "GrowlHelperApp")) > 0 then
         tell application id "com.Growl.GrowlHelperApp"
            register as application "houthakker scripts" all notifications {pTitle} default notifications {pTitle} icon of application "DEVONthink Pro"
            notify with name pTitle title strGrowlTitle application name "houthakker scripts" description strGrowlBody
         end tell
      else
         -- (OR USE A STANDARD DIALOG)
         tell application id "com.apple.systemevents"
            activate
            display dialog strReport & "
            
(This script works best if Growl is installed)" buttons {"OK"} with title pTitle
         end tell
      end if
   end tell
end Announce
*)

-- 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

#2

Hey There,

How exactly are you invoking the AppleScript?

You probably want to post the macro too.

Keyboard Maestro can see AppleScript dialogs just fine, but context can be fiddly. To figure out where things are going wrong for you we need to see exactly what you’re doing.

-Chris


#3

Hi Chris,

Super thank you for replying!

The macro is three actions. A “Execute AppleScript” with
"Execute text script" and “display results in a window” selected. I have the applescript that is included in this post pasted in the body of the “Execute Applescript” action. I am evoking it by clicking on macro in a palette within DTPO.

Here is the macro: (don’t know how to upload it into this thread. Uploaded via share button in KM then dragged and dropped from the new post to here. What is a better way to do that?)

http://forum.keyboardmaestro.com/uploads/default/original/2X/f/f97710996ccef8c901e8818bc8a209dae9610d43.kmmacros

Thanks again


#4

Chris will have the best advice for you, but if I were writing that macro now, I would probably give it a KM dialog rather than an Applescript one.

( And it might be tempting now to experiment with the new HTML-defined custom dialogs in KM 7)


#5

If I were script literate I’d gladly take your advice. This was written by houthakker over at the DTPO Forum.

THANK you for replying :grinning:


#7

Hi korm,

Thanks for your thoughtful reply!

Being nearly script illiterate, I’m naively thinking that if it were a KM macro, I’d be better able to modify it as needed and get more support here (since it’s unlikely that I’d be doing the modifying).

The script runs as expected in DT so there is no problem IF you want to use it as it was designed to be used. It’s a fine and useful script only I’m looking to use it in a simpler and more limited way.

Here’s my use scenario. I use ScanSnap to import 80-100 items/month that end up in receipt group for that month and year. As it is now, I must manually drag each item from the DB’s inbox to that Group. I’ve kludged my way around by using a simple drag and drop KM macro that picks up the item under the cursor and drops it into the current months group which is fixed in place in the favorites sidebar. This is functional and ugly and a nice step up from having to drag and drop each item.

This script does the same job and is way more flexible in selecting a group at the cost of requiring a bunch of keystrokes. Combining the script with KM I think I can just assign a single keystroke to have the item under the cursor be moved to the target group. Ideally DT would allow assigning a key combo to a list of favored groups to allow simple filing but alas, it doesn’t nor does it look like it will anytime soon.

This is really filing at it’s most basic and EVERY keystroke really counts.


#9

Hey Bern,

Actually that's probably the easy way to upload stuff, but you can drag and drop items from the Finder into the edit field of the Keyboard Maestro forum.

(I have a couple of macros to export images and .kmmacros files, so I can just drag-and-drop from the Finder — but I've used exactly the method you did above as well.)

--

Not knowing precisely what your script is supposed to do — and not having sample DEVONthink Pro databases in the correct configuration to test with — makes it a bit difficult to figure out, however let's look at the overall problem first.

Keyboard Maestro runs macros sequentially, so KM is waiting for the AppleScript action to be finished before moving on to the next action. This means your macro cannot work as constructed.

What I've done is alter the script so the first dialog is not needed. The entry for it is automatically "1", so the Keyboard Maestro insert text action is not needed. I've also removed some unused code.

Try this edited script, and see if it works.

BernSh ; DEVONthink Pro ; Keyboard Maestro Forum.scpt.zip (21.7 KB)

If it fails then I'll need for you to send me test databases for DEVONthink Pro that illustrate exactly what the script is supposed to do. I have a general idea, but real data samples make this sort of thing so much easier.

-Chris


#10

Hi Chris,

I'm going back to clean up the trail of incomplete items I've had in this forum. I see you answered me here and I dropped the ball. I apologize.

I'm no longer using DTPO and think it unlikely that I'll return to it.

I don't imagine this thread will be of use to anyone. Is there a way to archive or delete it?

Thanks, Bern


#11

Don't worry about it. You never know what some future reader will find useful. Doesn't consume enough storage space to worry about.