Video Playback and Annotations

So I haven’t yet worked on the annotation component or even playback in DEVONthink (I will though) but I did discover that VLC has AppleScript support and includes a current time properly (to the second). So I created a script that will take the current time from VLC, get the Reference URL for the current file in DEVONthink and copy a markdown link with it and the ?time parameter to the clipboard.

I’ve got a bunch more I want to do, adapt a rich text version. Do a pair of matching scripts for QuickTime (and see if any other players have the necessary AppleScript options). I’d also like to do a more complete annotations workflow and probably adapt them for Keyboard Maestro as well but I thought you might like to see or be able to adapt what I have now.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

# # Copy Markdown Link for DEVONthink Video Open in VLC at Current Time
#
# Since DEVONthink's video player is a bit limited (it doesn't support multiple playback speeds for instance) I wanted to be able to use a more flexible player but link back to the video file in DEVONthink at the current timecode. 
#
# One minor issue, VLC only lets you get the current time to the second, not the exact frame or decimal.
# 
# This script will copy that URL as a markdown link in the format: [13:10:72](x-devonthink-item://45151EC1-8A15-4109-AFE9-1B4EA8C6DE2B?time=352)
#
# Created by [Christin White](http://christindwhite.com)
#

# # Set Options
# 
# - `DatabaseName`     *text*: The name for the database to use, if a valid database is not found the current database will be used.
# - `PausePlayback` *boolean*: If true, pause VLC when script is run.
#
set DatabaseName to "Collection"
set PausePlayback to true

try
	# Get Reference URL from DEVONthink and Assemble Link
	set TheVideo to GetVideoFromVLC(PausePlayback)
	
	# Get Reference URL
	set TheReferenceURL to GetReferenceURLFromPath(TheVideoPath of TheVideo, DatabaseName)
	
	# Assemble Link
	set TimeCodeURL to TheReferenceURL & "?time=" & (TheCurrentTime of TheVideo)
	set TimeCode to FormatTimeCode(TheCurrentTime of TheVideo)
	set MarkdownLink to "[" & TimeCode & "](" & TimeCodeURL & ")"
	
	# Copy to Clipboard
	set the clipboard to MarkdownLink
on error ErrorMessage number ErrorNumber
	if the ErrorNumber is not -128 then display alert "DEVONthink" message ErrorMessage as warning
end try


# # Functions
#
# These will be moved to a script library.
#


# # AddLeadingZeros
#
# Add leading zeros to a number to reach the specified number of digits. Based on [Apple's handler](https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateNumbers.html).
#
# Parameters:
# - `TheNumber`         *number* : The number to add leading zeros to.
# - `TheNumberOfDigits` *integer*: The total number of digits.
#
# Returns:
# - *text*: TheNumber with the specified number of leading zeros.
#
on AddLeadingZeros(TheNumber, TheNumberOfDigits)
	set IsNegative to TheNumber is less than 0
	
	# Determine the maximum number from the number of digits.
	set TheNumberOfDigits to (TheNumberOfDigits - 1)
	set TheThreshold to (10 ^ TheNumberOfDigits) as integer
	
	if TheNumber is less than TheThreshold then
		# If the number is negative, convert it to positive
		if IsNegative = true then set TheNumber to -TheNumber
		
		set TheLeadingZeros to ""
		set TheDigitCount to length of ((TheNumber div 1) as text)
		set TheCharacterCount to (TheNumberOfDigits + 1) - TheDigitCount
		repeat TheCharacterCount times
			set TheLeadingZeros to (TheLeadingZeros & "0") as text
		end repeat
		
		# Make the number negative, if it was previously negative
		if IsNegative = true then set TheLeadingZeros to "-" & TheLeadingZeros
		
		return (TheLeadingZeros & (TheNumber as text)) as text
		# If the number is greater than or equal to the maximum number of digits
	else
		# Return the original number
		return TheNumber as text
	end if
end AddLeadingZeros


# # FormatTimeCode
#
# Converts seconds into a timecode format matching `00:00:00`
#
# Parameters:
# - InSeconds *number*: Number of seconds to set timecode to. Accepts float but is converted to Integer
#
# Returns:
# - *text*: Timecode
#
on FormatTimeCode(InSeconds)
	set InSeconds to InSeconds as integer
	# Calculate Hours
	set CalculatedHours to (InSeconds div hours)
	
	# Calculate Minutes
	set RemainderSeconds to (InSeconds mod hours)
	set CalculatedMinutes to (RemainderSeconds div minutes)
	
	# Calculate Seconds
	set CalculatedSeconds to (RemainderSeconds mod minutes)
	
	# Convert to Two Digit Strings
	set CalculatedHours to AddLeadingZeros(CalculatedHours, 2)
	set CalculatedMinutes to AddLeadingZeros(CalculatedMinutes, 2)
	set CalculatedSeconds to AddLeadingZeros(CalculatedSeconds, 2)
	
	set TimeCode to CalculatedHours & ":" & CalculatedMinutes & ":" & CalculatedSeconds as text
	return TimeCode
end FormatTimeCode


# # GetReferenceURLFromPath
#
# Get the reference URL for a file in a specified or active database.
#
# Parameters:
# - `TheFilepath`  *text*: The path to the file.
# - `DatabaseName` *text*: That database to look in.
#
# Returns:
# - *text*: The Reference URL.
#
on GetReferenceURLFromPath(TheFilePath, DatabaseName)
	tell application id "DNtp"
		# Check if the database is valid, if not use the active database.
		if exists database DatabaseName then
			set TheRecordList to lookup records with path TheFilePath in database DatabaseName
		else
			display notification "The specified database wasn't found, trying the active database instead" subtitle "Invalid Database"
			set TheRecordList to lookup records with path TheFilePath
		end if
		
		# Make sure we found a matching record.
		if (count of TheRecordList) is greater than 0 then
			# A list could return more than one record such as if a file is indexed to more than one group. Use the first item but notify the user.
			if (count of TheRecordList) is greater than 1 then
				display notification "More than one record was found, using the first record" subtitle "Multiple Records"
			end if
			set TheReferenceURL to reference URL of item 1 of TheRecordList
			return TheReferenceURL
		else
			error "File not found in database."
		end if
	end tell
end GetReferenceURLFromPath


# # GetVideoFromVLC
#
# Gets metadata about the current video open in VLC
#
# Parameters:
# - `PausePlayback` *boolean*: Pause video when called.
#
# Returns:
# - *list* (indexed)
#   - `TheVideoPath`     *text*: The path for current video.
#   - `TheCurrentTime` *number*: The current second of playback.
on GetVideoFromVLC(PausePlayback)
	tell application "VLC"
		if (exists path of current item) then
			set TheVideoPath to path of current item
			set TheCurrentTime to current time
		else
			error "VLC does not have an open video."
		end if
		
		if PausePlayback and playing then
			play
		end if
		
		return {TheVideoPath:TheVideoPath, TheCurrentTime:TheCurrentTime}
	end tell
end GetVideoFromVLC

(My website is in-progress and not actually there yet. I’m also a monster who Pascal cases, soft-wraps and prefers tabs.)

Edit: Converted VLC related functions to a handler. Fixed hard-coded test value.

1 Like