[OS X TeX] A script to get the list of recently opened files of an application (e.g. TeXShop)

Ramón Figueroa-Centeno ramonf at hawaii.edu
Mon Apr 13 23:30:10 EDT 2009


Aloha,

As part of a somewhat larger project I wanted to know the list of recently opened files for TeXShop, when it is not running (it should work on most applications that use the standard scheme to store recently opened files in a plist). The solution is not trivial (I cobbled it together from different pieces of code that I found in MacScripter, plus some code of my own).

I can see it being useful to do things with your most recent documents like: 

1. Backing up.
2. Emailing yourself copies of your most recent documents. 
3. Have copies placed in your Public Folder. 
4. Print copies of your most recent documents. 
5. Launch TexShop opening your most recent n documents (n<=10 in my setup).

So here it is:

-- Script that get the list of recently opened items from an application's preference plist
-- Note: The application probably must quit before this list gets updated.

property prefName : "TeXShop.plist"

set p2p to (path to preferences from user domain as text) & prefName
tell application "System Events"
	try
		set listRecents to value of property list item "NSRecentDocumentRecords" of property list file p2p
	on error
		return
	end try
end tell

set Recent to {}
repeat with r in items of listRecents
	set alis to |_NSAlias| of |_NSLocator| of r
	
	try
		alis as text
	on error errmsg
		set savedDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "****"
		set errmsg to text item 2 of errmsg
		set AppleScript's text item delimiters to "»"
		set errmsg to text item 1 of errmsg
		set AppleScript's text item delimiters to ""
		set AppleScript's text item delimiters to savedDelimiters
	end try
	
	set decode to do shell script "echo " & quoted form of errmsg & " | xxd -r -p"
	
	set nullchar to ASCII character 0
	
	set savedDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to nullchar
	
	set maybe to {}
	repeat with p in text items of decode
		if ((count of p) > 1) and p contains ":" then copy p to end of maybe
	end repeat
	
	tell application "System Events"
		set found to false
		repeat with f in maybe
			if exists file f then
				set found to true
				exit repeat
			else
				set f to text 2 thru -1 of f
				if exists file f then
					set found to true
					exit repeat
				end if
			end if
		end repeat
	end tell
	
	set AppleScript's text item delimiters to savedDelimiters
	
	if found then
		copy f to end of Recent
	end if
end repeat

return Recent

Enjoy!

Ramón
-- 
View this message in context: http://n2.nabble.com/A-script-to-get-the-list-of-recently-opened-files-of-an-application-%28e.g.-TeXShop%29-tp2631029p2631029.html
Sent from the MacOSX-TeX mailing list archive at Nabble.com.




More information about the MacOSX-TeX mailing list