find dead links

Is there a way to find (and thus delete) dead links?

I have a bookmark file in Devonthink and it is dead (the website no longer exists). There are many such files in DTPro and I am wondering if there is a way to search for bookmarks (or any link) that is dead?

Thank you.

Itā€™s not possible to search for them but a script can verify them. The following script replicates invalidate bookmarks to a group named ā€œInvalid URLsā€. In addition, both invalid and updated bookmarks are logged to Windows > Log. However, depending on the number of bookmarks this might require some time.


tell application id "DNtp"
	set theRecords to (contents of current database whose type is bookmark)
	if (count of theRecords) > 0 then
		show progress indicator "Checking Linksā€¦" steps (count of theRecords) with cancel button
		try
			repeat with theRecord in theRecords
				set theURL to (URL of theRecord)
				step progress indicator ((name of theRecord) as string)
				if theURL begins with "http" and theURL contains "://" then
					set theStatus to 0
					download URL theURL method "HEAD"
					try
						set theResponse to last downloaded response
						set theStatus to |http-status| of theResponse
						if theStatus ā‰¤ 0 or (theStatus ā‰„ 400 and theStatus is not 403 and theStatus is not 405 and (theStatus is not 404 or theURL does not contain "github.com/")) then error
						set theDownloadedURL to last downloaded URL
						if theURL is not equal to theDownloadedURL then
							set URL of theRecord to theDownloadedURL
							log message theURL info "Updated URL (" & theDownloadedURL & ")"
						end if
					on error
						log message theURL info "Invalid URL (HTTP " & (theStatus as string) & ")"
						set theGroup to create location "/Invalid URLs" in (database of theRecord)
						replicate record theRecord to theGroup
					end try
				end if
				if cancelled progress then exit repeat
			end repeat
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
		end try
		hide progress indicator
	end if
end tell

2 Likes

Thank you!

Now one more question if you have a moment.

How would I delete the original and the replicant?

By the way, did you create this recently or was this something you already had?

Just select the item and choose Data > Move All Instances to Trash (accessible while holding the Alt modifier key) or press its shortcut.

Itā€™s based on a similar script Iā€™ve been using for some time now.

Fantastic script! Thanks for sharing.

Ok, Iā€™m on shaky ground here and I need a little guidance. I created the script and it is working. Where are the broken links? Where are they located or how do I identify them? Thanks

Never mindā€¦ I just found the ā€œInvalid URLā€ group - thanks very muchā€¦

Nice gem.
Thanks @cgrunenberg!
Made it a Smart Rule on a weekly basis and added flagging to broken bookmarks
A ā€œmusthaveā€ script

DEVONthink 3 include already a similar script for smart rules, see action Execute Script > External > Check Links.

2 Likes

I canā€™t seem to find this Check Links script in the Script menu. Is it still there?

Itā€™s a script for smart rules (see Execute Script action) but not available via the Scripts menu.

Found it, thanks!

Although Iā€™m not sure what it means when it says itā€™s updated the URL.

if the original URL gets redirected, then the script sets the destination URL of the redirects.

1 Like

I must be missing something - I donā€™t see it in my options ?

image

Iā€™m using the latest version of DT.

My fault, itā€™s actually not part of the latest releases anymore as thereā€™s now a ā€œCheck Bookmarkā€ action. This action is a lot faster than the script and performed in the background.

Confirmed - I can see it now. The help file needs to be updated to reflect this.

Help > Documentation > Appendix > Smart Rule Events and Actions > Actionsā€¦

1 Like

Christian, Jim - thanks!

May I ask two questions related to this, please:

  1. the help docs say of that Action ā€¦Check Bookmarks: Checks the URL of the matched items. Any issues with the URL are reported in the Log window or the Log popoverā€¦. what exactly does that do? Anything similar to the above-mentioned scriptā€™s ā‰„ 400 and 403 but neither 405 yet neither 404 nor containing 'github.com/' etc? I added ā€˜Display alertā€™ steps both before and after checking and they display almost instantaneouslyā€¦ does ā€˜Check Bookmarksā€™ actually perform HTTP validation?

  2. is there a way to run such validation on webarchives?

Thanks!

  1. Yes, the script validates the URL by calling it and checking the response.
  2. Yes, this will check the URL listed in the Info pane.

Thanks, Jim! Great for #1. I should have known :slight_smile: .

But for #2 (webarchives), Iā€™m a little puzzled: I have a webarchive (in DT 3.7) whose correct URL is https://www.alamy.com. If I change the URL (in DT) to https://www.alamx.com (replace the final ā€˜yā€™ with an ā€˜xā€™) in Safari I get a 404 of course.

In DT I created a Smart Group (not necessary as I also do that in the Smart Rule itself) to look for any URLs containing ā€˜alamxā€™ (ā€œKind is Web Archiveā€ + ā€œURL contains alamxā€).

Select the resulting item in the Info pane ā†’ right click ā†’ Apply Rules:

But nothing seems to happen.

I know the answer will be simpleā€¦ :slight_smile: .