AppleScript to open group

DTP 3 and Hazel have worked great for me. I download regular documents such as bank statements, Hazel recognizes them, grabs the date, and then renames with the reconfigured name and date utilizing this AppleScript:

    tell application "Finder"
	set _path to (the POSIX path of theFile as string)
	set {_name, _extension} to {name, name extension} of theFile
	
	-- optional: hide extension in Finder
	set extension hidden of theFile to true
	
	-- optional: remove extension for DEVONthink displays
	set _name to text 1 thru -((count _extension) + 2) of _name
	set _extension to "." & _extension
end tell

-- Launch DEVONthink if it isn't already open.
tell application "System Events"
	if not (exists process "DEVONthink Pro Office") then
		tell application id "DNtp" to activate
	end if
end tell

-- Import the file.

tell application id "DNtp"
	set theGroup to get record with uuid "7A72BA27-C302-4362-8B25-01ACEC1C1F9A"
	
	-- choose one option, Index or Import, comment out the other
	set theImport to import _path name _name to theGroup
	-- set theIndex to indicate _path to theGroup
end tell

However, I have to go back to DTP 3 and find the group to confirm what was received. So I searched through the forums, found this: https://discourse.devontechnologies.com/t/applescript-to-go-directly-to-smart-group-tried-but-failed-is-it-possible/55461/6

and modified a bit, then added to the end of my current script:

    tell application "Finder"
	set _path to (the POSIX path of theFile as string)
	set {_name, _extension} to {name, name extension} of theFile
	
	-- optional: hide extension in Finder
	set extension hidden of theFile to true
	
	-- optional: remove extension for DEVONthink displays
	set _name to text 1 thru -((count _extension) + 2) of _name
	set _extension to "." & _extension
end tell

-- Launch DEVONthink if it isn't already open.
tell application "System Events"
	if not (exists process "DEVONthink Pro Office") then
		tell application id "DNtp" to activate
	end if
end tell

-- Import the file.

tell application id "DNtp"
	set theGroup to get record with uuid "7A72BA27-C302-4362-8B25-01ACEC1C1F9A"
	
	-- choose one option, Index or Import, comment out the other
	set theImport to import _path name _name to theGroup
	-- set theIndex to indicate _path to theGroup
end tell

tell application id "DNtp"
	set a to "x-devonthink-item://7A72BA27-C302-4362-8B25-01ACEC1C1F9A"
	open tab for URL a in viewer window 1
end tell

So now, the renaming and dating occurs, the file is moved to the correct group, but a new tab is added to the front window, showing that group.

My aim is to have this final step occur in a new window. In other words, after the file is moved into DTP, the group in DTP to which it was moved opens in a new window.

I’m guessing its pretty simple, and I am a simpleton for not figuring it out. But thanks for any help in advance!

To follow up:

I found this discussion:

https://discourse.devontechnologies.com/t/applescript-goto-group/53512

Which opens the group with the appropriate UUID (Hooray!).

However, I am looking to do the same thing in a NEW window, so as not to lose the windows I am working with.

Getting close…

See this thread

Out of curiosity: What is the advantage of using a UUID for the group instead of determining it by searching for database/group?
I’m asking because I have a similar process: Have Hazel rename account statements and then send them to the appropriate group. That’s for 8 accounts distributed over 2 databases and (obviously) 8 groups. I decided to go for the approach mentioned above because I find the code easier to read - but probably the UUID does have advantages, too?

The UUID doesn’t depend on the current name and location of an item.

To close the loop on my original question, @pete31provided an elegant solution in this thread:

AppleScript goto group?

Thank you, Pete!

1 Like