保存并恢复Finder窗口列表

时间:2014-02-26 08:37:46

标签: applescript finder

我不记得我是怎么做的,但我曾经用Butler编写了键盘快捷键,运行了一些AppleScript,我感谢多个来源。

一个脚本用于将所有Finder窗口属性保存到文本文件(提示输入名称),另一个脚本将读取文件(再次提示输入名称)并相应地恢复Finder窗口。

文本文件看起来像这样:

{
    {
        folder "Desktop"
        of folder "Me"
        of folder "Users"
        of startup disk of application "Finder", {
            0, 0, 1200, 315
        }, column view, 192
    }, {
        folder "Backups"
        of disk "SAFE"
        of application "Finder", {
            0, 380, 1200, 685
        }, column view, 192
    }
}

从那里,我知道如何恢复窗户。我刚丢失了我制作的脚本,它会检索所有这些值......

恢复窗口:

tell application "Finder"

set windowList to {
    {
        folder "Desktop"
        of folder "Me"
        of folder "Users"
        of startup disk of application "Finder", {
            0, 0, 1200, 315
        }, column view, 192
    }, {
        folder "Backups"
        of disk "SAFE"
        of application "Finder", {
            0, 380, 1200, 685
        }, column view, 192
    }
}
close every window

    repeat with i from 1 to count of windowList
        set theseProps to item i of windowList
        make new Finder window at front to item 1 of theseProps
        tell window 1
            set bounds to item 2 of theseProps
            set current view to item 3 of theseProps
            set sidebar width to item 4 of theseProps
        end tell
    end repeat
end tell

我的问题: 如何使用AppleScript获取所有Finder窗口和属性? 以及如何将它们保存到文件中?

[编辑]

以下是如何获得所需的所有属性:

set windowsList to {}
tell application "Finder"
    set theWindows to windows
    repeat with theWindow in theWindows
        set t to target of theWindow
        set b to bounds of theWindow
        set v to current view of theWindow
        set w to sidebar width of theWindow
        copy {t, b, v, w} to end of windowsList
    end repeat
end tell

现在我需要一种方法将其保存在文件中,以后可以将其作为列表加载...

[编辑]

我能够以这种方式写入文件:

    set prefs_folder to path to preferences folder as string
set prefs_file to prefs_folder & "finderwindows"
try
    set open_file to ¬
        open for access file prefs_file with write permission
    -- erase current contents of file:
    set eof of open_file to 0
    write windowList to open_file starting at eof
    close access open_file
on error
    try
        close access file prefs_file
    end try
end try

但文件仍然是空的。脚本退出时出现错误-10004 ...

我现在的问题是:

如何将windowList转换为文件可保存的格式然后我可以通过读取另一个脚本来检索?

1 个答案:

答案 0 :(得分:2)

仅供参考:我有一个名为Simple WindowSets的程序正是这样做的...保存和恢复Finder窗口的集合。它很受欢迎。因此,尽管你可以按照你的要求使用applescript,但也许你会感兴趣。找到它here

顺便说一下,回答你的具体问题,这是我用来写一个文件的处理程序。祝你好运。

on writeTo(targetFile, theData, dataType, apendData)
    -- targetFile is the path to the file you want to write
    -- theData is the data you want in the file.
    -- dataType is the data type of theData and it can be text, list, record etc.
    -- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
    try
        set targetFile to targetFile as text
        if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
        set openFile to open for access file targetFile with write permission
        if apendData is false then set eof of openFile to 0
        write theData to openFile starting at eof as dataType
        close access openFile
        return true
    on error
        try
            close access file targetFile
        end try
        return false
    end try
end writeTo