Tinderbox Forum

Replacing picture links in imported markdown file

Hello!
I recently converted all my topic notes to markdown in order to move them tinderbox.
And it went smoothly, now I have a tinderbox file containing 854 fairly voluminous notes and I plan to replenish it.
Because a number of my notes contained pictures and were originally written in rtf, I used AppleScript in DEVONthink to convert them to markdown. Accordingly, all the pictures were moved to the appropriate folders in the DT, and links appeared in the notes instead.


But the links are not to the file path, but to the devonthink item, which works fine in DT, but doesn’t work in Peview in TB.
I have an idea how to fix this:
I moved all the pictures to a folder in mac.

I can use “Create Metadata Overview” to get a table with the file name and its “item link”.
Screen Shot 2021-04-30 at 22.03.44

And my question is how to automatically change the “item link” in the notes to the link to the file with the corresponding name in the folder so that in Preview TB the corresponding images will be displayed.

This should be pretty straightforward, fingers crossed. The key is to figure out where the absolute path is for each of the images. Hopefully, they’re all in the same directory, but if not we can work with that too. But first, we need to understand where the images are on your drive, their path. Once we know where the images are we can use a .replace stamp to stick the path to the images into Tinderbox and then you’ll be able to see and export them.

Before I work on the answer for you, however. I have a few questions.

  1. What is a “topic” note? Is this some other application? What is the original source? HTML? Something else?
  2. Can you share a link to the AppleScript you used to do the conversion? I’d like to reproduce your experience.
  3. Can you right mouse click on one of the files and say “Show in Finder”? Then in the Finder click View>Show Path Bar if it is not already selected. On the bottom of the Finder you’ll see the path to your files. If you click on one of the images, rt. mouse click on the file name on the far right of the path list you can then select copy path name. This will give us the path of the image. If they are all in the same directory, then we’re good to go. I can show you the next step.

Let’s step there for now. Depending on the answers above we can figure out the next step.

1 Like

Dear Michael.
The “topic” note is just notes about my research (my level of English is probably to blame here). Initially, they were in “rft” and were kept in “DT”.
Attached APPLESCRIPT.
I put all the images in one folder. Here is the path: /Users/maksimsmirnov/Documents/Tinderbox/Images/BaseDate/0ac0e808177870b27c42dceadbd28ea0229c1d1a.png

Convern RTFtoMK.zip (16.5 KB)

Is it possible to have the export script provide the path to each image file rather than the reference url?

Trying to grab the desired data at the source might be more efficient than trying to do all those substitutions in Tinderbox. Just a thought.

SG

1 Like

Dear Sumner Gerard.
Good idea. To be honest, I don’t know.
I’ll try to look it up.

Thanks. I’m curious, where did you find the “RegexAndStuffLib” library for the AppleScript? I don’t seem to have it and can’t find it.

1 Like

That would be idea, yes. Then we could make the whole process a lot easier. I can’t seem to get the script running, though.

1 Like

Sorry for the late report, I have a different time zone.
Here’s the library.
RegexAndStuffLib_stuff.zip (64.7 KB)

Hi, author of the script here. Your idea would work but it’s no good way to go, I think.

It’s far easier to

In Finder:

  • create an empty folder. That folder is used to store the images that will be linked in Tinderbox

