AppleScript 将最近的 Finder 文件夹作为列表返回

AppleScript 将最近的 Finder 文件夹作为列表返回

我需要编写一个 AppleScript 来返回列表Finder 中的“最近文件夹”菜单。我不想要一个只打开此菜单的 AppleScript – 我需要一个脚本以列表形式返回最近文件夹。

如果可能的话,也许可以通过对话框显示列表来演示。

是否可以?

菜单

答案1

MacScripter 线程有一个脚本可以将文件夹作为别名获取,但我无法让它在 10.8 上运行。不再有文件数据键,但这也不起作用:

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

您可以使用 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 ???? ??,

处理程序此主题在 10.7 中可以工作,但在 10.8 中不可以。

如果您只需要基本名称,则有单独的键:

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"

答案2

该脚本将返回基本名称和路径位置:

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

相关内容