A script for rapidly sub-dividing the contents of a group.
Throughout the day I pause to send quick thoughts (in the form of text files) to a particular group.
The number of files in this group can get large, and I like to be able to do a quick triage of its contents into sub-groups, before moving the records on elsewhere.
If you select a DT group and run this draft script, it will loop through all the content records at the top level of the group, displaying each time the name of a record, and offering to move it into one of the existing sub-groups of that group.
(If there is no suitable sub-group, it can create a new-group, prompting you for a name, and moving the record into that new group).
I find that it allows for fairly rapid triage.
(Note that DT may not refresh its display of the group contents until you exit the script).
-- QUICKLY SUBDIVIDE THE CONTENTS OF THE SELECTED GROUP INTO SUB-GROUPS
property pTitle : "Triage: "
property pVer : "0.6"
property pstrOpen : "View contents"
property pstrNewGroup : "Make new sub-group"
property pTab : " "
on run
tell application id "DNtp"
set oFolder to current group
set strFolder to name of oFolder
-- LOOP THROUGH RECORDS OF GROUP, ALLOCATING THEM TO SUBGROUPS
-- AND CREATING NEW SUB-GROUPS ON THE FLY, WHERE NEEDED
set varChoice to true
set winView to missing value
repeat while varChoice
-- STOP IF THERE ARE NO MORE CONTENT RECORDS TO BE ALLOCATED TO SUB-GROUPS
set refChildren to a reference to (children of oFolder where kind ≠ "Group")
set lngChildren to count of refChildren
if lngChildren < 1 then return
-- CHOOSE THE NEXT RECORD TO DEAL WITH
set oRec to first child of oFolder where kind ≠ "Group"
-- LIST THE AVAILABLE SUB-GROUPS
set refGroups to a reference to (children of oFolder where kind is "Group")
set lstGroupNames to name of refGroups
set lngGroups to count of lstGroupNames
set lngDigits to (length of (lngGroups as string))
set lstIndex to {}
repeat with i from 1 to lngGroups
set end of lstIndex to my PadNum(i, lngDigits) & pTab & item i of lstGroupNames -- tab
end repeat
-- AND THE OPTION TO CREATE A NEW SUB-GROUPS
set lstIndex to {pstrOpen, pstrNewGroup} & lstIndex
if (count of lstIndex) > 2 then
set strDefault to item 3 of lstIndex
else
set strDefault to item 2 of lstIndex
end if
-- DISPLAY THE NAME OF THE CURRENT RECORD, AND GET THE USER RESPONSE
set varChoice to choose from list lstIndex default items strDefault with prompt "• " & (name of oRec) as string with title pTitle & strFolder
if varChoice is false then return
set strChoice to first item of varChoice
-- EITHER MOVE THE RECORD TO THE SELECTED SUB-GROUP
set oMoved to missing value
if strChoice ≠ pstrNewGroup then
if strChoice ≠ pstrOpen then
-- Move record to existing group
set {dlm, my text item delimiters} to {my text item delimiters, pTab}
set lngChoice to (first text item of strChoice) as integer
set my text item delimiters to dlm
set oGroup to item lngChoice of refGroups
set oMoved to move record oRec to oGroup from oFolder
try
if winView is not missing value then close winView
end try
else
set winView to open window for record oRec
end if
else -- OR MOVE IT TO A NEWLY CREATED SUB-GROUP
-- create a new group and re-enter loop
set strNewGroup to ""
tell (display dialog "New Group" default answer "" with title pTitle & strFolder)
set strNewGroup to text returned
end tell
if strNewGroup ≠ "" then
--oFolder
tell oFolder to set oNewGroup to create location (location) & (name) & "/" & strNewGroup in database
set oMoved to move record oRec to oNewGroup from oFolder
end if
try
if winView is not missing value then close winView
end try
end if
-- AND SELECT THE NEXT RECORD (IF THERE IS ONE)
-- BEFORE REPEATING THE LOOP
if oMoved is not missing value then
set refChildren to a reference to (children of oFolder where kind ≠ "Group")
if (count of refChildren) > 0 then
set oRec to first item of refChildren
else
exit repeat
end if
end if
set varChoice to true
end repeat
end tell
end run
-- NUMERIC STRING OF FIXED WIDTH - PADDED WITH LEADING ZEROS
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