How can I find a file whose name is the same as name of other file?

Hello?

What I am trying to do is

  1. we have two files whose names are the same but have different extensions (a.pdf, a.bib, …)
  2. two files are in the same group (group A)
  3. one of the files is collected using smart group (a.pdf and other PDFs)
  4. using smart rule the file’s name is changed (a.pdf to b.pdf)
  5. — I’m stuck here! ----
  6. find the file in the group whose type is different (a.bib in group A)
  7. change the file’s name to another file (a.bib to b.bib like b.pdf)

Today is my first day to use of Apple Script and it is an interesting language.
But I cannot spend more time solving the problem…

Below is my trial :frowning:


on performSmartRule(theRecords)
	tell application id "DNtp"
		
		try
			set targetGroup to get record with uuid "x-devonthink-item://184702FC-0EE4-40E6-9156-D8E721E45FD7"
		on error error_message number error_number
		end try
		
		try
			repeat with thisRecord in theRecords
				
				set theName to name without extension of thisRecord
				display dialog theName
				
				set the name of thisRecord to "test"
				
				try
					display dialog (name of targetGroup as string)
					
					---display dialog (name of (the children of targetGroup whose (name is theName)) as string)
					try
						---set recTwo to (search "name:" & theName in targetGroup)
						---set recTwo to (search "name:bye" in targetGroup)
						---display dialog (name without extension of (children of targetGroup) as string)
						set recTwo to (the children of targetGroup whose (name without extension is theName))
						
						display dialog "hell0"
						display dialog (name of recTwo as string)
						
					on error error_message number error_number
					end try
					
					set name of recTwo to name of thisRecord
				on error
					
				end try
				
			end repeat
		end try
		
	end tell
end performSmartRule

I have tried some functions found here but it does not work.
Can I get some advice from you guys?

And I have one more question.
After I edit the smart rule, should I restart DT?
If I just run the rule without restart, it shows the same result as the previous one.

Thank you for reading!

Welcome @DDing

  • What are you actually trying to accomplish here? On the face of it, there’s nothing wrong with a.pdf and a.bib.
  • Where did you get the code?

After I edit the smart rule, should I restart DT?

No, that’s not necessary.

A screenshot of the smart rule would be useful. E.g. is it triggered on renaming or does it change the name on its own?

Here’s a simple, generic example how to find other items in the same group with the same name:

on run
	tell application id "DNtp" to my performSmartRule(selected records)
end run

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with thisRecord in theRecords
			set theGroup to location group of thisRecord -- Both items are in the same group
			set theUUID to uuid of thisRecord
			set theName to name without extension of thisRecord
			set theChildren to (children of theGroup whose name without extension is theName and uuid is not theUUID)
			repeat with thisChild in theChildren
				-- Process found items
			end repeat
		end repeat
	end tell
end performSmartRule

Thank you for your reply!

I hope to know how to find the record in other group using the name and get into the record variable

---set recTwo to (search "name:" & theName in targetGroup)
---set recTwo to (search "name:bye" in targetGroup)
---display dialog (name without extension of (children of targetGroup) as string)
set recTwo to (the children of targetGroup whose (name without extension is theName))			

After this code diag display "hello0does not work.
(I’m using this to check whether DT has reached the line since I don’t know how to debug in DT :frowning: )

I make the code from scratch.

I solved with code of @cgrunenberg

Thank you very much

Thank you for your reply

I used your code and solved!

I should replace (children of targetGroup whose (name without extension is theName))
to (children of targetGroup whose name without extension is theName)

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			set targetGroup to get record with uuid "x-devonthink-item://184702FC-0EE4-40E6-9156-D8E721E45FD7"
			
			repeat with thisRecord in theRecords
				
				set theName to name without extension of thisRecord
				set the name of thisRecord to "test"
				
				try
					display dialog (name of targetGroup as string)
					
					try
						set theChildren to (children of targetGroup whose name without extension is theName)
						
						set cnt to count of theChildren
						if cnt is not 1 then
							log message (theName & " is not exist")
							log message ("The number is " & cnt)
						else
							set thisChild to item 1 of theChildren
							set name of thisChild to name of thisRecord
						end if
						
						
					on error error_message number error_number
					end try
				on error error_message number error_number
					log message record theRecord info error_message
					
				end try
			end repeat
		end try
	end tell
end performSmartRule

Thank you very much

I do not really understand your code. What is this

set theName to name without extension of thisRecord
set the name of thisRecord to "test"

supposed to do? Basically, you save the current name of thisRecord in the variable theName. Then you change the same record’s name to “test”.

After that, you get all records in the same group with the same name (without extension) and check if there number is not one. Which makes no sense: At least thisRecord must be found, so the condition can never be matched.

See this output of Script Editor with a simplified version of your code:

tell application "DEVONthink 3"
	get record with uuid "x-devonthink-item://DCCA5514-E48D-4D8D-9524-80E665609BC8"
		--> content id 4724 of database id 3
	get name of content id 4724 of database id 3
		--> "Highlight in MD"
	get location group of content id 4724 of database id 3
		--> parent id 2.147483645E+9 of database id 3
	get every child of parent id 2.147483645E+9 of database id 3 whose name without extension = "Highlight in MD"
		--> {content id 4724 of database id 3}
end tell
Ergebnis:
{content id 4724 of database id 3 of application "DEVONthink 3"}

So, you always end up in the else branch of your if, where you set the name of the first record you found (which might well be the thisRecord, even if there are more than one identically named ones) to name of thisRecord, which is test.

I have no idea what you want to achieve, but this code doesn’t seem to do it. @cgrunenberg provided you with a useful way to find the records with the same name – why did you decide to ignore his code?

have you written your code with chat-gpt? this might explain why you are struggling so much to achieve your aim. better listen to the help here in the forum. chatgpt isn’t good enough to code for DTP up to now…

Yeap I know what you mean.

I used the examples from DT’s manual and functions here.

Thank you for your reply :slight_smile: