New Scripts for MarginNote4

Hi,

MarginNote has finally brought out a long-awaited new major version and released MarginNote 4. It’s great!

Since the old Hookmark scripts no longer worked, I’ve created a very simple replacement based on UI scripting. It seems to work without issues in my testing.

Get Name

try
    tell application "System Events" to tell process "MarginNote 4"
        delay 0.1

        click menu item "Copy Card Clone" of menu "Card" of menu bar 1
        delay 0.1
        set clipboardContent to the clipboard
    end tell
    
    -- Retain only the content up to the first line break
    set theURL to paragraph 1 of clipboardContent

    return theURL

on error msg
    display dialog msg
end try

Get Address

try
    tell application "System Events" to tell process "MarginNote 4"
        delay 0.1

        click menu item "Copy Card URL" of menu "Card" of menu bar 1
        delay 0.1
        set clipboardContent to the clipboard
    end tell

set theTitle to the clipboard


    return theTitle

on error msg
    display dialog msg
end try
1 Like

Note that I’ve now modified the “Get Name” script in the original post to include only the title of the card when creating a Hook.

Thank you very much for your scripts, @AW2307 .

The script server has been updated. The version is 327.

Please let us know if there is any problem.

Thank you

1 Like

Could Hookmark please be compatible to Marginnote 4?

Thank you for getting in touch, @Elise .

Hookmark is supposed to work with cards of Marginnote 4, since script version 327.

What’s your system language?

I’ve decoded MarginNote’s UI status URL and would like to share a solution for getting the current document link in a study set.

Get Name

tell application "System Events"
	tell process "MarginNote 4"
		set the clipboard to ""
		
		-- Check if an item is selected by seeing if "Copy" is enabled
		if enabled of menu item "Copy" of menu 1 of menu bar item "Edit" of menu bar 1 then
			-- An item is selected, so copy it
			click menu item "Copy" of menu 1 of menu bar item "Edit" of menu bar 1
			delay 0.1
			set theTitle to ""
			
			-- Wait for the clipboard to be populated
			repeat 3 times
				set theItem to the clipboard
				delay 0.1
				try
					set theTitle to item 1 of paragraphs of theItem
				end try
				if theTitle is not null and the length of theTitle is not 0 then
					exit repeat
				else
					delay 0.2
				end if
			end repeat
			return theTitle
		else
			-- No item is selected, get the window name from the "Window" menu
			try
				tell application "System Events"
					set frontApp to name of first application process whose frontmost is true
					if frontApp is "MarginNote 4" then
						tell application process "MarginNote 4"
							set theWin to front window
							set theName to name of theWin
							return theName
						end tell
					else
						return "MarginNote 4 is not in the foreground."
					end if
				end tell
			on error errMsg
				return "Error:" & errMsg
			end try
			
		end if
	end tell
end tell

Get Address


tell application "System Events"
	tell process "MarginNote 4"
		set the clipboard to ""
		
		if enabled of menu item "Copy Card URL" of menu 1 of menu bar item "Card" of menu bar 1 then
			click menu item "Copy Card URL" of menu 1 of menu bar item "Card" of menu bar 1
			delay 0.1
			
			
			set theURL to ""
			repeat 3 times
				set theURL to the clipboard
				if theURL is not null and the length of theURL is not 0 then
					exit repeat
				else
					delay 0.1
				end if
			end repeat
			
			return theURL
		else
			
			try
				click menu item "UI Status URL" of menu 1 of menu bar item "StudySet" of menu bar 1
				delay 0.1
				
				set theURL to ""
				repeat 3 times
					set theURL to the clipboard
					if theURL is not null and the length of theURL is not 0 then
						exit repeat
					else
						delay 0.1
					end if
				end repeat
				
				set pythonScript to "decode_mnurl.py"  -- remember to replace the path 
				set topicId to do shell script "python3 " & quoted form of pythonScript & " " & quoted form of theURL
				
				return topicId
			on error
				
				return "Cannot get UI Status URL"
			end try
		end if
		
	end tell
end tell

:warning: You’ll need a Python environment, and you’ll have to replace the Python file decode_mnurl.py address in the “Get address”.