Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to write an AppleScript that will return me a list of the Recent Folders menu in Finder. I don't want an AppleScript that will simply open this menu – I need a script to return the recent folders as a list.

If this is possible, it might be an idea to demonstrate by displaying the list with a dialog, perhaps.

Is it possible?

Menu

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

This MacScripter thread has a script for getting the folders as aliases, but I couldn't get it to work on 10.8. There's no longer file-data keys, but this didn't work either:

tell application "System Events"
    value of property list item "file-bookmark" of property list item 1 of property list item "FXRecentFolders" of contents of property list file "~/Library/Preferences/com.apple.finder.plist"
    try
        value of result
    on error err
        text 30 thru -4 of err
        (run script "«data alis" & result & "»") as alias
    end try
end tell

You can print the plain text values with PlistBuddy:

$ PlistBuddy -c 'Print FXRecentFolders:0:file-bookmark' ~/Library/Preferences/com.apple.finder.plist
book?0?Userslau?4A?x?iH???A?1M?$5DF7A03E-A7FB-3E80-B61D-F10CD8BF7B5D?/?6a0c3f51ea4eaf67e96c08fa9b69b93aee598f01;00000000;0000000000000020;com.apple.app-sandbox.read-write;00000001;01000002;000000000005ca9f;/users/lauri?????$Tt@d  ? ? ? ?  ?0 ???? ??,

The handler in this thread worked in 10.7 but not 10.8.

If you only need the basenames, there's separate keys for them:

tell application "System Events" to value of property list item "name" of property list items of property list item "FXRecentFolders" of contents of property list file "~/Library/Preferences/com.apple.finder.plist"

share|improve this answer
    
Thanks for your response. So it is possible to get the paths to these recent folders back as a list? The last example you gave works but,as you say, only returns the basenames. –  Oliver Joseph Ash Jan 16 '13 at 16:23
    
I guess it's possible, but I don't know how. The format seems to change in every version of OS X. –  Lri Jan 16 '13 at 16:35
add comment

This script will return the basenames and path location:

on findPathSeparator(theData, theFile)
    set pathSeparator to {0, 0, 0, 1, 1, 0, 0}
    set bytesFound to 0
    set bytesSearched to 0

    try
        read theFile from 0 for 0

        set numIterations to 0

        repeat (get eof theFile) times
            set theId to id of (read theFile from bytesSearched for 1)

            if theId is item (bytesFound + 1) of pathSeparator then
                set bytesFound to bytesFound + 1
            else
                set bytesFound to 0
            end if

            if bytesFound is (count of pathSeparator) then exit repeat

            set bytesSearched to bytesSearched + 1
        end repeat
    on error msg
        msg
    end try

    return bytesSearched - (count of pathSeparator)
end findPathSeparator

on getPathFromData(theData)
    set pathSeparator to {0, 0, 0, 1, 1, 0, 0}

    set theFile to (open for access POSIX file ("/tmp/get_recent_folders") with write permission)

    set eof theFile to 0

    write contents of theData to theFile

    set startPosition to findPathSeparator(theData, theFile)

    try
        read theFile from startPosition for 0

        set thePath to ""

        repeat
            set idList to id of (read theFile for 8)

            if (idList does not end with pathSeparator) then exit repeat

            set theLength to item 1 of idList

            set thePath to thePath & ("/" & (read theFile for theLength as «class utf8»))

            read theFile for (4 - theLength mod 4) mod 4
        end repeat
    on error msg
        msg
    end try

    close access theFile

    return thePath
end getPathFromData

tell application "System Events"
    tell property list file "~/Library/Preferences/com.apple.finder.plist"
        set dataItems to property list item "FXRecentFolders"'s property list items's property list item "file-bookmark"'s value
        set itemNames to property list item "FXRecentFolders"'s property list items's property list item "name"'s value
    end tell
end tell

set theOutput to ""

set itemNum to 1
repeat (count of dataItems) times
    set theOutput to theOutput & item itemNum of itemNames & "
" & getPathFromData(item itemNum of dataItems) & "
"
    set itemNum to itemNum + 1
end repeat

theOutput
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.