Updating webarchives

This script creates new webarchives from selected webarchives and inherits properties.

Comment out every property that should not be inherited in the “Inherit properties” block.
To do so prefix the line with #

-- Create new webarchive from selected webarchive and inherit properties

-- Note:		New webarchives may not contain the content you expect. It's necessary to manually check every new webarchive before you delete the old one
-- Setup:		Comment out every property that should not be inherited in the "Inherit properties" block. To do so prefix the line with #

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select some webarchives"
		show progress indicator "Creating webarchive..." steps (count theRecords) as string with cancel button
		
		repeat with thisRecord in theRecords
			set thisType to (type of thisRecord) as string
			if thisType is in {"webarchive", "«constant ****wbar»"} then
				set thisRecord_Name to name without extension of thisRecord
				step progress indicator thisRecord_Name
				
				set thisNewWebarchive to create web document from (URL of thisRecord as string) in parent 1 of thisRecord -- Create new webarchive in selected webarchive's group
				
				set URL of thisNewWebarchive to my getWebResourceURLKey(path of thisNewWebarchive) -- Set record's URL to webarchive's internal URL (the one that was actually used to create the content). Necessary in case of redirections 
				
				tell thisNewWebarchive -- Inherit properties
					set name to thisRecord_Name -- The name
					set aliases to aliases of thisRecord -- Wiki aliases
					set comment to comment of thisRecord -- The comment
					set creation date to creation date of thisRecord -- The creation date
					try
						set custom meta data to custom meta data of thisRecord -- User-defined metadata of a record
					end try
					set exclude from search to exclude from search of thisRecord -- Exclude record from searching.
					set exclude from see also to exclude from see also of thisRecord -- Exclude record from see also.					
					set exclude from Wiki linking to exclude from Wiki linking of thisRecord -- Exclude record from automatic Wiki linking.
					set label to label of thisRecord -- Index of label (0-7)
					set locking to locking of thisRecord -- The locking
					set rating to rating of thisRecord -- Rating (0-5)
					set state to state of thisRecord -- The state/flag
					set tags to tags of thisRecord -- The tags
					try
						set thumbnail to thumbnail of thisRecord -- The thumbnail
					end try
					set unread to unread of thisRecord -- The unread flag
				end tell
				
			end if
		end repeat
		
		hide progress indicator
	on error error_message number error_number
		hide progress indicator
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on getWebResourceURLKey(thePath)
	try
		set theWebarchiveData to current application's NSData's dataWithContentsOfFile:thePath
		set theWebarchivePlist to (current application's NSPropertyListSerialization's propertyListWithData:theWebarchiveData options:0 |format|:(current application's NSPropertyListBinaryFormat_v1_0) |error|:(missing value))
		set theWebResourceURLKey to (theWebarchivePlist's valueForKeyPath:"WebMainResource.WebResourceURL") as string
		return theWebResourceURLKey
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"getWebResourceURLKey\"" message error_message as warning
		error number -128
	end try
end getWebResourceURLKey

1 Like