In DEVONthink:

  • index that folder (menu File > Index ). Let’s call it “My indexed images”

  • create a Smart Group that searches for images in the script’s output groups
    (In case you didn’t use a top level group that holds all of the script’s output: Move all output groups to a new group (menu Data > Group Items) as we need a top level group for the Smart Group's search scope.)

  • move the Smart Group's results to the “My indexed images” group
    This turns them into indexed records

This way we can act on the DEVONthink records instead of messing around with records in DEVONthink and files in Finder.

Then write an AppleScript that

  • iterates over each paragraph of the Markdown record

  • uses a regex to find the x-devonthink… item link(s) in a paragraph

  • uses the found x-devonthink… item link to create a HTML link with the image record’s URI:

    -- Example: Get basic HTML link
    
    use AppleScript version "2.4"
    use framework "Foundation"
    use scripting additions
    
    tell application id "DNtp"
    	try
    		set theRefURL to "x-devonthink-item://05F79AC2-9A58-430C-A144-F9434B0DFE91" -- regex's result
    		set thisImageRecord to (get record with uuid (characters 21 thru -1 in theRefURL) as string) -- get the image record
    		set thisImageRecord_Path to path of thisImageRecord -- get the image record's path
    		set thisImageRecord_URI to my toURI(thisImageRecord_Path) -- get the image record's URI
    	
    		set theReplacementText to "<img src=\"" & thisImageRecord_URI & "\">"
    	
    	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 toURI(thePath)
    	try
    		set theURL to current application's |NSURL|'s fileURLWithPath:thePath
    		set theURI to theURL's absoluteString()
    		return theURI as string
    	on error error_message number error_number
    		activate
    		if the error_number is not -128 then display alert "Error: Handler \"posixToURI\"" message error_message as warning
    		error number -128
    	end try
    end toURI
    
    
  • replaces the found x-devonthink… item link with the HTML image link.

Afterwards

  • Export the Markdown records
  • Delete the indexed group in DEVONthink - make sure to only delete it in DEVONthink, not in the Finder

I didn’t test it but this should work :slight_smile:

1 Like

Thanks, Pete. If I understand hwat you’re doing here you’re looking to get all the image assets in one common path, correct? if so, that is great. Once we have that we can then have Tinderbox point to all the images in the common path, as I explain in my various working with media videos.

1 Like

@maksim I’ll wait to see if you make progress with @Pete’s suggestions before weighing in again.

1 Like

Yes.

If you use the example script and extend it with e.g. HTML height etc. then the links are ready, there’s nothing more to do in Tinderbox, I think.

But I’m still experimenting with linked images myself, so it would probably be good if you keep an eye on @maksim’s link implementation :slight_smile:

2 Likes

I tried your script on one file, but nothing worked.


Maybe I did something wrong?

It’s not a full script, you need to

If you take a look at the original script you’ll find everything you need to write a script that does the steps I outlined above.

If you don’t get a working script and no one else wrote it for you then I’ll do it in the next days.

1 Like

I apologize for my misunderstanding.
I’ll try, but I’m a complete zero at writing scripts.

@maksim I got lost in the sophisticated DEVONthink scripts by @Pete so here is a more pedestrian attempt to solve the problem on the Tinderbox side. It has no dependencies like RegexAndStuff. This assumes:

  1. You already have notes in Tinderbox with DEVONthink reference urls within Markdown style links in the text as shown in your screenshot.
  2. You have all your image files in one folder on your Mac.
  3. You have dragged the Metadata Overview.tsv file from DEVONthink into that same folder (together with the images).
  4. The Metadata Overview.tsv has the name of the image file in its first column and the DEVONthink reference URL in its fifth column (as in your screenshot).
  5. The image file names in the first column include the file extension .png, .jpg, .gif, etc. (I can’t tell from your screenshot.) This is important. On my machine DEVONthink does provide those extensions in Metadata Overview.tsv. If it doesn’t on yours, then additional scripting will be needed to restore the extensions.)

I suggest duplicating your document and testing by selecting a few notes containing in their text DEVONthink reference urls you want to convert. Click [run] and at the prompt navigate to and choose the Metadata Overview.tsv file you have saved in the same folder with the images.

Before:

After:

The script will automatically substitute the path on your machine, i.e. it will have your user name and instead of sg3, and the path to your chosen folder for the images.

The Script


set tsvFile to choose file with prompt "Choose TSV Metadata file"

tell application "Finder" to set pathToFolder to POSIX path of (container of tsvFile as alias)
set urlPathToFolder to "file://" & pathToFolder -- results in file:///path/to/

set dtref2imgnamPairs to readValsFromTSV(tsvFile)

