Increasing page numbers in links in markdown documents

I have a number of documents in markdown that include links to pdf pages. Most of them have been created with the great Annotation Pane by Frederiko.

To increase or decrease the page number shown in the caption of markdown links, ie. (Pg. 43) , I have written this script that draws from a previous one by cgrunenberg.

I don’t know how to change regex argument from all occurrences to last occurrence. I have also been unable to include the non-capturing group for additional accuracy: b[/b]

Most importantly, it doesn’t work properly because it actually searches for the string regex finds (let’s say 59), divorced from its regex context ((Pg. ), and therefore it changes the number both to the link caption and the link destination (?page=).

It would need to make the replacement with regex itself in context -only after (Pg. . Any help?


-- Increase or decrease number in page caption "(Pg. XX)" for all PDF links in Plain Text & Markdown documents
-- This script is based on the one created by Christian Grunenberg to Replace Text in Plain Text & Markdown documents, available in https://discourse.devontechnologies.com/t/script-to-replace-text-in-given-selection-of-records/22272/1 Tue May 16 2017. Copyright (c) 2017. All rights reserved.

tell application id "DNtp"
	try
		set this_selection to the selection
		if this_selection is {} then error "Please select some records."
		
		repeat
			set page_offset to display name editor "Replace Text" info "In- or decrease link page caption by:"
			if page_offset is not "" then exit repeat
		end repeat
		
		set od to AppleScript's text item delimiters
		repeat with this_item in this_selection
			set this_type to ((type of this_item) as string)
			if this_type is "markdown" or this_type is "text" then
				set current_text to plain text of this_item
				set search_string to find text "\\b(?<=\\(Pg.\\s)[0-9]{1,4}\\b" in current_text with regexp, all occurrences and string result
				if current_text contains search_string then
					set replacement_string to ((search_string as number) + (page_offset as number)) as string
					set AppleScript's text item delimiters to search_string
					set text_item_list to every text item of current_text
					set AppleScript's text item delimiters to replacement_string
					set new_item_text to text_item_list as string
					set plain text of this_item to new_item_text
				end if
			end if
		end repeat
		set AppleScript's text item delimiters to od
	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
end tell

The following change, funny enough, would work correctly for the link destination, “?page=” &. But it doesn’t work for the link caption in this scrip, "Pg. " & :

set AppleScript's text item delimiters to "Pg. " & search_string
set text_item_list to every text item of current_text
set AppleScript's text item delimiters to "Pg. " & replacement_string

There must be some escaping problem that I cannot work out. Any help?

Where are you getting this find text command? That’s not part of DEVONthink’s AS dictionary.

Looks like Satimage

http://www.satimage.fr/software/en/smile/text/find_text.html

I did install Satimage osax, and know little more about Applescript than combining previous scripts, as Devonthink suggests.

Can anybody assist on how to modify my Applescript to address this need that I often have?

Could you post an example of the markdown file you want to change, with instructions as to what you what it changed ot and perhaps I will be able to assist with the Applescript

Frederiko

Bumping an old thread… but I’m wondering if anyone has achieved anything like this.

Most journal articles I read do not start on page 1. So, for example, the article I’m reading starts on page 37 (the first page of the PDF). Summarising highlights, therefore, works but has the wrong page number i.e. highlights from page 37 are listed under a heading of page 1. This is important when I’m citing references later.

I’m happy to fix manually, but ideally not one by one - does anyone have a script to increment all the page numbers on a page by a given number. It looks like this script was intended to do that but didn’t work?

Actually, it was a good exercise in AppleScript as I try to get familiar with it! :slight_smile:

Select the markdown/text file containing the summarised highlights, enter the offset and it’ll automatically do increment/decrement the displayed page numbers. Still being tested by me - I’ll update if I find any bugs! Currently works for a PDF of up to 100 pages which is more than I usually work with, but that can be changed in the script with minimal effort.

tell application id "DNtp"
	try
		set this_selection to the selection
		if this_selection is {} then error "Please one or more markdown or plain text summary files."
		repeat
			set page_offset to display name editor "Replace Text" info "In- or decrease link page caption by:"
			if page_offset is not "" then exit repeat
		end repeat
		--display dialog "Running"
		set od to AppleScript's text item delimiters
		repeat with this_item in this_selection
			set this_type to ((type of this_item) as string)
			if this_type is "markdown" or this_type is "«constant ****mkdn»" or this_type is "text" then
				set current_text to plain text of this_item
				repeat with loopVar from 1 to 100
					--Search for the page numbers
					set search_string to ("## [Page " & (loopVar) as string) & "]"
					--Build the replacement string, including temp name
					set replacement_string to ("## [Xage " & ((loopVar as number) + (page_offset as number)) as string) & "]"
					if current_text contains search_string then
						set AppleScript's text item delimiters to search_string
						set text_item_list to every text item of current_text
						set AppleScript's text item delimiters to replacement_string
						set current_text to text_item_list as string
					end if
				end repeat
				-- Renames the temporary page name that was used to avoid loop
				if current_text contains "Xage" then
					set AppleScript's text item delimiters to "Xage"
					set text_item_list to every text item of current_text
					set AppleScript's text item delimiters to "Page"
					set current_text to text_item_list as string
				end if
				set plain text of this_item to current_text
			end if
		end repeat
		set AppleScript's text item delimiters to od
	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
end tell