AppleScript to check if file with same name exists in other database

This must be really simple, but I cannot figure it. Im trying to find whether a file with the same name already exists in a location in a database; ie:

  • does record /a/b/c/thenameofthefile in database 1 exist in database 2 in the location /a/b/c/

I know how to find out whether a record exists in /a/b/c/ in database 2, but not how to find out whether the specific file called “thenameofthefile” exists there. It must be blindingly obvious, but I can’t do it… so, please :smiley:

-- Check whether a record with the same name already exists in the same location in another database 

tell application id "DNtp"
	try
		set theRecords to selection of viewer window 1
		if theRecords = {} then error "Nichts ausgewählt."
		
		set thisRecord to item 1 of theRecords
		
		set theName to name of thisRecord
		
		if theName contains "/" then set theName to my replace_String(theName, "/", "\\/")
		
		set theLocation to location of thisRecord
		
		set theOtherdatabase to database id 1
		
		if (exists record at theLocation & theName in theOtherdatabase) then
			true
		else
			false
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on replace_String(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replace_String

not sure if this is the most efficient way, but I’ve just succeeded like so:

			set thePath to location of theRecord
			set fullPath to thePath & theName
			set theTest to get record at fullPath in database "Database 2"
			try
				set theID to id of theTest
			on error
				-- do various things
			end try

Excellent, thank you :slight_smile:

1 Like

No :roll_eyes:

DEVONthink escapes / in location automatically, but it of course doesn’t know what we want to do with the name.

So we need to take care of escaping the name in case it contains a /

I’ve edited the above script

1 Like

I’d just discovered that :smiley:

1 Like

:wink:

Haha, yes - I’ll let Bosch know…: “Bosch GSR|GSB Professional 14,4V-EC/18V-EC”

1 Like

Sorry, I’m going to have to ask for more help:

				set theName to name of theRecord
				set thePath to location of theRecord
				set theDatabase to database "Personal"
				set theFile to get record at thePath & theName in theDatabase
				set theGroup to parent of theFile
				set theTags to tags of theGroup

doesn’t work - I get Can’t get tags of {parent id 412701 of database id 3 of application “DEVONthink 3”}. What I’m trying to do is to get a list of tags for the group which contains the file theFile.

What I’m doing here is checking to see whether file a/b/c/afile in Database 2 is still tagged contained in a/b/c in Database 1, and whether /c is tagged.

Thanks - I’m not having trouble with the search routine per se; that works, I think. The problem is that I need to check whether the enclosing group in the database in which the document is also contained has a tag; and I can’t get a tag list for the enclosing group; I’m not even sure parent is the correct term…

OK. Irrelevant post removed.

noooo, that’s never necessary if you ask me - I love the tidbits of scripting - no reason to destroy knowledge, even if it’s not the solution to the problem in hand :slight_smile:

Weirdly that also doesn’t work; I’ve worked around the problem now by using the path, cutting off the final “/” and doing get record at the path, and then the tags from there. Why ever :smiley: Thanks for your input though!

You’ve used parent where it should be parent 1 as parents is a list.

This works set theGroup to parent 1 of theFile

As most often you won’t have further info about a specific parent (e.g. its name) to decide which one of the parents is the one you want I don’t like to use them.

If we however know e.g. the name, then we can ask directly (without using a repeat) for a specific parent

set thisParent to first parent of thisRecord whose name = "test"

and we can get the parent names directly like this

set theParentNames to name of parents of thisRecord

I tried that too, and had the same problem; I was able to work with theRecord as defined by the smart rule, but not with the file defined by getting the record at location/filename - I’ve discovered I need to completely rework what I was doing anyway, unfortunately. My script fails when there are documents with the same name in the source group, and won’t update files when they are changed. What do we learn from that: think first. Don’t script when tired. Ah well. Thanks for your help - I may have to come crawling back in a couple of days when I have rewritten things :wink:

Though I’m not totally sure what you’re up to or why, here’s a cursory stab at it…

tell application id "DNtp"
	set sel to (item 1 of (selection as list))
	set recordLocation to location of sel
	set recordName to name of sel
	set recordTags to (tags of (parent 1 of sel))
	
	tell database "File Types"
		set newLocation to (get record at recordLocation)
		set foundRecord to (search "name==" & recordName in newLocation)
		if (foundRecord ≠ {}) and ((count foundRecord) = 1) then
			set firstParent to (parent 1 of (item 1 of foundRecord))
			set parentTags to tags of firstParent
			if recordTags = parentTags then
				beep 2
			end if
		else
			display alert "More than one or no match found."
		end if
	end tell
end tell

Thanks one & all. What I am trying/going to do is to implement a one-way sync of selected groups and files to a separate database. My intention is to be able to make a selection of documents available to my wife (think insurance documents, contracts, instructions etc.). DT does not allow granular selection for sync. Whilst I could simply put those documents which I wish to share in a separate database, the risk of accidental deletion of a file rises with the number of users. My solution would copy appropriate files to a separate database, which I would then sync with my wife (well, not directly :rofl:). Any changes to those files, or deletion etc. would not sync back.

Of course, there is more to be considered here than I originally took into account. So it’s going to take more time than I thought. Time is somewhat constrained at the moment, but I’ll post back when I have solved it. Unless somebody posts a 3-line JS version that is :smiley: