Trash Mountain Lion "Version" files

I would like to write an applescript that will automatically trash Mountain Lion’s annoying (imao!) “Version” files from the directory that Pages documents are saved in.

These are the files with “~” appended that the system automatically generates as a version to revert to.
Maybe it’s only Pages that writes these version files to the same directory… don’t quote me but I think TextEdit more sensibly hides them away in ~/Library/Saved Application State. I’m not sure because I’ve disabled AutoSave on my system through a defaults write fix, and I no longer see these files with TextEdit. However, despite disabling Pages from AutoSaving, It still generates version files which I believe cannot be switched off. I don’t use them, I don’t need them, and I want to stop them cluttering up my folders!

So, my thinking is to check the “com.apple.iWork.Pages.LSSharedFileList.plist” to see what recent documents have been opened. To scour the directories they live in for any files ending in “~”, and trash them.

Thanks to Nigel Harvey & McUserII from a previous post, I have the following to read the “Bookmark” entries in the plist:

set plistPath to (path to preferences from user domain as text) & "com.apple.iWork.Pages.LSSharedFileList.plist"

--read pList
tell application "System Events"
	tell property list file plistPath
		if (it exists) then
			tell property list item "RecentDocuments"
				tell property list item "CustomListItems"
					
					set RecentBookmarks to {value of property list item "Bookmark"} of every property list item --<data>
					
				end tell
			end tell
		end if
	end tell
end tell

get RecentBookmarks

However this returns DATA!

Can I get file paths from this?

Operating System: Mac OS X (10.8)

Just for interest, this is how I’ve switched off AutoSave:

Terminal command “defaults write com.apple.iWork.Pages ApplePersistence -bool no”
“Ask me to keep changes when closing documents” in System Preferences/General is checked.
“Back up previous versions when saving” in Pages Preferences/General is unchecked.

Writing the data to file and reading them back 'as «class bmrk» works:

set plistPath to (path to preferences from user domain as text) & "com.apple.iWork.Pages.LSSharedFileList.plist"

--read pList
tell application "System Events"
	tell property list file plistPath
		if (it exists) then
			tell property list item "RecentDocuments"
				tell property list item "CustomListItems"
					
					set RecentBookmarks to value of property list item "Bookmark" of every property list item --<data>
					
				end tell
			end tell
		else
			set RecentBookmarks to {}
		end if
	end tell
end tell

if (RecentBookmarks is not {}) then
	set accessRef to (open for access file ((path to temporary items as text) & "Bookmark.dat") with write permission)
	repeat with i from 1 to (count RecentBookmarks)
		try
			set eof accessRef to 0
			write item i of RecentBookmarks to accessRef
			set item i of RecentBookmarks to (read accessRef from 1 as «class bmrk»)
		on error errMsg
			display dialog errMsg
		end try
	end repeat
	close access accessRef
end if

RecentBookmarks

This is just to solve the stated scripting problem. I’ve not had time to reach an opinion on whether deleting version files is a good idea or not.

Thanks a lot!

This works:

--"trash version files" v1 

--Trash automatically generated Pages version files (i.e docname appended "~")
--for MacOSX 10.8
--save as app
-----------------------

set plistPath to (path to preferences from user domain as text) & "com.apple.iWork.Pages.LSSharedFileList.plist"

--SCRIPT:

--welcome
activate
display dialog "Trash Pages version files?" with icon 1

--initialize
set iquitpages to false
tell application "System Events" to get name of processes
set application_list to result
if application_list contains "Pages" is true then
	display dialog "Pages will quit!" buttons {"Cancel", "Continue"} default button 2 with icon 0
	tell application "Pages" to quit
	set iquitpages to true
	delay 2
end if

--read plist
tell application "System Events"
	tell property list file plistPath
		if (it exists) then
			tell property list item "RecentDocuments"
				tell property list item "CustomListItems"
					set RecentBookmarks to value of property list item "Bookmark" of every property list item --<data>
				end tell
			end tell
		else
			set RecentBookmarks to {}
		end if
	end tell
end tell

--get recent file paths
if (RecentBookmarks is not {}) then
	set accessRef to (open for access file ((path to temporary items as text) & "Bookmark.dat") with write permission)
	repeat with i from 1 to (count RecentBookmarks)
		try
			set eof accessRef to 0
			write item i of RecentBookmarks to accessRef
			set item i of RecentBookmarks to (read accessRef from 1 as «class bmrk») --aliases!!
		on error errMsg
			display dialog errMsg
		end try
	end repeat
	close access accessRef
	
	--...get containing folders
	tell application "Finder"
		set RecentFolders to {}
		repeat with j from 1 to (count RecentBookmarks)
			set this_folder to container of (item j of RecentBookmarks) as text
			if this_folder is not in RecentFolders then
				set RecentFolders to RecentFolders & {this_folder}
			end if
		end repeat
		
		--search for version files
		set VersionFiles to {}
		repeat with m from 1 to count of RecentFolders
			set these_files to every file of folder (item m of RecentFolders)
			repeat with k from 1 to count of these_files
				if ((name of item k of these_files) as text) contains "~.pages" then
					set VersionFiles to VersionFiles & {(item k of these_files) as text}
				end if
			end repeat
		end repeat
	end tell
	
	--select version files to trash
	activate
	try
		set filesToTrash to (choose from list VersionFiles with prompt "Delete files(s):" OK button name "Delete" with multiple selections allowed)
	on error
		--no recent items(??), or plist not updated since last trash
		display dialog "No version files found!" buttons {"Cancel"} default button 1 with icon 0
	end try
	
	if (filesToTrash is false) then
		
		--user cancelled
		error number -128
	else
		
		--trash files
		tell application "Finder"
			repeat with p from 1 to count of filesToTrash
				delete item p of filesToTrash
			end repeat
		end tell
		
		--done
		activate
		display dialog "File(s) deleted." buttons {"Done"} default button 1 with icon 1
	end if
else
	
	--no recent docs, so quit...
	activate
	display dialog "No version files found!" buttons {"Cancel"} default button 1 with icon 0
end if


This is not ruling the set of datas stored by AutoSave but the one stored by Resume so that the system may reopen the application with the open at close windows open at restart.

It’s a huge mistake but it’s one which is widely spead by users understanding nothing to the features introduced by 10.7
Datas related to AutoSave are stored in the folder :

Macintosh HD:Users::Library:Autosave Information:

Datas related to Version are stored in the hidden folder

Macintosh HD:Applications:.DocumentRevisions-V100

These late ones allowed several users to recover dats lost because thei files were corrupted so that Pages was unable to reopen them.
They retreived the last version in the named folder thanks to my set of scripts entitled :

Versions as Recovery Tool.

KOENIG Yvan (VALLAURIS, France) vendredi 30 août 2013 22:01:39