File path

I am trying to right a script which obtains the file path of the selected document and saves it to the clipboard and then use this link as a hyperlink to the file from excel. I have tried the following script but I cannot get it to work.

tell application "DEVONthink Pro"
	try
		set theSelection to the selection
		if theSelection is {} then error "Please select a record"
		set filepath to the path of theSelection
		
	end try
	
	set the clipboard to filepath
end tell

Where am I going wrong?

Try this. Selection is a record and has no path of its own. You need to get the path of the first item in the record.

tell application id "com.devon-technologies.thinkpro2"
	try
		set theSelection to the selection
		if theSelection is {} then error "Please select a record"
		set filepath to the path of the first item of theSelection
		set the clipboard to filepath
	end try
	
end tell

Just curious, but what will this script allow you to do that cannot already be done by using the Open command from within DEVONthink?

Looks like the OP wants the path of a document on the clipboard to paste somewhere else. I’m not sure how that intersects with Open?

I don’t know if the two do intersect-thus my question.

Thank you for your answer.

The reason I want the code is to attach the file path of the devonthink document to an excel hyperlink. The excel hyperlink path needs to start with “file://localhost” and then all spaces in the link replaced with “%20”. The final link should look something like this:

“file://localhost/…/new%20document.doc”

By having this hyper link in excel, I can click on a cell and be taken to the document to which the data refers. I am experimenting with ways to make my research easier to follow and this seems to be a good solution.

I have amended the code to produce this type of link but I still can’t get it to work. The replace text part gives an error number -1708. I have used the same replace text code in another script and it seems to work without any problems.

Here is the amended code:

I am sure that the error is something simple. Any ideas?

I usually use a shell script to do URLencoding. Search over at macscripter.net for “shell urlencode” and you’ll find several simple methods to do that.

Have you tried the x-devonthink-item:// link? In scripting, this is the Reference URL of an item.

The x-devonthink-item:// link? does not seem to work within an excel file hyperlink.

I will look at macscripter for shell url encode.

The script that I posted should work because I can use the following script to get the filepath when I have located the devonthink file in Finder.

on replaceText(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replaceText

tell application "Finder"
	set thePath to POSIX path of (the selection as alias)
end tell
set linkpath to get replaceText(" ", "%20", thePath)

set linkpath to "file://localhost" & linkpath
--display dialog linkpath

tell application "Finder" to set the clipboard to linkpath

I don’t understand why I get the error message when I try and pass the filepath from the devonthink script posted earlier.

For anyone that is interested, I have found a solution to my script error. I just needed to put a “my” statement in front of the call to the subroutine. Here it is:

on replacetext(find, replace, subject)
	set prevTIDs to text item delimiters of AppleScript
	set text item delimiters of AppleScript to find
	set subject to text items of subject
	
	set text item delimiters of AppleScript to replace
	set subject to "" & subject
	set text item delimiters of AppleScript to prevTIDs
	
	return subject
end replacetext

tell application id "com.devon-technologies.thinkpro2"
	try
		set theSelection to the selection
		if theSelection is {} then error "Please select a record"
		set filepath to the path of the first item of theSelection
		
	end try
	set filepath to get my replacetext(" ", "%20", filepath)
	set the clipboard to "file://localhost" & filepath
end tell

I second the principle that it is usually much quicker to search online for a shell solution than to try to get a reinvented applescript wheel to work.

In addition to better development speed, shell solutions tend to be more general and more robust.

Something like the following, for example, should handle not only spaces, but most of the other slings, arrows and exotic characters that outrageous fortune can throw at it.

on encode(strPath)
	do shell script "python -c 'import sys, urllib as ul; print ul.quote(sys.argv[1])' " & ¬
		quoted form of strPath
end encode

I haven’t used shell scripts but am eager to learn if they are better than just using applescripts. Would you mind explaining exactly what your script does.

Many thanks

It just calls an existing function (for quoting/encoding urls) from a standard Python library.

In an AppleScript your usage would be something like


set encodedFilepath to my encode(filepath)

Christian has noted that the organization of files within the Files.noindex folder is dynamic.

If I understand correctly, one cannot rely on using the path to a file in that folder.

That’s an important point, and I think that the real problem here is with Excel, which won’t directly process DT links of the kind yielded by Edit > Copy Item Link or applescript equivalents like:

tell application id "DNtp"
	set lstSeln to selection
	if length of lstSeln > 0 then ¬
		set the clipboard to (reference URL of first item of lstSeln) as string
end tell

(Excel is fairly obdurate in its insistence on appending a file://localhost prefix to such links).

One solution would be to paste plain text DT links of the form x-devonthink-item://4B19AD66-4962-4261-A7D8-DCC9335C7E36 into Excel cells.

You could then run an applescript like the following from Excel when you wanted to open such a link:

tell application id "XCEL" to set strURL to string value of selection
if strURL begins with "x-devonthink-item://" then
	do shell script "open " & quoted form of strURL
	tell application id "DNtp" to activate
end if