保存并恢复Finder窗口列表



我不记得我是怎么做到的,但我曾经用Butler映射键盘快捷键,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窗口和属性?如何将它们保存到文件中?

[EDIT]

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

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

现在我需要一种将其保存在文件中的方法,以便稍后能够将其加载为列表…

[EDIT]

我可以这样写文件:

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转换为可保存的文件格式,然后通过从另一个脚本中读取来检索??

FYI:我有一个名为"简单窗口集"的程序,它正是这样做的。。。保存和恢复Finder窗口集。它很受欢迎。所以,尽管你可以用applescriptt按照你的要求做,但也许你会感兴趣。在这里找到它。

顺便说一句,为了回答您的具体问题,这里有一个我用来写入文件的处理程序。祝你好运

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

最新更新