tell front document of application "Tinderbox 8"
	repeat with aNote in selections
		tell aNote
			set theText to value of attribute "Text"
			repeat with i from 2 to length of dtref2imgnamPairs -- start from 2 to skip header row
				tell item i of dtref2imgnamPairs
					-- item 1 of each pair is dtRef, item 2 is imgName
					if its item 1 is in theText then
						set fullDtRefLink to "![](" & item 1 & ")"
						set imgLink to "<img src=" & quote & urlPathToFolder & item 2 & quote & "/>"
						-- the next 4 lines are the usual verbose standard AppleScript find-replace 
						set my text item delimiters to fullDtRefLink
						set theTextAsList to text items of theText
						set my text item delimiters to imgLink
						set theText to theTextAsList as text
					end if
				end tell
			end repeat
			set value of attribute "Text" to theText
		end tell
	end repeat
end tell


-- handlers/subroutines
on readValsFromTSV(tsvFile)
	try
		set theLines to (every paragraph of (read tsvFile))
		set text item delimiters to tab
		set the selValsLst to {}
		repeat with aLine in theLines
			tell aLine
				try
					-- assumes text item 5 (column 5) is DEVONthink reference url, text item 1 (col 1) is image name
					set the end of selValsLst to {(text 2 thru -2 of its text item 5)} & {(text 2 thru -2 of text item 1)}
				end try
			end tell
		end repeat
		set text item delimiters to ""
		return selValsLst
	on error errorMessage number errorNumber
		set text item delimiters to ""
		error errorMessage number errorNumber
	end try
end readValsFromTSV

This works in limited testing here, but some adjustments may be needed, so feel free to post the symptoms if it doesn’t work “out of the box”. Once you get it working you’ll find that it will take some time to convert hundreds of links, but in the end should get the job done.

2 Likes

Wonderful, this is exactly the approach I was thinking about. Nice job.

1 Like

Thank you so much! This works great! But as you mentioned, I have file names in the tsv file without an extension (the extension is in the next column). Maybe you know how to change the DT settings to include the extension in the file name as well, or how to add it with a script?
Thank you so much again!

Not sure how to change it in DEVONthink. But you can try this one (some changes in the handler/subroutine):


--https://forum.eastgate.com/t/replacing-picture-links-in-imported-markdown-file/4553/16

set tsvFile to choose file with prompt "Choose TSV Metadata file"

tell application "Finder" to set pathToFolder to POSIX path of (container of tsvFile as alias)
set urlPathToFolder to "file://" & pathToFolder -- results in file:///path/to/

set dtref2imgnamPairs to readValsFromTSV(tsvFile)

tell front document of application "Tinderbox 8"
	repeat with aNote in selections
		tell aNote
			set theText to value of attribute "Text"
			repeat with i from 2 to length of dtref2imgnamPairs -- start from 2 to skip header row
				tell item i of dtref2imgnamPairs
					-- item 1 of each pair is dtRef, item 2 is imgName
					if its item 1 is in theText then
						set fullDtRefLink to "![](" & item 1 & ")"
						set imgLink to "<img src=" & quote & urlPathToFolder & item 2 & quote & "/>"
						-- the next 4 lines are the usual verbose standard AppleScript find-replace 
						set my text item delimiters to fullDtRefLink
						set theTextAsList to text items of theText
						set my text item delimiters to imgLink
						set theText to theTextAsList as text
					end if
				end tell
			end repeat
			set value of attribute "Text" to theText
		end tell
	end repeat
end tell


-- handlers/subroutines
on readValsFromTSV(tsvFile)
	try
		set theLines to (every paragraph of (read tsvFile))
		set text item delimiters to tab
		set the selValsLst to {}
		repeat with aLine in theLines
			tell aLine
				try
					-- assumes text item 5 (column 5) is DEVONthink reference url, text item 1 (col 1) is image name
					set theKind to first word of text item 2
					if theKind is "JPEG" then set theKind to "JPG"
					set the end of selValsLst to {(text 2 thru -2 of its text item 5)} & {(text 2 thru -2 of text item 1) & "." & theKind}
				end try
			end tell
		end repeat
		set text item delimiters to ""
		return selValsLst
	on error errorMessage number errorNumber
		set text item delimiters to ""
		error errorMessage number errorNumber
	end try
end readValsFromTSV
1 Like

It works just fine! Thank you so much again!